Waitrwmx i 1 rwmxrelease implementing implementing

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: iving readers? Reader/Writer Reader/Writer Lock: Second Try SharedLock::AcquireWrite() { rwMx.Acquire(); while (i != 0) wCv.Wait(&rwMx); i = -1; rwMx.Release(); } SharedLock::AcquireRead() { rwMx.Acquire(); while (i < 0) rCv.Wait(&rwMx); i += 1; rwMx.Release(); } Implementing Implementing Spinlocks: First Cut class Lock { int held = 0; } void Lock::Acquire() { while (held); held = 1; } void Lock::Release() { held = 0; } “busy-wait” for lock holder to release Spinlocks: Spinlocks: What Went Wrong void Lock::Acquire() { while (held); held = 1; } void Lock::Release() { held = 0; } /* test */ /* set */ Implementing Implementing Locks: Another Try class Lock { } void Lock::Acquire() { disable interrupts; while (held); held = 1; enable interrupts } void Lock::Release() { … } /* test */ /* set */ Implementing Implementing Locks: Another Try (cont’d) class Lock { } void Lock::Acquire() { disable interrupts; while (held) { enable interrupts; disable interrupts; } held = 1; /* set */ enable interrupts; } void Lock::Release() { … } /* test */ The The Need for an Atomic “Toehold” To implement safe mutual exclusion, we need support for some sort of of “magic toehold” for synchronization. • The lock primitives themselves have critical sections to test and/or set the lock flags. • These primitives must somehow be made atomic. uninterruptible a sequence of instructions that executes “all or nothing” Synchronization Synchronization Hardware while (held); /* test */ held = 1; /* set */ Test and modify the content of a word atomically. boolean Test-and-Set (boolean &lock) { boolean TS; TS = lock; lock = true; return TS; } Mutual Mutual Exclusion with Test-and-Set Shared data: boolean lock = false; Process Pi while (Test-and-Set (lock)); //acquire critical section lock = false; remainder section //release Summarize Summarize Techniques for atomicity disable interrupts (software solution) test-set-lock Next Next Lecture Memory Management Read Chap. 3 MOS Project #1 due on Wednesday...
View Full Document

Ask a homework question - tutors are online