Lecture 12 Petersons Solution and Hardware Support 1

  • Slides: 14
Download presentation
Lecture 12: Peterson’s Solution and Hardware Support 1

Lecture 12: Peterson’s Solution and Hardware Support 1

Review: Mutual Exclusion • Critical region • Disabling interrupts • Strict alternation 2

Review: Mutual Exclusion • Critical region • Disabling interrupts • Strict alternation 2

In this lecture • Mutual exclusion solutions – Peterson’s solution – Hardware support 3

In this lecture • Mutual exclusion solutions – Peterson’s solution – Hardware support 3

General structure enter_region(); critical_region(); leave_region(); 4

General structure enter_region(); critical_region(); leave_region(); 4

Peterson’s Solution (1981) 5

Peterson’s Solution (1981) 5

Hardware Support: TSL • New Instruction: TSL R, Lock TSL = “Test and Set

Hardware Support: TSL • New Instruction: TSL R, Lock TSL = “Test and Set Lock” R = register, Lock = memory location • Atomically (atomic = nothing can interfere) - Read Lock into R - Store a non-zero value into Lock 6

Mutual Exclusion using TSL /* set lock to non-zero, proceed if it was 0

Mutual Exclusion using TSL /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ Jump enter_region /* try again */ return leave_region: Lock = 0 return 7

Alternative to TSL: XCHG /* set lock to non-zero, proceed if it was 0

Alternative to TSL: XCHG /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: Reg = 1 XCHG Reg, Lock /* swap contents of Reg and Lock */ If (Reg != 0) then /* not the first to set to zero */ Jump enter_region /* try again */ return leave_region: Lock = 0 return 8

Problem • Busy waiting • Possible solution: Yield to another thread if unable to

Problem • Busy waiting • Possible solution: Yield to another thread if unable to lock first time 9

Eliminate Busy Waiting /* set lock to non-zero, proceed if it was 0 earlier

Eliminate Busy Waiting /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ { thread_yield() ; /* let somebody else run */ Jump enter_region /* try again */ } return leave_region: Lock = 0 return 10

Summary of mutual exclusion solutions • Software solution – Disabling interrupts • Single processor

Summary of mutual exclusion solutions • Software solution – Disabling interrupts • Single processor only • Use in kernel mode – Strict alternation • Strict ordering • Busy waiting – Peterson’s solution • Busy waiting • Hardware solution – TSL/XCHG • Works on multiprocessors • Busy waiting 11

Avoiding Busy Waiting • If a process cannot enter critical region, the process calls

Avoiding Busy Waiting • If a process cannot enter critical region, the process calls sleep() to give up CPU. • A process wakes up another process using wakeup() 12

Review: Mutual Exclusion using TSL /* set lock to non-zero, proceed if it was

Review: Mutual Exclusion using TSL /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ Jump enter_region /* try again */ return leave_region: Lock = 0 return 13

TSL without busy waiting /* set lock to non-zero, proceed if it was 0

TSL without busy waiting /* set lock to non-zero, proceed if it was 0 earlier */ enter_region: TSL Reg, Lock /* set Lock to 1, copy old value of Lock into Reg*/ If (Reg != 0) then /* not the first to set to zero */ { sleep() Jump enter_region /* try again */ return leave_region: Lock = 0 wakeup(process_id) return 14