I am dealing with unrelated processes synchronization. For unrelated processes, Named semaphores are necessary. We can't handle unrelated process with Unnamed Semaphore as mentioned here StackOverFlow. Now, if we want to use Named semaphore (counting semaphore) as a binary semaphore, then what is the procedure. I know that we can make Unnamed Semaphore to binary semaphore as mentioned here StackOverFlow . And if we want to synchronize threads by binary semaphores, then pthread_mutex are available. May you please suggest how to use named semaphore as binary semaphore or Is there any other methods to synchronize unrelated process as binary semaphores. Thanks in advance.
Asked
Active
Viewed 916 times
4
-
1It's a bit unclear what you're asking, but if I understand you correctly then the answer is simply "never increase the semaphore's value above one". You can achieve that by initializing the semaphore to value 1, and by ensuring that the value is only incremented by a thread that has previously decremented it successfully, with exactly one increment following each decrement. – John Bollinger Aug 22 '15 at 18:05
-
thanks, but can you elaborate it. Suppose I have multiple processes then to increment or decrement semaphore value I have to use common thread shared between all process. How it is implemented? – piyush-balwani Aug 22 '15 at 19:04
-
No, you are entirely missing the point of a semaphore. Any thread of any process can increment it or try to decrement it. It is thread-safe to do so, else the semaphore couldn't do its job. – John Bollinger Aug 23 '15 at 00:29
-
1st comment: "ensuring that the value is only incremented by a thread that has previously decremented" 2nd comment: "Any thread of any process can increment it or try to decrement it." I have multiple processes and they have to share. please explain – piyush-balwani Aug 24 '15 at 16:09
-
"Two processes can operate on the same named semaphore by passing the same name to sem_open(3)." (Linux manual page sem_overview(7)). Moreover, POSIX semaphores are thread-safe within each process. These characteristics are required for them to serve their purpose. – John Bollinger Aug 25 '15 at 00:45
-
This is not a tutorial site. If you want further assistance here then ask a specific question about a specific, concrete coding problem. – John Bollinger Aug 25 '15 at 00:47