UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 3.4 Free with a free account
Mutual Exclusion
Introduction to Operating Systems · Chapter 3: Concurrency, Synchronization, and Deadlock PreventionSection 3.4 · lesson 21 of 836 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Deadlocks →← Interrupts and Atomic OperationsNext lesson: Deadlocks →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
The three requirements for a correct lock and Peterson's two-thread solution.
- Identify the entry, critical, exit, and remainder sections of a thread's loop
- Explain the mutual exclusion, progress, and bounded waiting requirements
- Show why strict alternation and a flags-only rule fail
- Explain how Peterson's solution meets all three requirements for two threads
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (878 words). Read along, or search it.
Opening Question
One Sign-Out Sheet at the Help Desk
Ivy, who looks after the computers at a school, keeps one sign-out sheet at the help desk for the loan laptops. Only one person may write on it at a time. That sounds easy, until you ask two more questions. What if the sheet sits free, and people still stand there waiting? And what if one busy student keeps cutting in front of everyone else? A good rule for one at a time has to handle those cases too. This lesson shows what a correct rule needs, and a famous way to get one.
Definitions
Entry, Critical, Exit, and Remainder
A critical section is the stretch of code where a thread uses shared data, data that other threads also change, like the load, add and store of a counter. Mutual exclusion means that while one thread is inside its critical section, no other thread is inside theirs. Each thread's loop has four parts. The entry section asks to go in. Then comes the critical section. The exit section says: I'm done. And the remainder section is everything else. The rules for taking turns live in the two parts around the critical section: the entry, and the exit.
Three Requirements
Exclusion, Progress, and Bounded Waiting
It's easy to write an entry rule that keeps threads apart. The hard part is doing it well. A correct solution must meet three requirements. Mutual exclusion: never two threads in the critical section at once. Without it, updates get lost. Progress: if nobody is inside and someone wants in, one of the threads that wants in gets in soon. A thread that isn't interested must not hold up the others. Bounded waiting: once a thread asks, only a limited number of others may go in ahead of it. Without it, one thread can be passed over again and again. That's called starvation. A common mistake is to think mutual exclusion alone makes a good lock. A lock nobody can ever get also keeps everyone apart!
Failed Attempt
Strict Alternation Breaks Progress
Here's a tempting rule that fails. Two threads share one variable, turn. A thread may enter only when turn holds its own number. On the way out, it hands the turn to the other thread. This strict alternation does guarantee mutual exclusion. But in this program, thread zero wants in three times, and thread one wants in only once. Each wait gives up after a hundred million checks, so the program can report instead of hanging forever. Build it, and run it. Thread zero got in twice, not three times. Its third turn could only come from thread one, and thread one had finished and left. The critical section sat empty, and thread zero waited for nothing. Strict alternation breaks progress.
Peterson's Solution
Flags Plus an After-You Turn
In 1981, the computer scientist Gary Peterson published a neat solution for two threads, using only shared variables. Each thread has a flag that says: I want in. And a shared turn variable breaks ties. The key move is generous. Before entering, a thread sets turn to the other thread, as if to say: after you. Then it waits only while the other one wants in, and it's the other one's turn. If both want in at once, the one that wrote turn last gives way, and the other goes first.
Checking the Rules
Meeting All Three Requirements
Now check it against the three requirements. Mutual exclusion: turn holds one value, so it can't favor both threads at once. Progress: if the other thread isn't interested, its flag is down, and the wait ends at once. Bounded waiting: a thread that leaves must write turn again before it can come back in, and that hands the waiting thread its chance. So a waiter is passed over at most once. Build it, and run it: two threads, fifty thousand additions each, and the total is exact.
Warning
Atomic Variables and Memory Order
One warning. The flags and turn here are atomic ints, and that matters. On a modern CPU and compiler, ordinary variables can be reordered, or kept in registers, so the two writes may not be seen in the order the code gives. Atomic types use the strongest ordering by default, which adds the memory barriers Peterson's reasoning needs. And Peterson's solution works for exactly two threads. Real programs use the library's mutex, which the OS and special CPU instructions make correct for any number of threads.
Quick Check
Two Raised Flags, Nobody Enters
Quick check. Here's a simpler entry rule, with no turn variable: raise your flag, then wait while the other thread's flag is up. To force the unlucky timing, both threads raise their flags before either one checks. Pause and predict: what happens? Neither one gets in. Each thread sees the other's flag up, and waits for it to drop. But each flag drops only after its owner gets in. Run it: both report zero. Without the give-up limit, they'd wait forever. Nobody is inside, both want in, and nobody gets in. That breaks progress. Mutual exclusion holds, which is why this rule looks safe at first. It's a small deadlock, and deadlock gets its own lesson, 3.5.
Key Takeaways
What a Correct Entry Rule Needs
To recap. A thread's loop has an entry, a critical section, an exit, and a remainder. The rules live in the entry and the exit. A correct rule needs mutual exclusion, progress, and bounded waiting. Strict alternation keeps threads apart, but breaks progress. And Peterson's solution meets all three for two threads, as long as its variables are atomic.
About Introduction to Operating Systems at UPI Study
Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
Where the credit goes
Universities that work with NCCRS
Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Mutual Exclusion
What are the three requirements for a solution to the critical section problem?
Why does strict alternation fail as a mutual exclusion solution?
How does Peterson's solution work?
Why is raising a flag and waiting not enough to protect a critical section?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Introduction to Operating Systems.
83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →