09-Process_Synchronization_II_2spp

09-Process_Synchronization_II_2spp - 1 CSE 421/521 -...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 421/521 - Operating Systems Fall 2011 Tevfik Ko ar University at Buffalo September 26 th , 2011 Lecture - IX Process Synchronization - II 2 Roadmap Critical-Section Problem Solutions to Critical Section Different Implementations Semaphores Classic Problems of Synchronization Mutual Exclusion 3 ! 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 Mutual Exclusion 4 ! 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 5 ! 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 Mutual Exclusion 6 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 5 ! 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 Mutual Exclusion 6 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 7 !...
View Full Document

Page1 / 18

09-Process_Synchronization_II_2spp - 1 CSE 421/521 -...

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

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