operating system - Process synchronization - Critical section -
i studying final in os. on process sync chapter. our book offering following algorithm deal critical section. claims algorithm solves problem of starvation , offers bounded wait. here pseudocode
var flag: array [0..1] of boolean; turn: 0..1; repeat flag[i] := true; turn := j; while (flag[j] , turn=j) no-op; critical section flag[i] := false; remainder section until false;
from understanding, starvation occurs when process in critical section , process needs access it, first process won't finish. second process has wait forever. remember cpu scheduling implementing aging
in algorithm, example, solve starvation. don't see how happen here.
am wrong? there i'm not seeing here? thank you.
imagine have 2 processes: 1 , 2, process1 wants go cs: let's start first process:
repeat flag[1] := true; turn := 2;
i want explain "while" in code:
if falg[2] equal true: means the process2 has ran blow line of it's code
flag[2] := true;
but question: did process2 run next line? mean: did ran: "turn := 1;" ? if yes: means both of them want enter cs @ same time.
which 1 should go cs first?
if turn value equal number 2 means process1 ran "turn:=2" code after process2, because turn variable value process1 set means process2 ran earlier, it's process2 turn. n while process2 in cs nothing
while (flag[2] , turn=j) no-op; critical section flag[i] := false; remainder section until false;
process2 in csand process1 waiting, process2 false flag after finishing cs section. process1 go cs.
starvation happen in solution?
starvation resource management problem process not resources needs long time (maybe unlimited) because resources being allocated other processes. @ solution process2 cant resource again, because set's flag value 0, , should run "flag[2]=1" again enter cs, process1 in cs @ time, , process2 should wait getting cs.
what happens when 1 process don't finnish it's cs?
if process2 can't finish cs, process1 wait ever,but there no solution 1 when solution don't have starvation, don't mean situation. mean if both processes work correctly , don't have problem in there critical section code, starvation wont happen
how algorithm works?
there 2 people, want enter room, room cs.
both should there flag, tell want enter cs
but, 1 person can enter
both have 1 ticket, on ticket number has written. number id of person.
there packet has space ticket.
when person1 want enter, should put ticket there.
there ticket number "2"
@ time person2 come
must put ticket on packet, because there isn't space. put on first ticket
when @ packet, see second ticket
on second ticket number 1 has written
so, person1 can enter.
, cant enter again because person2 standing in front of door , when person1 came out flag false (after cs flag false) , while (for person2) stop can enter
if it's not clear tell me, explain more
Comments
Post a Comment