When a process executes, it goes through a number of states. The current state of the process tells us about the current activity of the process. The state of a process may change due to events like I/O requests, interrupt routines, synchronization of processes, process scheduling algorithms, etc.
We will start with Two State Process Model which is one of the most simplest process model.
Two State Process Model
Two State Process Model consists of two states:
- Not-running State: Process waiting for execution.
- Running State: Process currently executing.
When a process is first created by the OS, it initializes the program control block for the process and the new process enters the system in Not-running state. After some time, the currently running process will be interrupted by some events, and the OS will move the currently running process from Running state to Not-running state. The dispatcher then selects one process from Not-running processes and moves the process to the Running state for execution.
For example, if we implement round-robin scheduling then the running process moves to not-running state after the time quantum.
When a process finishes the execution, the process exits the system and the dispatcher again selects a new process and moves it to Running state.
All the processes in the Not-running state are maintained in a queue.
Three State Process Model
There is one major drawback of two state process model. When dispatcher brings a new process from not-running state to running state, the process might still be waiting for some event or I/O request. So, the dispatcher must traverse the queue and find a not-running process that is ready for execution. It can degrade performance.
To overcome this problem, we split the not-running state into two states: Ready State and Waiting (Blocked) State.
- Ready State: The process in the main memory that is ready for execution.
- Waiting or Blocked State: The process in the main memory that is waiting for some event.
The OS maintains a separate queue for both Ready State and Waiting State. A process moves from Waiting State to Ready State once the event it’s been waiting for completes.
Five State Process Model
In the five state model, we introduce two new states: new state and terminated state.
Reason for New State
In the previous models, we assumed that the main memory is large enough to accommodate all programs but this is not true. Modern programs are very large. Loading all processes in the main memory is not possible.
When a new process is made, its program is not loaded in the main memory. The OS only stores some information about the process in the main memory. The long term scheduler moves the program to the main memory when sufficient space is available. Such a process is said to be in new state.
Reason for Terminated State
In the previous models, when a process finishes execution, its resources are immediately freed. But there might be some other process that may need its data in the future.
For example, when a child process finishes execution, the OS preserves its data until the parent call wait(). The child process is still in memory but not available for execution. The child process is said to be in terminated state.
- Running: The currently executing process.
- Waiting/Blocked: Process waiting for some event such as completion of I/O operation, waiting for other processes, synchronization signal, etc.
- Ready: A process that is waiting to be executed.
- New: The process that is just being created. The Program Control Block is already being made but the program is not yet loaded in the main memory. The program remains in the new state until the long term scheduler moves the process to the ready state (main memory).
- Terminated/Exit: A process that is finished or aborted due to some reason.
New -> Ready: The long term scheduler picks up a new process from second memory and loads it into the main memory when there are sufficient resources available. The process is now in ready state, waiting for its execution.
Ready -> Running: The short term scheduler or the dispatcher moves one process from ready state to running state for execution.
Running -> Terminated: The OS moves a process from running state to terminated state if the process finishes execution or if it aborts.
Running -> Ready: This transition can occur when the process runs for a certain amount of time running without any interruption. For example, if we use round-robin to schedule processes, then the running process will move to the ready state after time quantum. Another example is if the priority of a process in the ready state is more than the priority of the currently running process, then OS may preempt the running process and move it to ready state.
Running -> Waiting: A process is put in the waiting state if it must wait for some event. For example, the process may request some resources or memory which might not be available. The process may be waiting for an I/O operation or it may be waiting for some other process to finish before it can continue execution.
Waiting -> Ready: A process moves from waiting state to ready state if the event the process has been waiting for, occurs. The process is now ready for execution.
Six State Process Model
It is commonly as Five state process model with suspend state.
There is one major flaw in the five-state model. As we know, the processor is much faster than I/O devices. Therefore, a situation may occur where the processor executes so fast that all of the processes move to waiting state and no process is in ready state. The CPU sits idle until atleast one process finishes the I/O operation. This leads to low CPU utilization.
To prevent this, if all the processes in the main memory are in waiting state, the OS suspends a process in waiting/blocked state and move it to secondary memory. The process is now in suspend state. This is known as swapping. All suspend processes are kept in a queue. The memory of the swapped process is freed.
CPU can now bring some other process in the main memory. There are two options. First is to bring a brand new process and the second option is to bring another process from suspend queue back to main memory. Bringing a process from the suspend queue is preferred.
You may be wondering swapping process to secondary memory is also I/O operation so it should degrade performance? Yes, it should but disk I/O operations are generally the fastest I/O operations. Thus, it enhances performance.
Waiting -> Suspend: The OS moves a process from the waiting state to a suspend state if all the processes in the main memory are in the waiting state.
Suspend -> Ready: When sufficient memory is available, the OS moves a process from the suspend state back to the main memory for execution.
Suspend -> Waiting: The process brought by OS from secondary memory to main memory might still be waiting for some event.
Seven State Process Model
It is commonly known as Five state process model with two suspended states.
There is one major drawback in the previous process state model. That is, the CPU doesn’t know which process in the suspend queue is ready for execution. CPU may swap a process that is still waiting for event completion from secondary memory back to the main memory. There is no point in moving a blocked process back to the main memory. The performance suffers.
To avoid this, we divide the suspend state into 2 states:
- Blocked/Suspend: The process is in secondary memory but not yet ready for execution.
- Ready/Suspend: The process is in secondary memory and ready for execution.
Waiting -> Blocked/Suspend: If all the processes in the main memory are in the waiting state, the processor swaps out atleast one waiting process back to secondary memory to free memory to bring another process.
Blocked/Suspend -> Waiting: This transition might look unreasonable but if the priority of a process in Blocked/Suspend state is greater than processes in Ready/Suspend state then CPU may prefer process with higher priority.
Blocked/Suspend -> Ready/Suspend: The process moves from Blocked/Suspend to Ready/Suspend state if the event, the process has been waiting for occurs.
Ready/Suspend -> Ready: The OS moves a process from secondary memory to the main memory when there is sufficient space available. Also, if there is a high priority process in Ready/Suspend state, then OS may swap it with a lower priority process in the main memory.
Ready -> Ready/Suspend: The OS moves a process from the ready state to ready/suspended to free main memory for a higher priority process.
New -> Ready/Suspend: The OS may move a new process to Ready/Suspended if the main memory is full.
Note: In all the process state models, a process can directly move from any state to terminated state. This is because the parent process can terminate the child process at any moment.