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
0is 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()returns0on success ๐- on error,
-1is returned anderrnois set to indicate the error ๐ญ
๐ ERRORS ๐
EINVALโsemis not a valid semaphore. and okay, dramatic pause, because the glibc implementation currently does not check whethersemis 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
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms