Processes, Explained | Introduction to Operating Systems

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

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

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

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.

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 Processes

What is the difference between a program and a process?
A program is a list of instructions sitting on a disk, while a process is a program that is running, with its own memory, stack, program counter, and open files. One program can become several processes at once, which is why opening the same spreadsheet twice can give two independent windows. Typing in one leaves the other unchanged.
What is stored in a process control block?
A process control block, or PCB, stores everything the operating system needs to pause a process and later resume it exactly where it stopped. That includes the process ID and state, the program counter and other registers, memory information, the list of open files, and scheduling details such as priority. The operating system keeps one PCB for each process.
What is the difference between the ready and waiting states of a process?
A ready process could run this very moment if a CPU were free, while a waiting, or blocked, process could not run even on an empty CPU. A waiting process is stuck until something slow arrives, such as a disk answer or a key press. When it does, the process moves back to ready, not straight to running.
What does fork return in the child and parent processes?
The fork call returns 0 in the child process and the child's process ID in the parent. After the call, both processes continue at the very next line with separate copies of memory, so the return value is how the code tells which process it is. A shell runs a command by forking, calling exec in the child, and waiting in the parent.

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 →