Process concept, states and the PCB
Process concept, states and the PCB
Recorded by Dr. Krithika L.B. Slot m2-process-states
What a process is, how it is laid out in memory, the five states it moves through, the PCB that represents it, and how the kernel switches between processes.
By the end of this session you should be able to:
Module 1 ended with systemd starting as PID 1. From that moment, everything the kernel manages is a process.
A program is a passive file on disk (an executable such as /usr/bin/gcc). A process is a
program in execution: it has a program counter, registers, open files, memory and a state. One program can give rise to
many processes — ten students running gcc on the same server create ten processes from one file.
A process is the unit of work in a modern OS: a program in execution together with its current activity — program counter, CPU registers, stack, data, heap and the resources the kernel has given it.
Each process gets its own address space, laid out in four regions:
malloc() / new memory (grows ↑)#include <stdio.h>
#include <stdlib.h>
int counter = 0; /* data section */
int main(void) /* main's code: text */
{
int local = 5; /* stack */
int *p = malloc(sizeof(int)); /* p on stack, *p on heap */
printf("text %p\ndata %p\nheap %p\nstack %p\n",
(void *)main, (void *)&counter, (void *)p, (void *)&local);
free(p);
return 0;
}Run it and you will see the addresses increase from text → data → heap, with the stack far above — exactly the picture.
As a process executes it changes state. The classic five-state model:
New — the process is being created: the kernel allocates a PCB and an address space and loads the program.
New → Ready (admitted). The process has everything it needs except the CPU. It waits in the ready queue.
Ready → Running (scheduler dispatch). The CPU scheduler picks it; the dispatcher loads its registers. On one core only one process is Running at a time.
Running → Ready (interrupt). The timer interrupt fires (time slice over) or a higher-priority process arrives. It goes back to the ready queue — it can still run.
Running → Waiting (I/O or event wait). The process asked for a disk read or called wait(); it cannot use the CPU until the event happens.
Waiting → Ready (I/O or event completion). The device interrupt (Session 1.4) wakes it. Note: it goes to Ready, not straight back to Running.
Running → Terminated (exit). The process calls exit() or is killed. Its resources are freed; the PCB stays until the parent collects the exit status.
There is no Waiting → Running transition and no Ready → Waiting transition. A process can only start waiting while it is running (it must execute the instruction that requests I/O), and when the wait ends it must queue for the CPU again.
The kernel represents each process by a PCB (in Linux, struct task_struct). It holds everything
needed to stop the process and later resume it exactly where it left off:
Process ID (PID), parent PID, user and group IDs.
New, ready, running, waiting, terminated.
Program counter and all CPU registers, saved on every switch.
Priority, pointers into scheduling queues, CPU time used.
Base/limit registers or page-table pointer (Module 4).
CPU time, time limits, list of open files and allocated devices.
Switching the CPU from one process to another requires saving the state of the old process into its PCB and loading the saved state of the new one — a context switch.
Process P0 is executing in user mode. Its registers live in the CPU.
A timer interrupt or system call traps into the kernel (Session 1.4).
The kernel saves P0's program counter, registers and state into PCB0.
The scheduler chooses the next process, P1, from the ready queue.
The kernel reloads P1's registers from PCB1, switches the memory map, and returns to user mode.
P1 continues exactly where it stopped last time. P0 sits in a queue until it is chosen again.
No useful work is done during the switch — typically a few microseconds. It depends on memory speed, the number of registers, and whether caches/TLB must be flushed. This cost is why time slices cannot be made arbitrarily small (Session 2.5).
The goal of multiprogramming is to keep the CPU busy; the goal of time-sharing is to switch often enough for users to interact. The kernel keeps processes in queues:
A queueing diagram: a process circulates between queues until it terminates.
| Scheduler | Decides | Runs | Controls |
|---|---|---|---|
| Long-term (job scheduler) | Which jobs are admitted into memory | Seconds/minutes apart | Degree of multiprogramming; aims for a good mix of I/O-bound and CPU-bound jobs |
| Short-term (CPU scheduler) | Which ready process gets the CPU next | Every few milliseconds — must be fast | CPU allocation (Sessions 2.4–2.5) |
| Medium-term | Which process to swap out of / back into memory | When memory is over-committed | Reduces the degree of multiprogramming temporarily (swapping, Module 4) |
An I/O-bound process spends more time doing I/O than computing (many short CPU bursts — an editor). A CPU-bound process does long computations (a video encoder). A good mix keeps both the CPU and the devices busy.
Process concept, states and the PCB
Recorded by Dr. Krithika L.B. Slot m2-process-states
Context switch and scheduling queues
Recorded by Dr. Krithika L.B. Slot m2-context-switch
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0When the event completes, the scheduler may already be running another process. The woken process must join the ready queue and wait to be dispatched — only the short-term scheduler moves processes to Running.
PID, state, PC and registers, scheduling info, memory info, accounting and open files. The PC tells the CPU which instruction to execute next when the process resumes; without it the process could not continue where it stopped.
The short-term scheduler picks the next process for the CPU many times a second; the long-term scheduler decides which jobs enter memory (controlling the degree of multiprogramming) and runs rarely.
Every switch costs context-switch time with no useful work. With a tiny slice, a large fraction of CPU time is wasted on switching.
counter, local, p and *p live?counter → data; local → stack; p (the pointer variable) → stack; *p (the int it points to) → heap.
We know what a process is. But where do processes come from, and how do they end? Next: fork(), exec(), wait() and exit().