09_Process_Synchronization_II_2spp

09_Process_Synchronization_II_2spp - CSC 4103 Operating...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 4103 - Operating Systems Fall 2009 Tevfik Ko ! ar Louisiana State University September 22 nd , 2009 Lecture - IX Process Synchronization - II 2 Roadmap • Solutions for Critical-Section Problem • Semaphores • Classic Problems of Synchronization Bounded Buffer Readers-Writers Dining Philosophers Sleeping Barber
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Mutual Exclusion 3 critical region 1. thread A reaches the gate to the critical region (CR) before B 2. thread A enters CR first, preventing B from entering (B is waiting or is blocked) 3. thread A exits CR; thread B can now enter 4. thread B enters CR ! Desired effect: mutual exclusion from the critical region B A B A B A B A HOW is this achieved?? Mutual Exclusion 4 ! Implementation 1 — disabling hardware interrupts critical region B 1. thread A reaches the gate to the critical region (CR) before B 2. as soon as A enters CR, it disables all interrupts, thus B cannot be scheduled 3. as soon as A exits CR, it enables interrupts; B can be scheduled again 4. thread B enters CR B A B A A B A
Background image of page 2
Mutual Exclusion 5 ! Implementation 1 — disabling hardware interrupts " # it works, but not reasonable! # what guarantees that the user process is going to ever exit the critical region? # meanwhile, the CPU cannot interleave any other task, even unrelated to this race condition # the critical region becomes one physically indivisible block, not logically # also, this is not working in multi- processors disable hardware interrupts enable hardware interrupts void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (. ..); } Mutual Exclusion 6 ! Implementation 2 — simple lock variable critical region 1. thread A reaches CR and finds a lock at 0, which means that A can enter 2. thread A sets the lock to 1 and enters CR, which prevents B from entering 3. thread A exits CR and resets lock to 0; thread B can now enter 4. thread B sets the lock to 1 and enters CR B A B A B A B A
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Mutual Exclusion 7 test lock, then set lock reset lock ! Implementation 2 — simple lock variable # the “lock” is a shared variable # entering the critical region means testing and then setting the lock # exiting means resetting the lock bool lock = FALSE; void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (. ..); } while (lock); /* do nothing: loop */ lock = TRUE; lock = FALSE; Mutual Exclusion 8 ! Implementation 2 — simple lock variable " 1. thread A reaches CR and finds a lock at 0, which means that A can enter 1.1 but before A can set the lock to 1, B reaches CR and finds the lock is 0, too 1.2 A sets the lock to 1 and enters CR but cannot prevent the fact that . . . 1.3 . . . B is going to set the lock to 1 and enter CR, too critical region B A B A B A B A
Background image of page 4
Mutual Exclusion 9 then set lock reset lock !
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

09_Process_Synchronization_II_2spp - CSC 4103 Operating...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online