I am trying to use POSIX counting semaphore
as a binary semaphore
?
For the purpose, I have written the following code
sem = sem_open(argv[optind], flags, perms, 1); // Initialising semaphore to 1
while(sem_getvalue(sem) > 0)
{
continue;
}
sem_post(sem);
Are there any other methods to use a counting semaphore as a binary semaphore? Here if the comtext switching occurs immediately after the while lopp is evalauted to false, and yet sem_post hasn't been called, in such a situation wouldn't that leads to race condition? Is there any other better soultions, for what I am trying to achieve.
I have multiple processes being synchronized with the sempahore. I am aware this code does not let gurantee a scenario, when during sem_getvalue even if the sem
value becomes zero, even before sem_post in a particular process is called, anotehr process may also call sem_post, leading the value to be 2. How such a scenario can be resolved.
My problem won't be solved by a mutex, as in my problem, there are processes that is used only for the signal i.e. sem_post
operation, unlike in mutex, where all the processes will have wait and signal continually