Cooperating Processes, Explained

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

← 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 →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

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.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
Interprocess communication, or IPC, is a deliberate channel that lets processes exchange data even though each has private, protected memory. The two basic models are shared memory and message passing. With shared memory, the OS maps one region into both address spaces. With message passing, processes call send and receive while the kernel carries each message. Pipes, sockets and message queues are common examples.
Is shared memory faster than message passing?
Yes, shared memory is usually faster, because once the region is mapped, processes read and write it like ordinary variables without the kernel's help. Message passing sends every message through the kernel, which adds cost. The tradeoff is safety and reach: shared memory needs locks or semaphores to avoid race conditions, while message passing is easier to get right and also works between different computers.
Why must a pipe be created before calling fork?
A pipe must exist before fork so that the child inherits the same two file descriptors as the parent. The pipe call fills a two-slot array, where slot zero is the read end and slot one is the write end. If the pipe is created after the fork, the parent and child each make a separate pipe, and no data ever passes between them.
How do semaphores solve the producer-consumer problem?
Two counting semaphores and a mutex solve the bounded-buffer problem. A semaphore called empty starts at the buffer size and counts free slots, while full starts at zero and counts filled slots. The producer waits on empty, adds an item while holding the mutex, then posts full. The consumer does the mirror image, so the producer sleeps when the buffer is full and the consumer sleeps when it is empty.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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

Introduction to Operating Systems
3 credits · NCCRS
Enroll →