Operating System — Scheduling
Understand Scheduling in just 5 minutes
Your computer is performing hundreds and thousands of tasks at the same time every day. Have you ever thought about how it handles so many things in a stable and organised way?
What is Scheduling?
Scheduling is the procedure of deciding which process to run next and allocate resources to it to perform tasks. We often mention “Context Switching” when discussing scheduling. It is the actual switching procedure, from one process to another.
When does context switching happen?
The image below shows the lifecycle of a process. If you want to know more about process management, feel free check out my previous article.
There are many ways to make context switching happen:
- Whenever a process goes to blocking / waiting state (wait()/sleep())
- When a POSIX signal arrives (SIGCHLD)
- When an interrupt arrives (keystroke)
- When the OS scheduler interrupts and stops the process (round-robin, round-robin with priority)
Take a deep look on context switching
You may be wondering what context switching is actually doing in.
Suppose a process gives up running on the CPU. For examples, calling sleep().
Then, the scheduler is going to choose the next process to run.
But, before the scheduler can seize the control of the CPU, CPU has to backup all current context of that process to the kernel-space memory:
- current register values
- program counter (which line of code the current program is at)
If the scheduler decides to schedule another process in the ready queue, the schedule has to load the context of that process from the main memory to the CPU.
This entire operation is called “Context Switching”.
Context Switching is expensive
Direct costs in kernel:
- Save and restore registers, etc.
- Switch address space (expensive instructions)
Indirect costs:
- cache
- buffer cache
- TLB misses
What is process scheduling?
Scheduling is required because the number of computing resource — the CPU — is limited.
When a process is chosen by the scheduler, the process would have the CPU until:
- the process voluntarily waits for I/O
- the process voluntarily releases the CPU (e.g. exit())
- the process detects particular kinds of interrupts (e.g. periodic clock interrupt)
In old days, it was called “time-sharing”. Nowadays, all systems are time-sharing.
For process scheduling, there are different scheduling algorithms.
Scheduling algorithms
Shortest-job-first (SJF)
Whenever a new process arrives at the system, the scheduler steps in and selects the next task based on their remaining CPU requirements.
The drawback of this algorithm is the number of context switching can be high as context switching is expensive.
Round-robin
Round-Robin (RR) scheduling is preemptive.
Processes are running one-by-one as a circular queue and with no priority.
- New processes are added to the tail of the ready queue
- New process arrives will not trigger a new selection decision
Whenever the quantum of a process is used up, the process releases the CPU and this is the preemption.
Then, the scheduler steps in and it chooses the next process which has a non-zero quantum to run.
If all processes in the system have used up the quantum, they will be re-charged to their initial values.
Priority Scheduling
A task is given a priority and is usually an integer.
- A scheduler selects the next process based on the priority
- Higher priority process + Round-Robin = priority queue + new process arrival triggers a new selection
Static priority scheduling
Process is assigned a fix priority when they are submitted to the system. The highest priority class will be selected. The tasks are usually short-lived, but important
To prevent high-priority tasks from running indefinitely, lower priority classes will be scheduled only when the upper priority classes has no tasks.
Multiple queue priority scheduling
It is still a priority scheduler. But, at each priority class, different schedulers may be deployed. The priority can be a mix of static and dynamic.
Next Steps
If you’re reading this line, congratulations!!! You made it. You have learnt the basics of scheduling and the most common scheduling algorithms.
Feel free to check other articles about operating system below:
To know more about my backend learning path, check out my journey here: