Semaphore wait () and signal ()

I experience process synchronization and have difficulty understanding the semaphore. So here are my doubts:

the source says that

"S semaphore is an integer variable that is accessed through standard atomic operations, that is, wait () and signal ().

He also provided a basic definition of wait ()

wait(Semaphore S) { while S<=0 ; //no operation S--; } 

Signal Definition ()

 signal(S) { S++; } 

Let the initial value of the semaphore be equal to 1 and say that there are two parallel processes P0 and P1, which should not simultaneously perform the operations of their critical partition.

Now let's say that P0 is in the critical section, so Semaphore S should be set to 0, now let's say that P1 wants to enter its critical section so that it executes wait (), and in wait () it continuously sang, now to exit the loop should increase the value of the semaphore, but this may not be possible, because according to the source, wait () is an atomic operation and cannot be interrupted, and therefore, the process P0 cannot cause signal () in a uniprocessor system.

I want to know this understanding, which I am still true or not. and if it is correct, what is the signal P0 of the process () to receive when the process P1 is hit during the cycle?

+6
source share
5 answers

I think this is an inaccuracy in your source. Atomic for the wait() operation means that each iteration of Atomic , which means that S-- is executed without interruption, but the whole operation is interrupted after each S-- completed inside the while .

+3
source

I do not think that saving an infinite while loop in a wait () operation is reasonable. I would go for an example of Stallings:

 void semWait(semaphore s){ s.count--; if(s.count<0) *place this process in s.queue and block this process } 
+1
source

When a task tries to get a semaphore that is not available, the semaphore puts the task in a wait queue and puts the task in mode . Then the processor can execute other code. the semaphore becomes available, one of the tasks in the wait queue wakes up, so that it can then receive the semaphore.

whereas S <= 0; // no operation This does not mean that the processor is working with this code. The process / task is blocked until it receives a semaphore.

0
source

I think the book means for an atomic operation, checking S<=0 both truth and S-- . Just like testAndset() , it is mentioned earlier.

if both separate operations S<=0 and S-- are atomic, but can be interrupted by another process, this method will not work.

imagine two processes p0 and p1 if p0 wants to enter the critical section and test S<=0 to be true. and it was interrupted by p1, and testing S<=0 also true. then both processes will go into the critical section. And this is wrong.

the actual non-atomic operation is inside the while loop, even if the while loop is empty, another process can still interrupt the current one when S<=0 checked as false, which allows another process to continue working in the critical section and release the lock.

however, I think that the code from the book cannot actually be used in the OS, since I donโ€™t know how to make the operations S<=0 true and S-- atomic. a more possible way to do this is to put S-- inside the while loop, as stated in SomeWittyUsername.

0
source

I think when process P1 is hit during the cycle, it will be in a wait state. The processor will switch between processes p0 and p1 (context switching), so priority goes to p0 and its call signal (), and then s will increase by 1 and p0 will exit the section, so that process P1 can enter the critical section and can avoid mutual exclusion

0
source

All Articles