The Life of a Process
A process isn't just "on" or "off." It goes through a series of states from the moment it's created to the moment it's destroyed. Understanding these states is like understanding the phases of a project โ each one has different rules and possibilities.
The Five Process States
Every process goes through these five states during its lifetime:
- New โ The process is being created. The OS is setting up its data structures, allocating memory, and loading the program code. It hasn't started running yet.
- Ready โ The process is loaded into memory and waiting for the CPU. It's fully prepared to run, but the processor is currently busy with another process. Think of it like waiting in line at a coffee shop โ you know what you want, you're ready to order, but the barista is serving someone else.
- Running โ The process has the CPU and is actively executing instructions. This is when the actual work happens.
- Waiting โ The process is waiting for something external โ an I/O operation to complete, a signal from another process, or data from a file. It can't run until that event occurs.
- Terminated โ The process has finished executing. The OS is cleaning up its resources, freeing its memory, and removing its data structures.
State Transitions
Processes move between these states based on specific events:
- New โ Ready โ The OS finishes creating the process and moves it to the ready queue.
- Ready โ Running โ The scheduler selects this process and assigns it the CPU.
- Running โ Ready โ The process's time slice expires (in time-sharing systems) and it's moved back to the ready queue.
- Running โ Waiting โ The process requests I/O or waits for an event.
- Waiting โ Ready โ The I/O completes or the event occurs, making the process ready to run again.
- Running โ Terminated โ The process finishes execution or is forcibly killed.
These transitions happen constantly. On a typical computer, thousands of state transitions occur every second, all managed by the operating system.
Process State Diagram:
โโโโโโโโโโโโโ
โ NEW โ
โโโโโโโฌโโโโโโ
โ admit
โผ
โโโโโโโโโโโโโ schedule โโโโโโโโโโโโโ
โ READY โ โโโโโโโโโโโโโโโโโ โ RUNNING โ
โโโโโโโฌโโโโโโ โโโโฌโโโฌโโโฌโโโโ
โ โ โ โ
โ timeout โ โ โ terminate
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โโโโโโโโ โโโโโโโโโโโโโโ
โ โ TERMINATED โ
โ I/O or โโโโโโโโโโโโโโ
โ event wait
โผ
โโโโโโโโโโโโโ
โ WAITING โ
โโโโโโโฌโโโโโโ
โ I/O complete
โโโโโโโโโโโโ READY
The Ready Queue
When multiple processes are ready to run, they're placed in a ready queue. This isn't necessarily a physical queue โ it could be a linked list, a priority tree, or some other data structure. The important thing is that the scheduler picks processes from this queue to run on the CPU.
In a system with 200 processes running, maybe only 4 are actually using CPUs at any given moment. The rest are waiting in the ready queue or stuck in the waiting state. The scheduler's job is to decide which process gets to run next โ and we'll explore the algorithms it uses in a later lesson.