From the birth (spawn) till death (kill/terminate or exit), process has a life cycle going through several states. Some processes exist in process table even after they are killed/died, those processes are called zombie processes. We have seen much about zombie process in this article. Lets check different process states now. Broadly process states are :
- Running or Runnable
- Sleeping or waiting
- Stopped
- Zombie
How to check process state
top command lists total count of all these states in its output header.
See highlighted row named Tasks above. It shows total number of processes and their state-wise split up.
Later in above top output observe column with heading S. This column shows process states. In output we can see 2 processes in sleeping state.
You can even use ps command to check process state. Use below syntex :
In above output you can see column titled S shows state of process. We have here 3 sleeping and one running process. Lets dive into each state.
Process state : Running
Most healthy state of all. It indicates process is active and serving its requests. Process is properly getting system resources (especially CPU) to perform its operations. Running process is process which is being served by CPU currently. It can be identified by state flag R in ps or top output.
Runnable state is when process has got all the system resources to perform its operation except CPU. This means process is ready to go once CPU is free. Runnable processes are also flagged with state flag R
Process state : Sleeping
Sleeping process is the one who awaits for resources to run. Since its on waiting stand, it gives up CPU and goes to sleep mode. Once its required resource is free, it gets placed in scheduler queue for CPU to execute. There are two types of sleep modes : Interruptible and Uninterruptible
Interruptible sleep mode
In this mode process awaits for particular time slot or specific event to occur. If those conditions occur, then process will come out of sleep mode. These processes are shown with state S in ps or top output.
Uninterruptible sleep mode
Process in this sleep mode gets its timeout value before going to sleep. Once the timeout sets off, it awakes. Or it awakes when waited-upon resources becomes available for it. It can be identified by state D in outputs.
Process state : Stopped
Process ends or terminates when they receive kill signal or they enter exit status. At this moment, process gives up all the occupied resources but does not release entry in process table. Instead it sends signal about termination to its parent process. This helps parent process to decide if child is exited successfully or not. Once SIGCHLD received by parent process, it takes action and release child process entry in process table.
Process state : Zombie
As explained above, while exiting process sends SIGCHLD to parent. During the time between sending signal to parent and then parent clearing out process slot in process table, process enters zombie mode. Process can stay in zombie mode if its parent died before it release child process’s slot in process table. It can be identified with Z in outputs.
So complete life cycle of process can be circle as –
- Spawn
- Waiting
- Runnable
- Running
- Stopped
- Zombie
- Removed from process table