Skip to content

Deep look at Linux process state

Linux Process Types

User Processes

User processes: Most processes in the system are user processes. User processes are started by regular user accounts and run in user space. Usually, user processes cannot access the processor for special purposes or access files that the user who started the processes does not have permission to access.

Daemon Processes

Daemon processes: Daemon processes are usually background programs managed by some running services. Daemon processes can be used to listen for requests and then access certain services. For example, the httpd daemon listens for requests to access web pages. Daemon processes can also be used to start tasks on their own. For example, the crond daemon starts scheduled tasks at predetermined times. Daemon processes are usually started by the system at startup and run continuously until the system is shut down. Of course, daemon processes can also be started and stopped on demand, and can be made to run at specific system run levels or trigger the reloading of configuration information during runtime.

Kernel Processes

Kernel processes: Kernel processes only run in kernel space. Kernel processes are similar to daemon processes, but the main difference is that kernel processes have complete access to kernel data structures. In addition, kernel processes are less flexible than daemon processes. For example, modifying a configuration file and triggering a reload can modify the behavior of a daemon process, but for a kernel process, modifying behavior requires recompiling the kernel itself.

Linux Process States

static const char * const task_state_array[] = {
"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"X (dead)", /* 16 */
"Z (zombie)", /* 32 */};

Running state (R)

TASK_RUNNING: In this state, the process is either executing in the CPU or waiting to run in the run queue.

Stopped state (T)

TASK_STOPPED or TASK_TRACED: When a process is suspended due to a specific signal (such as SIGINT or SIGSTOP), it is in this state, waiting for a resumption signal such as SINCONT.

Interruptible wait state (S)

TASK_INTERRUPTIBLE: Processes in this state are blocked and become running when a certain condition is met. Processes in this state are also awakened early due to signals received.

Uninterruptible wait state (D)

TASK_UNINTERRUPTIBLE: This state is similar in meaning to the interruptible wait state, with the only difference being that processes in this state do not respond to signals. The most typical example of an uninterruptible wait state is a process waiting for disk I/O operations.

Zombie state (Z)

TASK_DEAD - EXIT_ZOMBIE: Also known as a zombie process, every process is in a zombie state after it ends, waiting for the parent process to release resources. The process in this state has already ended, but its parent process has not released its system resources.

The process of generating a zombie process is as follows: After the parent process calls fork() to create a child process, the child process runs until it terminates and is immediately removed from memory, but the process descriptor is still retained in memory (the process descriptor occupies very little memory). The child process’s status becomes EXIT_ZOMBIE, and the child process sends a SIGCHLD signal to the parent process. At this point, the parent process should call the wait() system call to obtain the child process’s exit status and other information. After the wait() call, the zombie process is completely removed from memory. Therefore, a zombie process exists between its termination and the time when the parent process calls the wait() function to collect the zombie process. Generally, they disappear quickly, but if the programming is unreasonable, the parent process will not call wait() and other system calls to collect the zombie process, so these processes will continue to exist in memory. Finally, a lingering zombie process is formed.

Exit state (X)

TASK_DEAD - EXIT_DEAD: The EXIT_DEAD state is very short-lived and is almost impossible to capture with the ps command.

In-depth Reading

  • Understanding Unix Processes