The Linux process state machine

On the Linux OS, every process or thread runs through a variety of definite states, and by encoding these, we can form the state machine of a process (or thread)  on the Linux OS (do refer to Figure 1 in the following section while reading this).

Since we now understand that the KSE on the Linux OS is a thread and not a process, we shall ignore convention—which uses the word process—and instead use the word thread when describing the entity that cycles through various states of the state machine. (If more comfortable, you could always, in your mind, substitute the word process for thread in the following matter.)

The states that a Linux thread can cycle through are as follows (the ps(1) utility encodes the state via the letter shown here):

  • R: Ready-to-run or Running
  • Sleeping:
    • S: Interruptible Sleep
    • D: Uninterruptible Sleep
  • T: Stopped (or suspended/frozen)
  • Z: Zombie (or defunct)
  • X: Dead

When a thread is newly created (either via the fork(2), pthread_create(3) or clone(2) APIs), and once the OS determines that the thread is fully born, it informs the scheduler of its existence by putting the thread into a runnable state. A thread in the R state is either actually running on a CPU core or is in the ready-to-run state. What we need to understand is that in both cases, the thread is enqueued on a data structure within the OS called a run queue (RQ). The threads in the run queue are the valid candidates to run; no thread can possibly run unless it is enqueued on an OS run queue. (For your information, Linux from version 2.6 onward best exploits all possible CPU cores by setting up one RQ per CPU core, thus obtaining perfect SMP scalability.) Linux does not explicitly distinguish between the ready-to-run and running states; it merely marks the thread in either state as R.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset