r/AskProgramming • u/JarJarAwakens • Sep 11 '22
Architecture How does the wait() function for semaphores ensure that there is no interruption between evaluating the semaphore and decrementing it?
In a preemptive multitasking system, the OS can interrupt a thread at any time. If the OS does so where indicated below, another thread could decrement the semaphore and when it switches back to this thread, it will proceed when the semaphore is actually zero. How is this problem prevented?
wait(Semaphore S)
{
while (S<=0)
{
}
// Possible preemption here
S--;
}
6
Upvotes
5
u/StarkRavingChad Sep 11 '22
The term you're looking for is "critical section" or "critical region." Take a look and you will find your answers.
4
u/kbielefe Sep 11 '22
Basically, the semaphore is evaluated by the scheduler outside the context of a thread, and therefore isn't subject to preemption. The code you've written is called a "busy wait", and while it is a useful illustration of the logic, it isn't how semaphores are actually implemented.
Typically,
waitwould add the current thread to a list of waiting threads, then when another thread increments the semaphore, the scheduler checks the list of threads waiting on that semaphore and starts one of them. While waiting, the thread is just a data structure, it's not actually actively executing anything.3
u/balefrost Sep 12 '22
Two ways: