9. Intro to Scheduling Flashcards
What are the three states of a thread?
Running (executing instructions on a CPU), ready (not executing, but capable), and waiting/blocking/sleeping (not executing, not able to start yet)
Why does a thread transition from running to ready?
it was descheduled
Why does a thread transition from running to waiting?
it performed a blocking system call
Why does a thread transition from waiting to ready?
the event the thread was waiting for happened
Why does a thread transition from ready to running?
it was scheduled
Why does a thread transition from running to terminated?
it exited or hit a fatal exception
What is scheduling?
The process of choosing the next thread (or threads) to run on the CPU(s)
Why do we schedule threads?
We generally have more threads than cores to run them on, so we need to multiplex the CPU.
We (with the kernel) are also in charge of allocating the CPU and must try to make good decisions so applications can run properly
When are the 4 times that scheduling happens?
- When a thread voluntarily gives up the CPU by calling yield().
- When a thread makes a blocking system call and must sleep until the call completes
- When a thread exits.
- When the kernel decides that a thread has run for long enough (preemptive policy, not cooperative)
What makes a scheduling policy preemptive as opposed to cooperative?
The kernel preempts (or stops) a thread that has not requested to be stopped
What is the rationale behind having a way for threads to voluntarily give up the CPU?
yield() can be a useful way of allowing a well-behaved thread to tell the CPU that it has no more useful work to do (which is inherently cooperative)
What is the mechanism component of scheduling?
How we switch between threads
What is the policy component of scheduling?
How we choose the next thread to run
How do we switch between threads?
Perform a context switch and move threads between the ready, running, and waiting queues
Is this an example of scheduling policy or mechanism:
Deciding what thread to run
Policy