SEMOP(2)

πŸ’– semop(2) β€” bestie's guide to semaphore ops πŸ’–

okay so this is the semop page from section 2 of the manual (system calls, duh), and it is literally the most unhinged little API in the whole IPC girlboss universe. atomic array of operations on a semaphore set. say that five times fast. πŸŽ€


✨ LIBRARY ✨

Standard C Library (libc, -lc). she's a libc girlie, always has been. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

int
semop(int semid, struct sembuf *array, size_t nops);

🌸 DESCRIPTION 🌸

okay so here's the tea. semop() is a system call that atomically does the whole array of operations that array points at, on the semaphore set that semid refers to. like, all of them or none of them, no in-between, no half-finished mess. nops is how long that array is.

every single operation is packed into a struct sembuf, which looks like this:

struct sembuf {
        u_short sem_num;        /* semaphore # */
        short   sem_op;         /* semaphore operation */
        short   sem_flg;        /* operation flags */
};

so for each element in array, the sem_op and sem_flg decide what happens to semaphore number sem_num in the set. and you can OR the values SEM_UNDO and IPC_NOWAIT into that sem_flg member to change up the vibe of the operation. ✨

what actually goes down depends on sem_op's value, and it goes a lil something like this:

  • when sem_op is positive and the process has alter permission, the semaphore's value gets incremented by sem_op's value. and if SEM_UNDO was specified, the semaphore's adjust on exit value gets decremented by sem_op's value. a positive sem_op generally means the process is releasing a resource that's associated with the semaphore. bye bestie, u did ur job πŸ’–
  • when sem_op is negative and the process has alter permission, what happens depends on the semaphore's current value, and it's giving choose-your-own-adventure:
    • if the current value of the semaphore is greater than or equal to the absolute value of sem_op, then the value gets decremented by the absolute value of sem_op. and if SEM_UNDO was specified, the semaphore's adjust on exit value gets incremented by the absolute value of sem_op.
    • if the current value of the semaphore is less than the absolute value of sem_op, one of these things happens instead:
      • if IPC_NOWAIT was specified, then semop() returns immediately with a return value of EAGAIN. not waiting, not today. πŸ’€
      • otherwise, the calling process gets put to sleep until one of these conditions is satisfied:
        • some other process removes the semaphore with the IPC_RMID option of semctl(2). in this case, semop() returns immediately with a return value of EIDRM.
        • the process receives a signal that is to be caught. in this case, the process will resume execution as defined by sigaction(2).
        • the semaphore's value is greater than or equal to the absolute value of sem_op. when this condition becomes true, the semaphore's value is decremented by the absolute value of sem_op, and the semaphore's adjust on exit value is incremented by the absolute value of sem_op.
a negative value for sem_op generally means a process is waiting for a resource to become available. she's patient. she's waiting. she's in her waiting era. 🧚
  • when sem_op is zero and the process has read permission, one of these will occur:
    • if the current value of the semaphore is equal to zero, then semop() can return immediately. slay.
    • if IPC_NOWAIT was specified, then semop() returns immediately with a return value of EAGAIN.
    • otherwise, the calling process gets put to sleep until one of these conditions is satisfied:
      • some other process removes the semaphore with the IPC_RMID option of semctl(2). in this case, semop() returns immediately with a return value of EIDRM.
      • the process receives a signal that is to be caught. in this case, the process will resume execution as defined by sigaction(2).
      • the semaphore's value becomes zero.

for every semaphore a process has in use, the kernel keeps an "adjust on exit" value, which is the thing we kept hinting at earlier like a lil cliffhanger. when a process exits β€” voluntarily or involuntarily, doesn't matter, no take-backs β€” the adjust on exit value for each semaphore gets added to that semaphore's value. this is how you make sure a resource gets released even if a process just. dies. mid-thought. no cap, it's the safety net of the whole system. 🫢


πŸ‘‘ RETURN VALUES πŸ‘‘

semop() returns the value 0 if it succeeded. otherwise, the value -1 is returned, and the global variable errno gets set to tell you which error it was. iconic behaviour honestly.


πŸ”₯ ERRORS πŸ”₯

the semop() system call will fail if:

  • [EINVAL] β€” no semaphore set corresponds to semid, or the process would exceed the system-defined limit for the number of per-process SEM_UNDO structures.
  • [EACCES] β€” permission denied, because the operation and the mode of the semaphore set don't match up. mismatch energy. πŸ™ˆ
  • [EAGAIN] β€” the semaphore's value would have resulted in the process being put to sleep, and IPC_NOWAIT was specified.
  • [E2BIG] β€” too many operations were specified. [SEMOPM]
  • [EFBIG] β€” sem_num was not in the range of valid semaphores for the set.
  • [EIDRM] β€” the semaphore set was removed from the system.
  • [EINTR] β€” the semop() system call was interrupted by a signal.
  • [ENOSPC] β€” the system SEM_UNDO pool [SEMMNU] is full.
  • [ERANGE] β€” the requested operation would cause either the semaphore's current value [SEMVMX] or its adjust on exit value [SEMAEM] to exceed the system-imposed limits.

🌈 SEE ALSO 🌈

semctl(2), semget(2), sigaction(2)


πŸ’€ BUGS πŸ’€

the semop() system call may block waiting for memory even if IPC_NOWAIT was specified. yes really. i know. she said "no wait" and then she waited. betrayal of the highest order. 😭

and that's the whole page, bestie. this one came from the Debian crew, dated September 22, 1995 β€” which is genuinely ancient, like older than most of the internet, and yet here she is, still doing atomic semaphore operations like the queen she is. go forth and synchronize responsibly. πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from