sem_init… What is the value parameter for?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The value parameter in sem_init sets the semaphore's initial count. That count determines how many successful sem_wait operations can happen before callers start blocking.
The Meaning of the Initial Value
The function signature is:
The third argument, value, is the starting value of the semaphore. Think of it as the number of available permits.
- '
value = 1means one thread can pass immediately' - '
value = 3means three threads can pass before blocking' - '
value = 0means nobody passes until another thread callssem_post'
That is why semaphores are often described as counting semaphores.
Using value = 1 for Mutual Exclusion
A semaphore initialized to 1 behaves a lot like a lock for simple cases:
Because the initial count is 1, only one thread can enter the protected section at a time.
Using a Larger Value to Limit Concurrency
If the resource can safely be used by several threads at once, set a larger initial value.
For example, value = 3 allows up to three threads into the guarded region:
At most three workers run in the protected section at the same time because the semaphore starts with three permits.
Using value = 0 for Signaling
An initial value of zero is useful when one thread must wait for another thread to announce that work is ready.
Since the initial count is zero, sem_wait blocks until sem_post increments the semaphore.
How to Choose the Right Value
Pick the initial value based on what the semaphore models:
- a binary gate: use
1 - a pool of
Nidentical resources: useN - a "wait until signaled" event: use
0
The value is not arbitrary. It encodes the starting availability of the resource or event.
pshared Does Not Change the Meaning of value
The pshared argument controls whether the semaphore is shared between threads in one process or between processes. It does not change what value means. The count still represents the number of currently available permits.
Common Pitfalls
The most common mistake is assuming value is a maximum rather than an initial count. It sets the starting number of permits, and later sem_post or sem_wait calls change that number.
Another issue is using value = 1 automatically when the resource actually allows more concurrency. That can serialize work unnecessarily and hurt performance.
People also misuse value = 0 without arranging a matching sem_post. In that case, the waiter blocks forever because no thread ever signals readiness.
Summary
- The
valueparameter sets the semaphore's initial count. - '
1is common for mutual exclusion,Nfor a resource pool, and0for signaling.' - Each successful
sem_waitdecrements the count, and eachsem_postincrements it. - Choose the value based on how many permits should exist at startup.
- '
psharedchanges sharing scope, not the meaning of the count itself.'
Related reading
- Semaphore - What is the use of initial count?
- Semaphore thread throttling with async/await
- Semaphore vs. Monitors - what's the difference?
- send mail python asyncio
- Sending large amounts of HTTP requests concurrently with a small number of threads
- SendPingAsync Reply return 11050
- Separate Boost async read and async write in two threads
- Service vs IntentService in the Android platform
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.