Process Synchronization using a flag

703 Views Asked by At

I am learning operating systems through an online course and I came across some software solutions for Process Synchronization. The teacher is explaining all software solutions starting from using a single turn variable upto Peterson's solution.

I have a doubt in the most basic approach. Please refer to the attached screenshot from the course video for clarity. The approach is to use a single turn variable in case of two processes and store 1 or 2 in turn depending on which process wants to access the critical section. This approach guarantees mutual exclusion but does not satisfy the progress requirement because if turn is 1 initially and P2 wants to enter the critical section first then it will be simply blocked waiting in the while loop even though P1 is not in the critical section. My idea is to initiate turn as -1 and now proceed. No process will be blocked depending on other then.

I have checked multiple online courses but no one is discussing this simple change and rather moving on to advanced solutions like Petersons algorithm or semaphores. Am I thinking right? Is my approach correct?enter image description here

1

There are 1 best solutions below

0
On

What you're describing here is called "strict alternation". Your proposal to modify the algorithm so that turn is initially -1 won't work. In your example, if turn is not equal to 2, then process 1 will not block. If turn is not equal to 1, then process 2 will not block. Initially, if turn is -1, then neither process 1 nor process 2 will block. As a result, both can execute their critical sections at the same time. You no longer have the mutual exclusion property.