Operating Systems · Operating Systems
Processes and the PCB
What a process is, the five states, the PCB, context switches, multiprogramming and how two processes exchange data.
Stay with Maya's editor. She clicks it once; the operating system creates a process we will call maya and stamps it PID 1420. This lesson is that one process: what the word means, the five states the kernel writes on her folder, how a context switch saves that folder, why more residents raise the degree of multiprogramming, and how maya talks to a different process without sharing memory by accident.
- Operating Systems
- Medium level
- 7 concepts
- 5 practice questions
1A process is a program that is running
Maya double-clicks her editor. The file on disk is just bytes — a program. The moment the operating system loads those bytes, gives them a number, and lets them use the CPU, that running instance is a process. We will call it maya. The kernel stamps it with a process identifier, written PID: maya is PID 1420. The same editor binary can be started twice; each start is a different process with its own PID.
A process is not the source file and not the window on screen. It is the OS's record of one execution: which instruction comes next, what is in the CPU registers, which files are open, and how much memory it owns. Until that record exists, the program cannot run.
Figure. The file on disk is the program. Loading it creates maya, PID 1420 — one process.
Program versus process
- ProgramThe editor file on disk. It does not have a PID and does not use the CPU.
- ProcessOne running copy. Maya's click created maya, PID 1420.
- Another clickA second process, new PID, same program file.
| Word | What it is for maya |
|---|---|
| Program | The editor file sitting on disk |
| Process | That file, loaded and running |
| PID | 1420 — the kernel's name for this run |
Maya starts the same editor a second time. What is created?
- A second process with a new PID
- A second copy of the program file on disk
- A new thread inside PID 1420
Each start is a new process. The file on disk stays one file. A thread is a later idea — still one process until the OS creates another PID.
2The five states
maya, PID 1420, does not stay in one condition. The kernel keeps a state for it: a short label that says what maya is allowed to do right now. Five labels cover the life of a process: New (just created, not yet eligible), Ready (could run, waiting for the CPU), Running (this core is executing maya's instructions), Waiting — also called Blocked — (maya asked for something slow, such as a disk read, and cannot use the CPU until it finishes), and Terminated (maya has exited; the record is being cleaned up).
Only one process per CPU core is Running at a time. If the laptop has one core and Spell is Running, maya cannot also be Running. She sits in Ready, or she is Waiting on a file.
Figure. The dashed Ready ← Running edge is the timer preemption. The solid Running → Waiting edge is a voluntary I/O block. Wakeup climbs Waiting → Ready; there is no arrow from Waiting into Running.
How a process moves
- NewThe kernel has created PID 1420 but has not yet put maya on the ready queue.
- Ready or RunningReady means maya could use the CPU. Running means this core is hers.
- Waiting or TerminatedWaiting is a voluntary pause for I/O. Terminated is the end of the run.
A running process issues a disk read and cannot continue until the read finishes. Which transition occurs?
- Running → Waiting (Blocked)
- Running → Ready
- Waiting → Running
The process blocks voluntarily for I/O, so it leaves Running for Waiting. Running → Ready is a preemption; Waiting → Running is illegal — wakeups land in Ready.
3How maya changes state
A state is a label. A transition is the event that changes the label. The kernel admits maya from New to Ready once she is in memory. The scheduler dispatches her from Ready to Running when it picks PID 1420. If her time slice — the timer quantum — expires while she is still runnable, she is preempted: Running back to Ready. If she issues a disk read and cannot continue, she blocks: Running to Waiting.
When the disk read finishes she does not jump onto the CPU. She goes Waiting to Ready, and waits her turn again. There is no direct Waiting → Running edge: a woken process must rejoin Ready first.
The state diagram already on the five-states concept is the map: Ready sits between Waiting and Running, so a wakeup always lands in Ready.
Edges maya actually takes
- Admit or dispatchNew → Ready when she is eligible; Ready → Running when the scheduler picks 1420.
- Preempt or blockTimer: Running → Ready. Disk read: Running → Waiting.
- WakeI/O done: Waiting → Ready — never Waiting → Running.
| What happened to maya | Transition |
|---|---|
| Admitted into memory | New → Ready |
| Scheduler picks PID 1420 | Ready → Running |
| Timer quantum expires | Running → Ready |
| Disk read, cannot continue | Running → Waiting |
| Disk read finishes | Waiting → Ready |
maya is Running and issues a disk read she must wait for. Which transition occurs?
- Running → Waiting (Blocked)
- Running → Ready
- Waiting → Running
She blocks voluntarily for I/O, so Running → Waiting. Running → Ready is a preemption. Waiting → Running is illegal — wakeups land in Ready.
4The PCB is maya's folder
While maya is not on the CPU, the kernel must remember enough to put her back. That memory is the Process Control Block — the PCB. Think of it as maya's folder: process state (Ready, Running, …), the program counter (which instruction is next), the CPU registers, the PID 1420, and scheduling info such as priority.
The PCB is not the program and not the window. It is the kernel's paper trail for one process. If the folder is lost, maya cannot be restarted from where she stopped.
The PCB table on this card is the folder: state, program counter, registers, PID 1420, scheduling info. The next concept shows that folder being saved and loaded.
What a switch does
- SaveWrite the running process's PC, registers and state into its PCB.
- PickThe scheduler chooses the next Ready process (or thread).
- RestoreLoad the chosen PCB into the CPU; if the address space changed, the TLB is also at risk.
| Field | Why it matters on a switch |
|---|---|
| Process state | Ready / Running / Waiting — decides eligibility |
| Program counter | Resume address after the switch |
| CPU registers | General-purpose state the process was mid-use |
| PID | Identity for signals, wait, and accounting |
| Scheduling info | Priority, quantum used, accounting ticks |
Which of these lives in maya's PCB?
- The program counter and PID 1420
- The editor source file on disk
- The disk's current cylinder
The PCB holds the live execution record — PC, registers, PID, state. The file on disk is the program. The disk cylinder is hardware, not maya's folder.
5A context switch saves one PCB and loads another
The CPU can run only one process at a time on one core. When the kernel takes the core from maya (PID 1420) and gives it to Spell, it must not lose maya's place. It writes maya's program counter and registers into her PCB, then reads Spell's PCB into the CPU. That save-and-restore is a context switch.
A context switch does no user work. Maya is not editing and Spell is not checking while the folders are copied. Extra switches are pure overhead. A later lesson will show that two threads of the same process can switch without changing the address space — cheaper, because the page tables stay put.
Figure. Save outgoing process A into its PCB, then load process B's PCB into the CPU. A same-process thread switch skips the address-space change that can flush the TLB.
Save, then load
- Save mayaWrite PID 1420's PC, registers and state into her PCB.
- Pick nextThe scheduler chooses Spell.
- Restore SpellLoad Spell's PCB onto the CPU. Spell runs; maya is Ready or Waiting.
Why is a context switch called pure overhead?
- No user process makes progress while the PCBs are saved and loaded
- The PCB is deleted during the switch
- Only Waiting processes ever switch
Save and restore are kernel bookkeeping. Maya and Spell both sit still until the incoming PCB is on the CPU.
6How many processes sit in memory
The degree of multiprogramming is a count: how many processes sit in main memory at once. If maya, Spell and Save are all resident, the degree is at least 3. The point of keeping more than one resident is simple: when maya waits on disk, the CPU can run Spell instead of sitting idle.
Raising the degree helps only while there is spare memory. Past a point the residents fight for frames — the fixed-size chunks of RAM a later lesson names — and the machine spends its time paging rather than running. That collapse is thrashing. Context-switch overhead sits on the same curve: more residents means more switches.
Figure. Degree of multiprogramming counts only processes resident in main memory. P4 waiting on disk is outside that count even if it is Ready in the long-term sense.
What the degree controls
- DefinitionCount the processes resident in main memory right now — that count is the degree.
- BenefitWhile one process waits on I/O, another Ready resident can run.
- CostToo high a degree invites thrashing and more context-switch overhead.
Degree of multiprogramming means
- The number of CPU cores
- The number of processes currently in main memory
- The number of threads inside one process
By definition it is how many processes are resident in memory. Cores and per-process thread counts are different quantities.
Notes
- Process states: New -> Ready -> Running -> Waiting(Blocked) -> Terminated; only one process per CPU core is in Running at a time.
- The PCB (Process Control Block) stores process state, program counter, registers, PID, and scheduling info; context switching saves/restores the PCB.
- Threads share the process's code, data and open files but have their own stack, registers and program counter; they enable concurrency with low overhead.
- User-level threads are managed in user space (fast, but one blocking call blocks all); kernel-level threads are scheduled by the OS (true parallelism).
- IPC mechanisms: shared memory (fast, needs synchronization) and message passing (send/receive, easier but slower); pipes and sockets are common implementations.
Formulas
- Context switch overhead is pure overhead — no useful work is done during a switch, so minimizing switches improves throughput.
- Degree of multiprogramming = number of processes currently in main memory.
- fork() returns 0 to the child and the child's PID (>0) to the parent; one call, two returns.
- n calls to fork() in sequence create 2^n total processes (including the original).
- A thread context switch is cheaper than a process switch because the address space (and TLB) need not change.
Exam traps & shortcuts
- A process moving Running -> Ready is a preemption (timer); Running -> Waiting is a voluntary block (I/O request).
- Shared memory = fast but you must handle race conditions; message passing = safe but slower.
- For fork() problems, count total processes as 2^n for n consecutive forks.
Reference tables
Every later OS lesson reuses these. The PID stays 1420.
| Word | Meaning for maya |
|---|---|
| Process | One running copy of the editor; PID 1420 |
| State | What 1420 is allowed to do right now |
| PCB | The kernel folder that survives a switch |
| Context switch | Save one PCB, load another |
| Degree of multiprogramming | How many processes sit in RAM |
| IPC | How two processes pass data |
Recap
maya is PID 1420. The folder is the PCB. Wakeups land in Ready.
- Process
- A program in execution. Same file, second click, new PID.
- States
- New, Ready, Running, Waiting, Terminated. One Running per core.
- Wakeup
- Waiting → Ready, never Waiting → Running.
- PCB
- State, PC, registers, PID, scheduling info.
- Switch
- Save outgoing PCB, load incoming. Pure overhead.
- IPC
- Shared memory is fast and racy. Messages keep spaces apart.
Practise Processes and the PCB
Reading is free and needs no account. Practice, mocks and progress live in the app.
- 5 exam-style questions on this topic, with explanations
- A 3-question practice set that ends the chapter
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device