Thursday, February 11, 2010

Linux Process States

The Linux kernel stores the list of processes in a circular doubly linked list of structures task_struct, called the task list. The process descriptor (task_struct) contains all the information about a specific process.Each task_struct structure has a state enry which describes the current state of the process. At any time on a Linux machine the processes running on it are in exactly one of five different states. The struct task_struct and the five states are defined in linux/sched.h.

TASK_RUNNING - runnable; The task is either running or ready to run and waiting in the runqueue.

TASK_INTERRUPTIBLE - sleeping or blocked; In this state the process is waiting for some event to complete. When it completes, the kernel puts back the process to the runqueue and make it runnable by changing the state to TASK_RUNNING. The process can also respond to a signal in this state.

TASK_UNINTERRUPTIBLE - Uninterrupted sleep; This is exactly the same state as TASK_INTERRUPTIBLE but it cannot be interrupted by any signals to make it runnable. This happens typically with the IO operation. This is used when the process must wait without interruption or when the event is expected to occur quite quickly like an IO operation. They are unkillable as the task will not respond to signals(SIGKILL wont work). These are the D state process that you will find with the ps command. Its not advised to attempt a kill on this process (by killing the parent), as the task may be in the middle of some important operation and may be holding some resources.

TASK_ZOMBIE - Zombie; A special case when the task has completed but its parent has not yet reaped it, by issuing a wait4() system call. A zombie process just occupies an entry in the process table. This entry exists because in case the parent wants to access it, the descriptor should be available. When the parent calls wait4() a zombie disappears.

TASK_STOPPED - All is well :) and is stopped; The task is not running and will not be scheduled.May be killed or caught by other signals.

No comments: