The R, S, D, T, ZR (TASK_RUNNING) and executable status of Linux Process status resolution.
Only processes in this status can run on the CPU. Multiple processes may be executable at the same time. The task_struct structure process control blocks of these processes) A process can only appear in an executable queue of one CPU at most ). The process scheduler selects a process from the executable queue of each CPU to run on the CPU.
Processes that are being executed on the CPU are defined as RUNNING, executable but not scheduled to be executed as READY. The two statuses are all considered TASK_RUNNING.
S (TASK_INTERRUPTIBLE), an interrupted sleep state.
A process in this state is suspended because it waits for the occurrence of a certain event, such as waiting for socket connection or semaphore. The task_struct structure of these processes is put into the wait queue of corresponding events. When these events are triggered by external interruptions or other processes), one or more processes in the corresponding waiting queue will be awakened.
Most processes in the process list are in the TASK_INTERRUPTIBLE state. There are only one or two CPUs, and hundreds of processes are involved. If not most processes are sleeping, the CPU will not respond.
D (TASK_UNINTERRUPTIBLE.
The process is in sleep state, but the process cannot be interrupted at the moment. Non-disruptive means that the CPU does not respond to external hardware interruptions, but that the process does not respond to asynchronous signals. In most cases, when a process is sleeping, it should always be able to respond to asynchronous signals.
The TASK_UNINTERRUPTIBLE status indicates that when a process operates on certain hardware, for example, a process calls the read system call to read a certain device file, the read system calls the code of the corresponding device driver and interacts with the corresponding physical device.) The process may need to be protected using the TASK_UNINTERRUPTIBLE status, to avoid the process of interaction between the Process and the device being interrupted, causing the device to fall into an uncontrollable state. In this case, the status of TASK_UNINTERRUPTIBLE is always very short, and it is basically impossible to capture it through the ps command.
In linux, TASK_UNINTERRUPTIBLE is easily captured. After the vfork system is called, the parent process enters the TASK_UNINTERRUPTIBLE State until the child process calls exit or exec. The following code can be used to obtain a process in the TASK_UNINTERRUPTIBLE state:
# Include <stdio. h>
# Include <unistd. h>
Void main ()
{
If (! Vfork ());
Sleep (100 );
Ruturn 0;
}
Compile and run the program. ps:
Njs @ njs :~ /Test $ ps-ax | grep a \. out
4371 pts/0 D +./a. out
4372 pts/0 S +./a. out
4374 pts/1 S + grep a. out
Then we can test the power of the TASK_UNINTERRUPTIBLE status. Whether killed or killed-9, the parent process in the TASK_UNINTERRUPTIBLE status still stands.
T (TASK_STOPPED or TASK_TRACED), paused or tracked.
When a SIGSTOP signal is sent to a process, it enters the TASK_STOPPED status because it responds to the signal, unless the process itself is in the TASK_UNINTERRUPTIBLE status and does not respond to the signal ). The same SIGSTOP and SIGKILL signals are mandatory. User processes are not allowed to reset the corresponding signal processing functions through system calls of the signal series .) Send a SIGCONT signal to the process to restore it from the TASK_STOPPED status to the TASK_RUNNING status.
Z (TASK_DEAD-EXIT_ZOMBIE), exit, and the process becomes a zombie process.
The process is in the TASK_DEAD status when it exits.
During this exit process, all resources occupied by the process will be recycled, except for the task_struct structure and a few resources. As a result, the process has only such an empty shell as task_struct, so it is called a zombie. Task_struct is retained because the process exit code and some statistics are saved in task_struct. The parent process may be concerned with this information. To release task_struct, you need to create a new data structure so that the parent process can find the exit information of its child process.
The parent process can wait for the exit of a child or child process and obtain its exit information through wait system calls such as wait4 and waitid. Then, wait system calls will release the child process's body task_struct. When a child process exits, the kernel sends a signal to its parent process to notify the parent process to "collect dead ". This signal is SIGCHLD by default, but you can set this signal when creating a sub-process by calling the clone system.
The following code creates an EXIT_ZOMBIE process:
# Include <stdio. h>
# Include <unistd. h>
Voidmain ()
{
If (fork ());
While (1)
Sleep (100 );
}
Compile and run the program. ps:
Njs @ njs :~ /Test $ ps-ax | grep a \. out
10410 pts/0 S +./a. out
10411 pts/0 Z + [a. out]
10413 pts/1 S + grep a. out
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 child process "? When a process exits, it will host all its sub-processes to other processes to make them sub-processes of other processes ). Who is hosting? It may be the next process in the process group that exits the process, if any) or process 1. Therefore, every process and every moment has a parent process. Unless it is a process no. 1.
Process 1, a process whose pid is 1, also known as the init process. After linux is started, the first user-state process created is the init process. It has two missions: 1. Execute the system initialization script and create a series of processes, all of which are descendants of the init process); 2. Wait for the exit event of its child process in an endless loop, and call the waitid system call to complete the "zombie" job. The init process will not be paused or killed, which is guaranteed by the kernel ). It is in the TASK_INTERRUPTIBLE status while waiting for the sub-process to exit, while the "zombie" process is in the TASK_RUNNING status.
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201309/161432195.png "title =" 12.png" alt = "161432195.png"/>