CSC 394 · Operating Systems
Our notes
2 min readProcess
A process is a program in execution. A program is passive (a file on disk); a process is active, with:
- code (text), data, heap and stack;
- a program counter and register values;
- open files and other resources.
Process states
| State | Meaning |
|---|---|
| New | being created |
| Ready | waiting for the CPU |
| Running | instructions being executed |
| Waiting (blocked) | waiting for an event (I/O, a signal) |
| Terminated | finished |
Transitions: New → Ready (admitted); Ready → Running (scheduler dispatch); Running → Ready (interrupt, time slice used up); Running → Waiting (I/O request); Waiting → Ready (I/O done); Running → Terminated (exit).
Only one process per CPU core can be running at a time.
Process control block (PCB)
The OS keeps a PCB for each process: process ID, state, program counter, CPU registers, scheduling information (priority), memory-management information, I/O status (open files), accounting information.
Context switch
Switching the CPU from one process to another: save the state of the old process in its PCB, load the state of the new one. It is pure overhead (no useful work), so it must be fast.
Process creation and ending
A parent creates child processes, forming a tree. In UNIX: fork() makes a copy of the parent (returns 0 in the child, the child's PID in the parent); exec() replaces the child's program with a new one; wait() lets the parent wait for the child to finish. A child that has ended but not been "waited for" is a zombie; a child whose parent has died is an orphan (adopted by init/systemd).
Worked example. How many processes after this C code runs? fork(); fork(); fork(); Each fork doubles the count: 2³ = 8 processes (the original plus 7 new).
Threads
A thread is a unit of execution inside a process; threads of one process share code, data and open files but each has its own program counter, registers and stack.
Benefits: responsiveness (one thread waits while another works), resource sharing, cheaper to create and switch than processes, use of multiple cores.
- User-level threads: managed by a library; fast, but one blocking call can block all.
- Kernel-level threads: managed by the OS; can run on separate cores.
- Models: many-to-one, one-to-one (Linux, Windows), many-to-many.
Inter-process communication (IPC)
Processes that cooperate need to exchange data:
- Shared memory: a region both can read and write (fast, but needs synchronisation).
- Message passing: send() and receive() through the kernel (pipes, message queues, sockets); simpler for distributed systems.
- UNIX pipes: ls | grep txt sends the output of ls into grep.
- Signals: short notifications (SIGKILL, SIGINT from Ctrl+C).
Exams usually ask: program vs process; draw and explain the five-state diagram; contents of a PCB; context switching; fork() counting; process vs thread; shared memory vs message passing.
Common mistake: drawing a transition from Waiting straight to Running. A process that finishes waiting goes to Ready and must be scheduled again.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick OSTEP: The Abstraction: The Process
Free chapter PDF from the authors' site (104 KB).
(opens pages.cs.wisc.edu in a new tab)
-
OSTEP: Concurrency: An Introduction (threads)
Free chapter PDF from the authors' site (122 KB).
(opens pages.cs.wisc.edu in a new tab)
-
Introduction to Computer Science, 6.3 Processes and Concurrency
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 6.3 Processes and Concurrency, CC BY-NC-SA 4.0 · licence
-
Process (computing)
(opens en.wikipedia.org in a new tab)
Wikipedia, Process (computing), CC BY-SA 4.0 · licence
-
Thread (computing)
(opens en.wikipedia.org in a new tab)
Wikipedia, Thread (computing), CC BY-SA 4.0 · licence
Watch
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.