UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 3.7 Free with a free account
Cooperating Processes
Introduction to Operating Systems · Chapter 3: Concurrency, Synchronization, and Deadlock PreventionSection 3.7 · lesson 24 of 837 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: How Computer Memory Works →← TransactionsNext lesson: How Computer Memory Works →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
How separate processes share data through IPC, pipes and bounded buffers.
- Distinguish independent from cooperating processes and give reasons to cooperate
- Compare shared memory with message passing for interprocess communication
- Explain how a pipe created before fork connects a parent and a child
- Solve the bounded-buffer producer-consumer problem with two counting semaphores and a mutex
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 (947 words). Read along, or search it.
Opening Scene
Passing Dough Through a Hatch
In a bakery, one baker makes the dough and another bakes it. They work in different rooms, so they can't just reach for the same bowl. Someone has to pass the dough through a hatch in the wall. And the baker in the oven room must wait whenever the hatch is empty. Processes face the same puzzle. Each one has its own private memory, yet useful programs often work in teams. So how do they pass things along?
Definition
Independent Versus Cooperating Processes
An independent process neither affects, nor is affected by, other processes. A cooperating process can affect others, or be affected by them, usually by sharing data. Systems are built from cooperating processes for three reasons. Speed: a job split across several processes can use several cores. Modularity: one process per job, like a web server and a database. And convenience: you can edit a document, play music, and print, all at the same time.
Two IPC Models
Shared Memory and Message Passing
Since each process's memory is private, and protected by the OS, processes need a deliberate channel. It's called interprocess communication, or IPC. There are two basic kinds. With shared memory, the OS maps one region of memory into the address spaces of both processes. After that, they read and write it like any variable, with no help from the OS. It's fast, but they must coordinate themselves, with locks and semaphores, or races come back. With message passing, processes call send and receive, and the OS carries each message. It's slower, since every message passes through the kernel. But it's easier to get right, and it also works between different computers.
Code Walkthrough
A Pipe Between Parent and Child
The simplest message channel on Unix-like systems, Linux and macOS included, is the pipe: a one-way tube in kernel memory. Bytes written into one end come out of the other, in the same order. A pipe has two file descriptors, small numbers that stand for open files. In the array, slot zero is the end you read from, and slot one is the end you write to. This program creates the pipe first, then calls fork to make a child process. The child inherits both ends, so parent and child share one pipe. The child writes an order note and exits. The parent waits for it, then reads the note, and prints it. Each side closes the end it doesn't use. Build it, and run it. The parent got thirty-one bytes: the thirty characters of the note, plus the invisible zero that ends a C string. The child chose to send that too. A pipe moves bytes, not strings or numbers. The two sides must agree on what the bytes mean. A common mistake is to create the pipe after the fork. Then parent and child each make a pipe of their own, and never share one.
Everyday Example
The Shell's Vertical Bar
You've used pipes already. Type one command, a vertical bar, and another command, and the shell makes a pipe just like that one. Here, printf writes three bread names into the pipe, and sort reads them from the other end, and prints them in order.
Classic Problem
Producer, Consumer and a Bounded Shelf
The dough and the oven are the classic producer-consumer problem. The producer makes items, and the consumer uses them. Between them sits a bounded buffer: a shelf with a fixed number of slots. Three things must hold. The producer waits when the shelf is full. The consumer waits when it's empty. And they never change the shelf at the same moment.
Code Walkthrough
Two Semaphores and a Mutex
Two counting semaphores handle the waiting. Empty starts at the shelf size, and counts the free places. Full starts at zero, and counts the loaves waiting. As in lesson 3.2, they're named semaphores, made with sem open, because macOS doesn't support the unnamed kind. A mutex protects the shelf and its two index numbers. And the shelf is a ring: after the last slot, the index goes back to the first. The producer waits for a gap, puts a loaf in, and posts one more full slot. The consumer does the mirror image. One producer and one consumer pass eight loaves over a shelf of only three slots. The producer will often find the shelf full, and sleep. Run it. The consumer received one through eight, in order. With one producer, one consumer, and a shelf that keeps its order, the list is the same on every run, even though the timing isn't. Pipes work the same way. The kernel's buffer is bounded too, and a writer waits when it's full.
Quick Check
A One-Slot Shelf Keeps the Order
Quick check. Shrink the shelf to a single slot, and change nothing else. Now the producer can put down only one loaf before it has to wait. Pause and predict: what does the consumer receive? The same list: one through eight. Run it to check. A one-slot shelf only means more waiting: producer and consumer take strict turns. The semaphores still wake each side, so nothing hangs. And the ring keeps order, so loaves come out in the order they went in. A smaller buffer changes the timing, not the result.
Real Case
A Bounded Queue for Labels
Sam, who runs a small online bakery, had an orders process that came in bursts, and a labels process that fell behind. Ivy, who looks after the computers at a school, said: put a bounded queue between them. If the label printer jams, orders slow down, instead of eating all the memory. A shelf with a limit, Sam said, smiling.
Key Takeaways
Channels, Pipes and Bounded Buffers
To recap. Processes cooperate through IPC: shared memory is fast, and message passing is simpler. A pipe is a one-way stream of bytes, and it must exist before the fork. A bounded buffer makes the producer wait when it's full, and the consumer wait when it's empty. And two counting semaphores and a mutex are enough to build one.
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 Cooperating Processes
What is interprocess communication in an operating system?
Is shared memory faster than message passing?
Why must a pipe be created before calling fork?
How do semaphores solve the producer-consumer problem?
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 →