Binary Semaphores (Locks)
This lesson talks about a special kind of semaphores, the binary semaphores.
We'll cover the following...
We are now ready to use a semaphore. Our first use will be one with which we are already familiar: using a semaphore as a lock. Look at the code snippet below.
Press + to interact
sem_t m;sem_init(&m, 0, X); // initialize to X; what should X be?sem_wait(&m);// critical section heresem_post(&m);
Therein, you can see that we simply surround the critical section of interest with a sem_wait()
/sem_post()
pair. Critical to making this work, though, is the initial value of the semaphore m
(initialized to X
in the figure). What should X
be?
… (Try thinking about ...
Access this course and 1400+ top-rated courses and projects.