
Init
- Thread creation
thread_create()puts a thread into itsinitstate and allocates and initialises per-thread data structures. - Once that is done, thread creation code puts the thread in
readystate by adding the thread to the ready list (set of runnable threads that are waiting their turn to use a processor).
Ready
- A thread in
readystate is available to be run but is not currently running. - Its TCB is on the ready list, and the values of its registers are stored in its TCB.
- At any time, the scheduler can cause a thread to transition from
readytorunningstate, by copying its register values from its TCB to a processor’s registers.
Running
- A thread in
runningstate is running on a processor. At this time, its register values are stored on the processor rather than in the TCB. - A
runningthread can transition to thereadystate in two ways:- The scheduler can preempt a
runningthread and move it to thereadystate by saving the thread’s registers to its TCB and switching the processor to run the next thread on the ready list. - A
runningthread can voluntarily relinquish the processor and go fromrunningtoreadyby callingthread_yield().
- The scheduler can preempt a
Waiting
- A thread in the
waitingstate is waiting for some event. - While the scheduler can move a thread in the
readystate to therunningstate, a thread in thewaitingstate cannot run until some action by another thread moves it fromwaitingtoready. - The TCB of a
waitingthread is stored on the waiting list of some synchronisation variable associated with the event. When the required event occurs, the operating system moves the TCB from the synchronisation variable’s waiting list to the scheduler’s ready list, transitioning the thread fromwaitingtoready.
Finished
- A thread in
finishedstate never runs again. - The system can free some or all of its state for other use. Some systems may store
finishedthreads in a finished list.
One way to understand the states is to consider where a thread’s TCB and registers are stored.
| State of Thread | Location of TCB | Location of Registers |
|---|---|---|
init | Being Created | TCB |
ready | Ready List | TCB |
running | Running List | Processor |
waiting | Waiting List of a Synchronisation Variable | TCB |
finished | Finished List / Deleted | TCB / Deleted |