Docsity

Sync: Mutual Exclusion with Interrupts, Locks, Turns, and Peterson's, Study notes of Operating Systems

Partial preview of the text

Download Sync: Mutual Exclusion with Interrupts, Locks, Turns, and Peterson's and more Study notes Operating Systems in PDF only on Docsity!

PROCESS SYNCHRONIZATION

How to implement Mutual

Exclusion

  • (^) Disabling interrupts
  • (^) Lock Variables
  • (^) Turn Variables
  • (^) Peterson solution

Idea No.1: Disabling Interrupts

  • (^) This approach is generally unattractive

because

  • (^) it is unwise to give user processes the power to turn off interrupts.
  • (^) Suppose an user process given all the controls and it is not progressing…….
  • (^) It will block the whole system.

Idea No.2: Lock Variables

  • (^) A single shared Lock variable is used with following status: - (^) Status 0 = The lock is free and the process is not in critical section. - (^) Status 1= The lock is not free and the process is in critical section.

Drawback with Lock Variables

  • (^) Error as same as Spooler Directory.
    • (^) Process 1 reads the lock = 0.
    • (^) Process 1 is going to set lock =
    • (^) On the Same time Process 2 read lock =.
    • (^) Process 2 set the lock= 1.
    • (^) Process runs again, o 1 , and two processes will be in their critical regions at the same time.

Idea No. 3 : Turn Variable.

  • (^) Turn is a Shared Variable.
  • (^) Initially turn=
  • (^) P0 Wants to enter in the CR found turn =0.
  • (^) Enter into CR.
  • (^) P1 Also waiting for its turn.
  • (^) Checking again n again
  • (^) It is Busy Waiting.

Idea no. 4 :Peterson's solution

for achieving mutual exclusion.

  • (^) To calls are introduced.
    • (^) Enter Region (For showing interest of CR)
    • (^) Leave Region ( For Leaving the CR.)

Idea no. 4 :Peterson's solution

for achieving mutual exclusion.

  • (^) STEP 1: Enter_region is call for all interested process for Critical Region.
  • (^) STEP 2: Before using the shared variables each process calls enter_region with its own process number, 0 or 1, as the parameter.
  • (^) STEP 3: This call will cause it to wait, if need be, until it is safe to enter.
  • (^) STEP 4: After it has finished with the shared variables, the process calls leave_region to indicate that it is done and to allow the other process to enter, if it so desires.

Idea no. 4 :Peterson's solution

for achieving mutual exclusion.

  • (^) Step 4: If process 1 now calls enter_region, it will hang there until interested [0] goes to FALSE ,
  • (^) Step 5: an event that only happens when process 0 calls leave_region to exit the critical region.

Idea no. 4 :Peterson's solution

for achieving mutual exclusion.

  1. Now consider the case that both processes call enter_region almost simultaneously.
  2. Both will store their process number in turn.
  3. Whichever store is done last is the one that counts; the first one is lost.
  4. Suppose that process 1 stores last, so turn is 1.
  5. So it creates conflicts.

Idea no. 4 :Peterson's solution

for achieving mutual exclusion.

while (turn == process && interested[other] == TRUE) /* null statement /; } void leave_region(int process) / process: who is leaving / { interested[process] = FALSE; / indicate departure from critical region */ }

Idea no 5: Test and Set Lock

  • (^) Hardware based approach….
  • (^) Use an Instruction
  • (^) TSL RX, LOCK
  • (^) TSL= Test and Set Lock
  • (^) RX= Register
  • (^) Set indicates read operation.
  • (^) Lock = memory word ( a small memory portion)

The TSL Instruction

  • (^) To use the TSL instruction, we will use a shared variable, LOCK , to coordinate access to shared memory.
  • (^) If LOCK = 0, any process may set it to 1 using the TSL instruction and then read or write the shared memory.
  • (^) When it is done, the process sets LOCK back to 0 using an ordinary move instruction.

The TSL Instruction