Our professor gave us the following algorithm that's supposed to solve the critical section problem. I'm guessing the code is just pseudocode so I'm not trying to focus on the strange syntax and just trying to understand what it does.
Main
begin
process_number=1;
parbegin
process1;
process2;
parend;
end
Process1;
begin
while process_number=2 do;
critical_section1;
process_number=2;
other_stuff;
end;
Process2;
begin
while process_number=1 do;
critical_section2;
process_number=1;
other_stuff;
end;
After the code he says the following:
"Algorithm Guarantees mutual exclusion.
Price is high: P1 must go first, so if P2 is ready to enter its critical section, it must wait.
When P1 enters and leaves its CS, P2 must go next even P1 is ready, and P2 is not ready.
If one of the processes is terminated, then eventually the other will not be able to proceed"
I don't even know what's going on. My best guess is that since process 1 and process 2 are running concurrently, and the variable process_number is originally equal to 1, Process 1 is going to stop at the condition (while process_number = 2
). But for process 2 the condition (while process_number = 1
) is true, so...it will do
whatever lines of code follow it. But the lines of code that follow it are (critical_section2;
) and why in the world would it be running critical_section2 if it the variable process_number = 1
is true. I..honestly have no idea what my professor was trying to get across in this confusing code.