1 / 12

FOUNDATION TO PETERSON’S SOLUTION

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

caden
Download Presentation

FOUNDATION TO PETERSON’S SOLUTION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Slides BY :Lokesh(11CS10033) Ref:Class notes dated 12/02/14 FOUNDATION TO PETERSON’S SOLUTION

  2. 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

  3. 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?

  4. 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.

  5. Problem of Busy Waiting while (true){ while(lock!=0); lock = 1; //Critical Section lock=0; //Non Critical section }

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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];

  11. 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) }

  12. 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 .

More Related