I. Preliminary ideas
Let the two processes achieve synchronization and mutex access to critical resources.
Pseudocode:
Turn .. 0 1p0while turn = 1 do {nothing} <critical section> turn: = 1p1while turn = 0 do {nothing} <critical section> turn: = 1
Problem:
When the value of turn is 0, The P0 process is before entering the critical section (outside the critical section). If a failure occurs, the execution of other processes will be affected. Similarly, when turn is 1, process P1 also affects the execution of process P0.
In addition, if another process fails in the critical section, the value of turn cannot be changed, and execution of other processes may be affected.
In short, the execution of other processes is affected regardless of whether the process is in or out of the critical section. In addition, they must be staggered for execution.
First improvement:
"Preliminary assumptions": Let's leave a question, as mentioned above. The reason is that the execution of one process depends on the execution of another process. To solve the problems left over from the preliminary idea, we use a flag to indicate whether the critical zone is in occupied state.
When flat [0] and flat [1] are true, they indicate P0 and P1 respectively. When they are false, they indicate P0 and P1 do not occupy the critical section. I personally define this idea as "array method" and "array method" for my understanding. (Of course there may be other better names ).
I have to mention that when I was learning Linux,