IPC::Semaphore(3perl)

πŸ’– NAME πŸ’–

okay so bestie, welcome to IPC::Semaphore β€” it's the SysV Semaphore IPC object class. that's the whole vibe. it's the cute object-y interface to SysV IPC semaphores. (and hey, the header up top says IPC::Semaphore(3perl) from the Perl Programmers Reference Guide, so like, you're in the right place 🫢)


✨ SYNOPSIS ✨

here's the whole thing in action, copy-paste energy, no notes:

use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR IPC_CREAT);
use IPC::Semaphore;

$sem = IPC::Semaphore->new(IPC_PRIVATE, 10, S_IRUSR | S_IWUSR | IPC_CREAT);

$sem->setall( (0) x 10);

@sem = $sem->getall;

$ncnt = $sem->getncnt;

$zcnt = $sem->getzcnt;

$ds = $sem->stat;

$sem->remove;

like… the range? the range is ranging. πŸ’…


🌸 DESCRIPTION 🌸

so basically, this is a class that gives you an object based interface to SysV IPC semaphores. that's it, that's the whole main character moment. it wraps the raw syscall nonsense in a nice little object so you don't have to rawdog the C API. iconic behavior, honestly.


πŸ‘‘ METHODS πŸ‘‘

πŸŽ€ new ( KEY , NSEMS , FLAGS )

creates a new semaphore set associated with KEY. NSEMS is the number of semaphores in the set. a new set gets created if:

  • KEY is equal to IPC_PRIVATE
  • KEY does not already have a semaphore identifier associated with it, and FLAGS & IPC_CREAT is true.

when a new semaphore set is created, FLAGS is what sets the permissions. and listen β€” be careful not to set any flags that the Sys V IPC implementation does not allow: on some systems, setting execute bits makes the operations fail. like it just says no and everything breaks. so don't do that πŸ™ˆ

lowkey the execute-bits thing is a trap. it's giving "why is nothing working" and the answer is the execute bits. beware.

πŸ”₯ getall

returns the values of the semaphore set as an array. that's it. all of them. every single one. slay.

πŸ’œ getncnt ( SEM )

returns the number of processes waiting for the semaphore SEM to become greater than its current value.

πŸ¦‹ getpid ( SEM )

returns the process id of the last process that performed an operation on the semaphore SEM. it's giving receipts. it remembers who touched it last πŸ‘€

⭐ getval ( SEM )

returns the current value of the semaphore SEM.

🌈 getzcnt ( SEM )

returns the number of processes waiting for the semaphore SEM to become zero.

πŸ“ id

returns the system identifier for the semaphore set.

πŸ’… op ( OPLIST )

OPLIST is a list of operations to pass to semop. OPLIST is a concatenation of smaller lists, each which has three values. the first is the semaphore number, the second is the operation and the last is a flags value. see semop(2) for more details. for example:

$sem->op(
    0, -1, IPC_NOWAIT,
    1,  1, IPC_NOWAIT
);

three values, three values, go off. math is mathing πŸ“βœ¨

🧚 remove

remove and destroy the semaphore set from the system. it's gone. deleted. archived. no take-backs. πŸ’€

πŸŽ€ set ( STAT )

πŸŽ€ set ( NAME => VALUE [, NAME => VALUE ...] )

set will set the following values of the stat structure associated with the semaphore set:

uid
gid
mode (only the permission bits)

set accepts either a stat object, as returned by the stat method, or a list of name-value pairs. your choice, bestie, both are valid 🫢

✨ setall ( VALUES )

sets all values in the semaphore set to those given on the VALUES list. VALUES must contain the correct number of values. (miss one? that's a you problem, no cap.)

🌸 setval ( N , VALUE )

set the Nth value in the semaphore set to VALUE.

πŸ‘‘ stat

returns an object of type IPC::Semaphore::stat which is a sub-class of Class::Struct. it provides the following fields. for a description of these fields see your system documentation.

uid
gid
cuid
cgid
mode
ctime
otime
nsems

and fr fr, see your system documentation for what those actually mean β€” the man page is not gonna guess for you πŸ’–


πŸ¦‹ SEE ALSO πŸ¦‹

like the whole friend group. they come as a set. iykyk πŸ’œ


πŸ’œ AUTHORS πŸ’œ

  • Graham Barr <gbarr@pobox.com>
  • Marcus Holland-Moritz <mhx@cpan.org>

two absolute legends. legends only. periodt. πŸ‘‘


Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz.

Version 1.x, Copyright (c) 1997, Graham Barr.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


that's the whole page bestie. go make some semaphores, be careful with your flags, and don't set execute bits unless you wanna have a bad time 😭

and that's a wrap on this perl v5.44.0 era of the docs, dated 2026-10-17 β€” IPC::Semaphore(3perl), signing off πŸŽ€βœ¨πŸ’…

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms