I'm trying to learn about mutex, semaphores and critical sections and I'm uncertain about some things with semaphores. Is a semaphore the same as a critical section? The definition of how to use semaphores from semaphore.h states the use is:
sem_t m;
sem_init(&m, 0, X); // initialize semaphore to X; what should X be?
sem_wait(&m);
// critical section here
sem_post(&m);
So my question is really is the "// critical section here" actually a critical section?
Semaphores are tools used to protect critical sections: to insure that only one CS is being executed at a time.
In you example, the first process to execute sem_wait(&m)
gets to execute its copy of the critical section; any other process that tries to execute its correspoinding sem_wait
will be blocked, until out first process finishes its CS by executing sem_post
. AT that point, some other call to sem_wait
will return, starting the process again.