Analysis of Linux Process status: R, S, and D

Source: Internet
Author: User

Linux is a multi-user, multi-task system. If you can run multiple programs of multiple users at the same time, many processes will inevitably be generated, and each process will have different states.

Linux Process status: R (TASK_RUNNING), executable status.

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 block of these processes) A process can only appear in an 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 this CPU.

Many operating system textbooks define the processes being executed on the CPU as RUNNING, while the processes that can be executed but have not been scheduled as READY, these two statuses are in the TASK_RUNNING state in linux.

Linux Process status: S (TASK_INTERRUPTIBLE), which can be interrupted.

A process in this state is suspended because it waits for the occurrence of a certain event, such as waiting for a socket connection or a 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.

Through the ps command, we can see that the vast majority of processes in the process list are in the TASK_INTERRUPTIBLE State unless the machine has a high load ). After all, there are one or two CPUs and hundreds of processes. if not most processes are sleeping, how can the CPU respond.

Linux Process status: D (TASK_UNINTERRUPTIBLE), which cannot be interrupted.

Like TASK_INTERRUPTIBLE, 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. Otherwise, you will be surprised to find that kill-9 is not killing a sleeping process! As a result, we also understand why the ps command shows almost no TASK_UNINTERRUPTIBLE status for the process, but always TASK_INTERRUPTIBLE status.

The significance of the TASK_UNINTERRUPTIBLE status lies in that some kernel processing processes cannot be interrupted. If an asynchronous signal is returned, a program is inserted into the execution process to process the asynchronous signal. The inserted process may only exist in the kernel state or extend to the user State ), the original process was interrupted. See Linux Kernel Asynchronous interrupt Analysis)
When a process operates on certain hardware, for example, a process calls the read system call to read a device file, and the read system call finally executes the code of the corresponding device driver, and interact with the corresponding physical device), you may need to use the TASK_UNINTERRUPTIBLE status to protect the process, so as to prevent the process of interaction between the Process and the device from 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, see magic vfork).
The following code can be used to obtain a process in the TASK_UNINTERRUPTIBLE state:

 
 
  1. #include   
  2. void main() {  
  3. if (!vfork()) sleep(100);  


Compile and run the program. ps:

 
 
  1. kouu@kouu-one:~/test$ ps -ax | grep a\.out  
  2. 4371 pts/0    D+     0:00 ./a.out  
  3. 4372 pts/0    S+     0:00 ./a.out  
  4. 4374 pts/1    S+     0:00 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.

  1. Parsing Linux Process status T, Z, and X
  2. In-depth discussion on Linux partition Solutions
  3. Six advantages of Linux
  4. Linux File redirection and file Filter
  5. Full parsing of Linux File Types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.