UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 2.2 Free with a free account
Processes
Introduction to Operating Systems · Chapter 2: Process Management: Scheduling, Multitasking, and System CallsSection 2.2 · lesson 11 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: System Calls →← MultiprocessingNext lesson: System Calls →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
What a process is, its five states, and fork, exec, and wait.
- Distinguish a program from a process and list a process's private resources
- Describe what a process control block stores and why
- Trace a process through the new, ready, running, waiting, and terminated states
- Explain how fork, exec, and waitpid create and manage processes
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 (1,040 words). Read along, or search it.
Opening Question
One Program, Two Windows
Sam, who runs a small online bakery, double-clicks the orders spreadsheet. A second later, Sam double-clicks it again by accident. There's one file on the disk, yet two windows now sit on the screen. And typing in one doesn't change the other. How can one program be in two places at once? Because what runs isn't the program at all. It's something the operating system builds around the program, called a process.
Definition
A Program in Motion
A program is a list of instructions sitting on a disk, like a recipe in a cookbook. By itself, it does nothing. A process is a program that's running: the recipe being followed right now, with real flour on the counter and a timer ticking. Sam's two windows are two processes made from one program, just as two bakers can follow one recipe, each with their own bowl.
Private Pieces
Memory, Stack, Counter, and Open Files
To keep a process going, the operating system gives it its own private pieces. Memory for its code and its variables, plus a stack: the workspace for function calls. The CPU's current place in its code, called the program counter. And a list of the files it has open. One process can't peek into another's memory. That's the protection job from lesson 1.1.
Process Control Block
A Notebook Page for Each Process
The operating system may be juggling hundreds of processes, so it keeps a notebook page for each one: the process control block, or PCB. It holds the process ID and its state, the program counter and the other registers, memory information, the open files, and scheduling details such as priority. That's everything needed to pause the process, and later bring it back exactly where it stopped, as if nothing happened.
Five States
New, Ready, Running, Waiting, Terminated
A process isn't always running. The operating system tracks which state each one is in. A newly made process is new. Once it's set up and able to run, it's ready, which means all I need is a CPU. Running means it's on a CPU right now. Only one process per core can be running at a time. If a running process asks for something slow, like a file or a key press, it becomes waiting, also called blocked. When the answer arrives, it moves back to ready, not straight to running. And when it ends, it's terminated. A running process can also go back to ready when its turn is over. Lesson 2.4 shows who calls time.
Common Mistake
Ready Is Not Waiting
Don't mix up ready and waiting. A ready process could use a CPU this very moment, if one were free. A waiting process couldn't run even on an empty CPU, because the thing it needs, a disk answer or a key press, hasn't arrived yet.
Creating Processes
Fork Copies the Calling Process
On Unix-like systems such as Linux and macOS, a new process is made with a system call named fork. Lesson 2.3 explains system calls. fork makes a child process, a near-exact copy of the caller, which is called the parent. After the call, both continue at the very next line. The only difference is what fork returns. The child gets zero, and the parent gets the child's process ID. That's how one piece of code can tell which process it is.
Worked Example
Ten Loaves, Two Separate Memories
This program shows the copy is real. The parent starts with ten loaves, then forks. The child sells four, and prints its count. The parent, meanwhile, calls waitpid, which puts it to sleep until the child has ended. Build it and run it. The child's line comes first, with six loaves. Then the parent wakes up, reports exit code zero, and its own copy still says ten. The child changed only its own copy of the variable. Two processes, two separate memories.
Details
Waiting, Zombies, and Flushing Output
Two details make that output dependable. Because the parent waits, the child's line always prints before the parent's last two lines. Without the wait, the order would depend on timing, and could change from run to run. Waiting also tidies up. A child that has ended but hasn't been waited for lingers as a zombie: an entry left behind in the process table. And the fflush line empties the print buffer before the fork, so the copy can't print the first line a second time.
Replacing Programs
Exec Swaps in New Code
Copying a whole program just to run a different one sounds wasteful, so there's a second call: exec. exec replaces the program inside the current process with a new one. It's the same process, but the new code starts from its first line. Here, the child becomes the echo program. The usual recipe is fork, then exec in the child, then wait in the parent. Run it. Echo prints its words and exits with code zero, and the parent's waitpid collects that code. Notice what the child never does: it never reaches the perror line. If exec works, the old code is gone. So when does that line ever run? Only when exec fails. Misspell the program's name, and run it again. Now exec returns, perror explains why, and the child returns one. The parent's waitpid reports exit code one. Put the name back, and the program works as before.
Real Example
How the Shell Runs a Command
This recipe is exactly what your shell does every time you type a command, such as l s. The shell forks a copy of itself. The copy turns into l s with exec. And the shell waits until it's done. Then it shows you the prompt again, ready for the next command.
Quick Check
Two Forks, Four Prints
Quick check, with a new program. It calls fork, then calls fork again, then prints the word bake. Every process then waits for its own children, so nothing gets cut off. How many lines say bake? Pause, and decide. Four lines. The first fork turns one process into two. Both of them run the second fork, so two become four. All four reach the printf, and each one prints once. Three is the tempting guess: one process, plus one for each fork. But the second fork runs in both copies, not just the parent.
Key Takeaways
Programs in Motion and Their Life Cycle
To recap. A process is a program in motion, with its own memory, program counter and open files. The process control block holds what's needed to pause a process and resume it exactly. A process moves between new, ready, running, waiting and terminated. And fork copies a process, exec replaces its program, and waitpid waits for a child to end.
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 Processes
What is the difference between a program and a process?
What is stored in a process control block?
What is the difference between the ready and waiting states of a process?
What does fork return in the child and parent processes?
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 →