UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 3.1 Free with a free account
Coordinating Threads
Introduction to Operating Systems · Chapter 3: Concurrency, Synchronization, and Deadlock PreventionSection 3.1 · lesson 18 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: Synchronization Tools →← ThreadsNext lesson: Synchronization Tools →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
Why threads sharing data need rules, and which tool fits which problem.
- Explain why the scheduler's interleaving makes thread coordination necessary
- Distinguish shared data from private data within a process
- Identify race conditions, deadlocks, and ordering problems and the tool for each
- Decide whether a given program needs a lock
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 (887 words). Read along, or search it.
Opening Question
Two Bakers, One Mixing Bowl
Two bakers reach for the same mixing bowl at the same moment. One pours in flour. The other already has eggs in her hands, and the batter is ruined. Nobody was careless. They simply had no rule for taking turns. Threads inside a program meet this problem thousands of times a second. So who supplies the rules, and what do the rules look like?
Why Rules Matter
The Scheduler Ignores What Threads Do
One baker never has this problem. Two bakers sharing one kitchen do. In a computer, the bakers are threads, and the kitchen is the process they live in. The CPU scheduler, from lesson 2.6, decides which thread runs and when. With several cores, two threads can even run at the very same instant. And the scheduler works for speed, not for politeness. It may pause a thread in the middle of a job, and it has no idea what the thread is doing with its data. So coordination means the rules, supplied by the operating system and the programmer, that make several threads work together safely, and in a sensible order.
Timing Bugs
Right on Monday, Wrong on Tuesday
Without those rules, a program can give the right answer on Monday and a wrong one on Tuesday, with no change at all to the code. Programmers have a nickname for a bug that vanishes when you try to watch it, because watching changes the timing. It's called a heisenbug, after the physicist Heisenberg. Timing bugs between threads are the classic cause.
Shared Versus Private
Globals Shared, Locals Private
Not everything threads touch is dangerous. Each thread keeps its own local variables on its own stack, and nobody else sees them. That's private data. Everything else in the process is shared data: global variables, memory on the heap, and open files. There's one copy, and every thread sees it. A small C test on a Mac shows the difference. Two threads each note where they found a global variable, and where they found a local one. The two notes for the global match: one shared copy. The two notes for the local differ: each thread has its own. Risk lives only in shared data that somebody changes. Data that's only read, or that's private, needs no rules.
Common Mistake
One Small Variable Is Enough
A common mistake is to think one little variable can't hurt. Size doesn't matter. A single shared integer that two threads update is enough to lose data, and lesson 3.3 shows it happening with a real counter.
Interleaving
Same Program, Different Orders
Because the scheduler can switch threads at almost any moment, the same program can run in different orders. Say two threads each take three steps. In run one, thread A finishes all three before B starts. In run two, the scheduler cuts in, and the steps interleave: A, B, A, B. Neither run is a bug by itself. It becomes a bug when the answer depends on which run you got.
Real Example
A Crash Once a Week
Sam, who runs a small online bakery, had a shop program that crashed about once a week, with no pattern. Ivy, who looks after the computers at a school, read the code. Two threads update the stock count, she said. Most days one finishes before the other starts, so nobody notices. So the bug only shows up when the timing is unlucky? Right, said Ivy. Those are the worst kind. Let's add a lock.
Three Troubles
Races, Deadlocks, and Ordering
Three kinds of trouble follow from all this, and each one has its own tool. A race condition is when the result depends on the exact timing of threads, as they touch shared data. The tool is a lock, or an atomic action. A deadlock is when threads wait for each other forever, each holding something the other needs. The tools are the rules in lesson 3.5: prevent it, avoid it, or detect it. And ordering is when one thread must wait for another to finish a step first, like: don't open the shop before the cake is baked. The tool for that is a semaphore, or a condition variable.
Quick Check
Which Program Needs a Lock
Quick check. Here are three small programs, and only one of them needs a lock. In the first, four threads only read one shared price list. In the second, two threads each count visitors into their own local tally, and main adds the tallies after joining them. In the third, one thread rewrites the price list while the others read it. Pause, and decide. The third one, C. Only one thread writes, but one writer is enough. A reader can catch the list half rewritten, with some prices old and some new. In A, the price list is shared, but nobody changes it, and reading can't spoil a value. And in B, the tallies are private. No two threads touch the same data while they run, so no rule is needed.
Key Takeaways
Shared, Changing Data Is the Risk
To recap. Threads share one process, and the scheduler can interleave them at any moment. The risk is shared data that someone changes. Private data and read-only data are safe. A timing-dependent result is a bug, even when most runs look fine. And each trouble has its tool: a lock or atomic for races, the deadlock rules, and semaphores or condition variables for order.
Try It
Coordination Rules at Home
Something to try. List three things at home that two people might use at once, like a bathroom, a charger, or a pan. For each, write down the rule you already use, such as a closed door, or a my-turn rule. You've just designed coordination.
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 Coordinating Threads
What is a heisenbug?
Which data is shared between threads and which is private?
Does shared data need a lock if only one thread writes to it?
What kinds of problems can happen when threads share data?
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 →