Multithreading | Sololearn: Learn to code for FREE!



How to fix the code so that there is no deadlock? process INIT; common boolean C1. C2; common integer N; common real T1, T2; begin С1:=false; С2:=false; T1:= random; T2:= random; N:=1; start(P1); start(P2); end; process P1; Begin while true do begin BEFORE1; C1:=true; while C2 and (N<>1) do delay(T1); CS1; C1:=false; N:=2; AFTER1; end end; process P2; Begin while true do begin BEFORE2; C2:=true; while C1 and (N<>2) do delay(T2); CS2; C2:=false; N:=1; AFTER2; end end;

6/13/2020 8:49:14 AM


1 Answer

New Answer


In principle, I think both subprocesses should release all resources if they cannot immediately allocate all resources. Or at least release them after a limited number of attempts.