sem_getvalue(3)

โœจ sem_getvalue(3) โœจ

okay bestie, welcome to the sem_getvalue page, straight out of the Unix manual for Library Functions ๐Ÿ’– it lives in the POSIX threads library (libpthread, and you link it with -lpthread) ๐Ÿงต


๐ŸŒธ NAME ๐ŸŒธ

sem_getvalue โ€” get the value of a semaphore ๐Ÿ‘€ like, you're just peeking at what number the semaphore is holding rn, no cap.


๐Ÿฆ‹ LIBRARY ๐Ÿฆ‹

POSIX threads library (libpthread, -lpthread) โ€” that's the girlies you need to link against, periodt.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <semaphore.h>

int sem_getvalue(sem_t *restrict sem, int *restrict sval);

๐Ÿ’… DESCRIPTION ๐Ÿ’…

Okay so here's the tea โ˜• sem_getvalue() takes the current value of the semaphore that sem points at, and drops it into the integer that sval points at. That's literally it. It's giving "read the vibe of the semaphore." โœจ

Now this next part is important, bestie, don't skip it ๐Ÿซถ if one or more processes or threads are blocked waiting to lock the semaphore with sem_wait(3), POSIX.1 is like "mmm I'll permit two possibilities for the value returned in sval":

  • either 0 is returned ๐Ÿ˜Œ
  • or a negative number whose absolute value is the count of the number of processes and threads currently blocked in sem_wait(3) ๐Ÿ˜ณ

And Linux? Linux adopts the former behavior. The first one. The 0 one. Slay, very decisive of it ๐Ÿ‘‘


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

  • sem_getvalue() returns 0 on success ๐Ÿ˜‡
  • on error, -1 is returned and errno is set to indicate the error ๐Ÿ˜ญ

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EINVAL โ€” sem is not a valid semaphore. and okay, dramatic pause, because the glibc implementation currently does not check whether sem is valid ๐Ÿ™ˆ like, it's not doing a vibe check on your semaphore at all. iconically unbothered.

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

for an explanation of the terms used in this section, go see attributes(7), bestie ๐Ÿ“–

  • Interface: sem_getvalue()
  • Attribute: Thread safety
  • Value: MT-Safe ๐ŸŒˆ

๐ŸŒŸ STANDARDS ๐ŸŒŸ

POSIX.1-2008 โญ


๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

POSIX.1-2001 ๐Ÿ’œ (a true elder, still thriving)


๐Ÿงš NOTES ๐Ÿงš

The value of the semaphore may already have changed by the time sem_getvalue() returns ๐Ÿ˜ญ like, by the time you read the number it's already a different number. it's giving "you took a selfie and the moment passed." no notes, just be aware ๐Ÿซถ


๐ŸŒˆ SEE ALSO ๐ŸŒˆ


and that's the whole page, bestie ๐ŸŽ€ this was Linux man-pages 6.20, dated 2026-02-08, and you just read sem_getvalue(3). go forth and read your semaphores responsibly ๐Ÿ’–โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms