IPC::Semaphore(3pm)

πŸ’– NAME πŸ’–

IPC::Semaphore β€” the SysV Semaphore IPC object class, bestie. Like, it's a whole class that gives you an object-based interface to SysV IPC semaphores, and yes it's from the Perl Programmers Reference Guide, section 3pm, we love a main-character manpage ✨


🌸 SYNOPSIS 🌸

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;

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so picture this: it's a class that provides an object based interface to SysV IPC semaphores. That's literally it, that's the whole vibe. You get semaphores, but ~objecty~ πŸŽ€


✨ METHODS ✨

πŸ’… new ( KEY , NSEMS , FLAGS )

Creates a new semaphore set associated with KEY. NSEMS is how many semaphores are in the set, periodt. 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 used to set the permissions. 🫢 But bestie, 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. No cap, it will just break on you 😭


πŸ”₯ getall

Returns the values of the semaphore set as an array. Simple, iconic, no notes.


🌈 getncnt ( SEM )

Returns the number of processes waiting for the semaphore SEM to become greater than its current value. It's giving very "how many people are queued at the door" energy πŸ™ˆ


⭐ getpid ( SEM )

Returns the process id of the last process that performed an operation on the semaphore SEM.


πŸ“ getval ( SEM )

Returns the current value of the semaphore SEM. Literally just the value. Slay.


🧚 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 of 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
);

Triplets of chaos, honestly, but we stan a structured list πŸ’…


πŸ’€ remove

Remove and destroy the semaphore set from the system. Gone, deleted, not coming back, bye bestie 😭


πŸŽ€ 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. Either way works, we're flexible like that 🫢


🌸 setall ( VALUES )

Sets all values in the semaphore set to those given on the VALUES list. VALUES must contain the correct number of values. Like, do not try to be cute and pass the wrong amount, it needs the correct number, fr fr.


πŸ¦‹ setval ( N , VALUE )

Set the Nth value in the semaphore set to VALUE. Targets one specific semaphore, very focused, very main character energy πŸ‘‘


✨ 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

All eight of them, listed and accounted for, no skips 🌟


πŸŽ€ SEE ALSO πŸŽ€

IPC::SysV, Class::Struct, semget(2), semctl(2), semop(2) β€” go read those too, iykyk πŸ’–


πŸ‘‘ AUTHORS πŸ‘‘

Graham Barr <gbarr@pobox.com>, Marcus Holland-Moritz <mhx@cpan.org> β€” absolute legends, we thank them for their service 🫢


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. Slay, open source icon behavior πŸ’…


okay that's the whole page bestie, go forth and semaphore responsibly πŸ¦‹βœ¨

Sign-off tea β˜•: this little love letter is perl v5.42.3, dated 2026-11-03, and it lives at IPC::Semaphore(3pm) β€” mwah, take care of her πŸ’œ

πŸ“¦ where this page comes from