09-Process_Synchronization_II

09-Process_Synchronization_II - 1 CSE 421/521 - Operating...

Info iconThis preview shows pages 1–3. 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
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 7 ! 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 Mutual Exclusion 8 test lock, then set lock reset lock ! Implementation 2 simple lock variable " # suffers from the very flaw we want to avoid: a race condition # the problem comes from the small gap between testing that the lock is off and setting the lock while (lock); lock = TRUE; # it may happen that the other thread gets scheduled exactly in between these two actions (falls in the gap) # so they both find the lock off and then they both set it and enter bool lock = FALSE; void echo() { char chin, chout; do { chin = getchar(); chout = chin; putchar(chout); } while (...); } Mutual Exclusion...
View Full Document

Page1 / 6

09-Process_Synchronization_II - 1 CSE 421/521 - Operating...

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

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