So, this is a really simple question. I’m trying to use semaphores to prevent race conditions. I tried reading the man pages, but they are really confusing. Could someone provide a simple explanation how they work?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume you are using in Linux/Unix.
Semaphores are used to control/restrict the access to a shared resource (lets say a global variable) from multiple threads.
One Thread can take semaphore, modify the value and release it
If another thread tries to access the variable, it should acquire the semaphore, if its already aquired, it is pended and gains access after the previous thread relinquishes control.
This way semaphores are used for sequencing of operations and integrity of variables.
Semaphores are also used to signal events from one thread to another.
Mutex are variants of semaphore, where the same thread acquires and release it (to protect critical section or race conditions)
read more details below
https://www.sao.ru/hq/sts/linux/doc/ipc_guide/semaphores.html