I have this requirement wherein I have to increment the value of a POSIX semaphore by more than 1.
Apparently, there is no way in POSIX specification to do this. There is no sem_setvalue() similar to sem_getvalue(). I do not want to go back to System V semaphores just because of this constraint.
Is there any alternative way to accomplish this? Or will I have to go the System V way?
I am programming in C on GNU/Linux.
Great thanks in advance.
No there is no such alternative when working with
sem_t
. If you have not yet done so, read thesem_overview
man page on linux. The calls that are listed there are all you can get: initialization to a specific value, increment and decrement by one.semctl
andsemop
are what you need.Use GETVAL SETVAL in smectl for getter and setter.Set sem_op in sembuf struct to what you want to do with the semaphore when using semop. See man for more.So what is your question really? How to implement something not supported by interface? Or how to create a structure behaving like semaphore using POSIX?
If this is later, before resorting to heavy guns like SysV, you can always use the
pthread_mutex_t
/pthread_cond_t
pair to implement pretty much any multi-threading synchronization primitive, semaphore included.E.g., untested:
If that is the complete specification, I think your teacher wants you to come up with a mechanism that will allow you to increment a semaphore by more than one atomically. So my guess is that one of your tasks is to synchronize the incrementation.