180 likes | 515 Views
Slides BY : Lokesh (11CS10033) Ref:Class notes dated 12/02/14. FOUNDATION TO PETERSON’S SOLUTION. Disable Interrupt. After entering critical region , disable all interrupts Since Clock is just an interrupt , no CPU preemption can occur
E N D
Slides BY :Lokesh(11CS10033) Ref:Class notes dated 12/02/14 FOUNDATION TO PETERSON’S SOLUTION
Disable Interrupt • After entering critical region , disable all interrupts • Since Clock is just an interrupt , no CPU preemption can occur • Disabling interrupt is useful for OS but not for users
Disadvantages of Disabling Interrupt • Disabling the interrupt for a large amount of time is harmful for the system • It disconnects the system from the environment • Not suitable for multi processor environment • What if someone disables interrupt but does not enable it?
Mutual Exclusion with busy waiting • Lock Variable: • A Software based solution • A single , shared variable lock • Before entering the critical section programs test the variable if 0 , then enter critical section if 1 , the critical section is occupied.
Problem of Busy Waiting while (true){ while(lock!=0); lock = 1; //Critical Section lock=0; //Non Critical section }
Problems of busy waiting(ctd) • Consider the situation below • lock is a shared variable p1 p2 lock = 0 lock =1 At this moment, context switch Enters the CR occurs Enter the CR Context Switch Now both are in Critical Region No mutual inclusion
SPIN LOCK • So the busy waiting is not a solution of Critical section problem as it does not assure Mutual Exclusion • Busy waiting : continuously testing a variable until testing some value appears Spin Lock :A lock busy waiting is called spin lock while(lock!=0) CPU wastage time As the process is not switched from running to waiting state, it consumes CPU time
Mutual exclusion with Busy Waiting : strict alternation While(TRUE) { While(TRUE) { while(turn!=0); while(turn!=0); //CR //CR turn=1; turn=1; //Non CR //Non CR } } P0 P1 turn = 0 => turn of P0 to enter CR turn = 1 => turn of P1 to enter CR
Problem(NCR=Non Critical Region CR=Critical Region) • Suppose P0 enters Non critical section At this moment , turn = 1 P1 enters Non critical region after executing CR Now if time(NCR-P0) >> time(NCR-P1) then P1 completes NCR but P0 is still in NCR.As turn = 0, when P1 again tries to enter CR, it can’t. It can do so only after P0 is done with NCR =Does not guarantee progress
Peterson’s Solution • Assumptions: • Two processes solution i..e applicable for two processes only • Assume that LOAD and STORE instructions are atomic i..e they cannot be interpreted • Two processes share two variables int turn; int interested[2];
The Code #define FALSE 0 #define TRUE 1 #define N 2 int turn; int interested[2N]; void enter_region(int process){ int other; other = 1 – process; interested[process] = TRUE; turn = process; while(turn==process&&interested[other]==TRUE) }
Code (ctd) void leave_region(int process){ interested[process] = FALSE; } When both processes are interested , the code is such that the process which executed turn = process; first is executed first ->What if turn = process; while(turn==1-process && interested[other]=TRUE) This will simply change the order .