Kernel states
We've been talking about running,
ready,
and
blocked
loosely—let's now formalize these thread states.
- RUNNING
- QNX OS's RUNNING state simply means that the thread is now actively consuming the CPU. On an SMP system, there will be multiple threads running; on a single-processor system, there will be one thread running.
- READY
- The READY state means that this thread could be running right now—except that it's not, because another thread, (at the same or higher priority), is running. If two threads were capable of using the CPU, one thread at priority 10 and one thread at priority 7, the priority 10 thread would be RUNNING, and the priority 7 thread would be READY.
- The blocked states
- What do we call the blocked state?
The problem is, there's not just one blocked state.
Under QNX OS, there are in fact over a dozen blocking states.
Why so many? Because the kernel keeps track of why a thread is blocked.
We saw two blocking states already—when a thread is blocked waiting for a mutex, the thread is in the MUTEX state. When a thread is blocked waiting for a semaphore, it's in the SEM state. These states simply indicate which queue (and which resource) the thread is blocked on.
If a number of threads are blocked on a mutex (in the MUTEX blocked state), they get no attention from the kernel until the thread that owns the mutex releases it. At that point one of the blocked threads is made READY, and the kernel makes a rescheduling decision (if required).
Why
if required?
The thread that just released the mutex could very well still have other things to do and have a higher priority than that of the waiting threads. In this case, we go to the second rule, which states,The highest-priority ready thread will run,
meaning that the scheduling order has not changed—the higher-priority thread continues to run.