search
HomeOperation and MaintenanceLinux Operation and MaintenanceThere are several states of linux processes

There are several states of linux processes

Mar 13, 2023 am 10:11 AM
linuxprocess status

Linux process has 6 states: 1. R executable state, only processes in this state can run on the CPU; 2. S interruptible sleep state, processes in this state are waiting for something The event occurs and is suspended; 3. D uninterruptible sleep state, the process is in sleep state, but the process is uninterruptible at this moment; 4. T pause state or tracking state, send a SIGSTOP signal to the process, and it will Entering the T state in response to this signal; 5. Z zombie state, indicating that a process is about to die; 6. X death state.

There are several states of linux processes

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

In Linux, a process has 6 states, namely: executable state, interruptible sleep state, uninterruptible sleep state, suspended state or tracking state, zombie state and my death state.

Linux process status detailed explanation

R(TASK_RUNNING) executable status

Only in this Only processes in this state may run on the CPU. There may be multiple processes in the executable state at the same time, and the task_struct structures (process control blocks) of these processes are put into the executable queue of the corresponding CPU (a process can only appear in the executable queue of one CPU at most). The task of the process scheduler is to select a process from the executable queue of each CPU to run on that CPU.

Many operating system textbooks define processes that are executing on the CPU as the RUNNING state, and processes that are executable but have not yet been scheduled for execution as the READY state. These two states are unified into the TASK_RUNNING state under Linux.

S(TASK_INTERRUPTIBLE) Interruptible sleep state

The process in this state is waiting for a certain event to occur (such as waiting for a socket connection, waiting for a semaphore), And was suspended. The task_struct structures of these processes are put into the waiting queue of the corresponding events. When these events occur (triggered by external interrupts or triggered by other processes), one or more processes in the corresponding waiting queue will be awakened.

Through the ps command, we will see that, under normal circumstances, the vast majority of processes in the process list are in the TASK_INTERRUPTIBLE state (unless the load on the machine is very high). After all, there are only one or two CPUs, and there are often dozens or hundreds of processes. If not most of the processes are sleeping, how can the CPU respond?

D(TASK_UNINTERRUPTIBLE) Uninterruptible sleep state

Similar to the TASK_INTERRUPTIBLE state, the process is in a sleep state, but the process is uninterruptible at this moment. Uninterruptible does not mean that the CPU does not respond to interrupts from external hardware, but that the process does not respond to asynchronous signals.

In most cases, the process should always be able to respond to asynchronous signals when it is in sleep state. Otherwise, you will be surprised to find that kill -9 cannot kill a sleeping process! So we can also easily understand why the processes seen by the ps command rarely appear in the TASK_UNINTERRUPTIBLE state, but always in the TASK_INTERRUPTIBLE state.

The significance of the TASK_UNINTERRUPTIBLE state is that certain processing flows of the kernel cannot be interrupted. If you respond to an asynchronous signal, a process for processing asynchronous signals will be inserted into the execution flow of the program (this inserted process may only exist in the kernel mode, or may extend to the user mode), so the original process will be interrupted. .

When the process operates on certain hardware (for example, the process calls the read system call to read a certain device file, and the read system call eventually executes the code of the corresponding device driver and communicates with the corresponding physical device interaction), you may need to use the TASK_UNINTERRUPTIBLE state to protect the process to prevent the interaction between the process and the device from being interrupted, causing the device to fall into an uncontrollable state. The TASK_UNINTERRUPTIBLE state in this case is always very short-lived and basically impossible to capture through the ps command.

There is also a TASK_UNINTERRUPTIBLE state that is easy to capture in Linux systems. After executing the vfork system call, the parent process enters the TASK_UNINTERRUPTIBLE state until the child process calls exit or exec.

T(TASK_STPPED or TASK_TRACED) Suspend state or trace state

Send a SIGSTOP signal to the process, and it will enter the TASK_STOPPED state in response to the signal (unless the The process itself is in the TASK_UNINTERRUPTIBLE state and does not respond to signals). (SIGSTOP, like the SIGKILL signal, is very mandatory. The user process is not allowed to reset the corresponding signal processing function through the signal series system call.)

Send a SIGCONT signal to the process to change it from the TASK_STOPPED state Restore to TASK_RUNNING state.

When a process is being traced, it is in the special state of TASK_TRACED. "Being tracked" means that the process is paused and waiting for the process that is tracking it to operate on it. For example, if you set a breakpoint on the tracked process in gdb, the process will be in the TASK_TRACED state when it stops at the breakpoint. At other times, the tracked process is still in the states mentioned earlier.

For the process itself, the TASK_STOPPED and TASK_TRACED states are very similar, both indicating that the process is suspended.

The TASK_TRACED state is equivalent to an additional layer of protection on top of TASK_STOPPED. The process in the TASK_TRACED state cannot be awakened in response to the SIGCONT signal. The debugged process can only return to the TASK_RUNNING state until the debugging process performs operations such as PTRACE_CONT and PTRACE_DETACH through the ptrace system call (the operations are specified through the parameters of the ptrace system call), or the debugging process exits.

Z(TASK_DEAD - EXIT_ZOMBIE) Zombie state, the process becomes a zombie process

The process is in the TASK_DEAD state during the exit process.

During this exit process, all resources occupied by the process will be recycled, except for the task_struct structure (and a few resources). So the process is left with only the empty shell of task_struct, so it is called a zombie.

The reason why task_struct is retained is because task_struct stores the exit code of the process and some statistical information. And its parent process is likely to care about this information. For example, in the shell, the $? variable stores the exit code of the last foreground process that exited, and this exit code is often used as the judgment condition of the if statement.

Of course, the kernel can also save this information elsewhere and release the task_struct structure to save some space. However, it is more convenient to use the task_struct structure, because the search relationship from pid to task_struct has been established in the kernel, as well as the parent-child relationship between processes. To release the task_struct, you need to create some new data structures so that the parent process can find the exit information of its child process.

The parent process can wait for the exit of one or some child processes through the wait series of system calls (such as wait4, waitid) and obtain its exit information. Then the wait series of system calls will also release the body of the child process (task_struct).

When the child process exits, the kernel will send a signal to its parent process to notify the parent process to "collect the corpse". This signal defaults to SIGCHLD, but this signal can be set when creating a child process via the clone system call.

As long as the parent process does not exit, the child process in this zombie state will always exist. So if the parent process exits, who will "collect the corpse" of the child process?

When a process exits, all its child processes will be hosted by other processes (making them child processes of other processes). Who is entrusted to? This may be the next process in the process group of the exiting process (if one exists), or process number 1. So every process and every moment has a parent process. Unless it's process number 1.

Process No. 1, the process with pid 1, is also called the init process. After the Linux system starts, the first user mode process created is the init process. It has two missions:

  • Execute the system initialization script and create a series of processes (all of them are descendants of the init process);

  • Wait for the exit event of its child process in an infinite loop, and call the waitid system call to complete the "corpse collection" work;

init process will not be suspended or killed (This is guaranteed by the kernel). It is in the TASK_INTERRUPTIBLE state while waiting for the child process to exit, and is in the TASK_RUNNING state during the "recovery" process.

X(TASK_DEAD - EXIT_DEAD) Death state, the process is about to be destroyed

And the process may not retain its task_struct during the exit process. For example, this process is a detachable process in a multi-threaded program.

Or the parent process explicitly ignores the SIGCHLD signal by setting the handler of the SIGCHLD signal to SIG_IGN. (This is a POSIX specification, although the exit signal of the child process can be set to a signal other than SIGCHLD.)

At this point, the process will be placed in the EXIT_DEAD exit status, which means that the following code immediately The process will be completely released. So the EXIT_DEAD state is very short-lived and almost impossible to capture with the ps command.

The initial state of the process

The process is created through the fork series of system calls (fork, clone, vfork), the kernel (or Kernel module) can also create a kernel process through the kernel_thread function. These functions that create subprocesses essentially perform the same function - copy the calling process to obtain a subprocess. (You can decide whether various resources are shared or private through option parameters.)

So since the calling process is in the TASK_RUNNING state (otherwise, if it is not running, how can it be called?), the child process is also in the TASK_RUNNING state by default. In addition, the system call clone and kernel function kernel_thread also accept the CLONE_STOPPED option, thereby setting the initial state of the child process to TASK_STOPPED.

Process status changes

After the process is created, the status may undergo a series of changes until the process exits. Although there are several process states, there are only two directions for process state changes - from TASK_RUNNING state to non-TASK_RUNNING state, or from non-TASK_RUNNING state to TASK_RUNNING state.

In other words, if a SIGKILL signal is sent to a process in the TASK_INTERRUPTIBLE state, the process will first be awakened (enter the TASK_RUNNING state), and then exit in response to the SIGKILL signal (change to the TASK_DEAD state). It will not exit directly from the TASK_INTERRUPTIBLE state.

The process changes from the non-TASK_RUNNING state to the TASK_RUNNING state by other processes (which may also be interrupt handlers) performing wake-up operations. The process that performs the awakening sets the status of the awakened process to TASK_RUNNING, and then adds its task_struct structure to the executable queue of a CPU. Then the awakened process will have the opportunity to be scheduled for execution.

There are two ways for the process to change from the TASK_RUNNING state to the non-TASK_RUNNING state:

  • In response to the signal, it enters the TASK_STOPED state or the TASK_DEAD state;

  • Execute system calls to actively enter the TASK_INTERRUPTIBLE state (such as nanosleep system calls), or TASK_DEAD states (such as exit system calls); or enter the TASK_INTERRUPTIBLE state or because the resources required to execute system calls cannot be met. TASK_UNINTERRUPTIBLE status (such as select system call).

Obviously, both of these situations can only occur while the process is executing on the CPU.

Linux process status description

##Status symbolFull name of statusDescriptionRTASK_RUNNINGExecutable status & running status (status in the run_queue queue) STASK_INTERRUPTIBLEInterruptible sleep state, can handle signalDTASK_UNINTERRUPTIBLEUninterruptible sleep state, signal can be processed, with delay##TZ
TASK_STOPPED or TASK_TRACED In the pause state or tracking state, the signal cannot be processed because there is no time slice to run the code
TASK_DEAD - EXIT_ZOMBIE Exit status, the process becomes Zombie process. Cannot be killed, that is, does not respond to task signals, cannot be killed with SIGKILL
Extended knowledge: What is a waiting queue, what is a running queue, and what is a hang /Blocking, what is waking up the process? Being scheduled by the CPU is called waking up the process

When a process is running, because some of its operating conditions are not ready yet (for example, the network is required but the network card is not available, or it needs to wait for IO, that is, it needs to use peripherals ), it will be placed in the waiting queue, and the status bit in task_struct will also be changed to S/D.

When the process is in the S/D state, it is waiting in a waiting queue to use peripherals (such as network cards, disk monitors, etc.)

The queue waiting for the CPU is called the run queue. The device we set up is called the waiting queue

The so-called process, when running, may be in different queues due to operational needs

In different queues, the status is It's different

When a process is in the R state, if a certain peripheral is needed but the peripheral is being used, I will change your state to S/D, and then change your task_struct Put it in the waiting queue

Related recommendations: "

Linux Video Tutorial

"

The above is the detailed content of There are several states of linux processes. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools