Fall 2000M.B. Ibáñez Lecture 08 High Level mechanisms for process synchronization Critical Regions Monitors
Fall 2000M.B. Ibáñez Critical Regions Language constructions var v: shared T v will be shared among many processes region v when B do S v can be access only inside a region-statement while S is executed, no other process can access v B is a boolean expression that governs the access to the critical region. When a process tries to enter the critical-section region, B is evaluated: –True => S is executed –False => the process is blocked waiting for B
Fall 2000M.B. Ibáñez Bounded-buffer with Critical Regions I var buffer: shared record pool: array[0..n-1] of item; count, in, out : integer; end;
Fall 2000M.B. Ibáñez Bounded-buffer with Critical Regions II The producer executes: region buffer when count < n do { pool[in] := nextp; in := in + 1 mod n; count := count + 1; } The consumer executes: region buffer when count > 0 do { nextc := pool[out]; out := out + 1 mod n; count := count - 1; }
Fall 2000M.B. Ibáñez Deadlock risk var v: shared T1; var w: shared T2; region v do region w do {…}; region w do region v do {…};
Fall 2000M.B. Ibáñez Monitor: Abstract Data Object I type monitor-name = monitor ; procedure entry P1(…) {…}; procedure entry P2(…) {…};... procedure entry PN(…) {…}; { }
Fall 2000M.B. Ibáñez Monitor: Abstract Data Object II Only one process at a time can be active within the monitor. A procedure defined within a monitor can access: –Its formal parameters –Its local variables –The monitor variables The local variables of a monitor can be accessed by only the local procedures
Fall 2000M.B. Ibáñez Condition Variables var x,y: condition; The process invoking “x.wait;” is suspended until another process invokes “x.signal;” The “x.signal;” resumes exactly one suspended process If no process is suspended, then the signal operations has no effect.
Fall 2000M.B. Ibáñez Example: Resource Assignation I type resource-assignation = monitor var busy: boolean; var free: condition; procedure entry reserve(){...} procedure entry release(){…} { busy := false; }
Fall 2000M.B. Ibáñez Example: Resource Assignation II procedure reserve() { if busy then free.wait; busy := true; } procedure release() { busy := false; free.signal; }; process use_resource(){ resource- assignation.reserve(); using_resource(); resource- assignation.release(); }
Fall 2000M.B. Ibáñez A monitor solution to the dining- philosopher problem I type dining-philosophers = monitor var state: array[0..4] of {thinking, hungry, eating}; var self: array[0..4] of condition; procedure entry pickup(i: 0..4);{…} procedure entry putdown(i: 0..4);{…} procedure entry test(k: 0..4);{…} { for i := 0 to 4 do state[i] := thinking; }
Fall 2000M.B. Ibáñez A monitor solution to the dining- philosopher problem II procedure entry pickup(i: 0..4); { state[i] := hungry; test(i); if state[i] != eating then self[i].wait; } procedure entry putdown(i: 0..4); { state[i] := thinking; test(i+4 mod 5); test(i+1 mod 5); }
Fall 2000M.B. Ibáñez A monitor solution to the dining- philosopher problem III procedure test(k:0..4); { if state[k+4 mod 5] != eating and state[k] = hungry and state[k+1 mod 5] != eating then { state[k] := eating; self[k].signal; }