is the basis of multiprogrammed operating systems.
The objective of multiprogramming is
to have some processes running at all times
to maximize CPU utilization.
Alternative sequence of CPU-bursts and I/O-bursts | Histogram of CPU-burst duration |
---|---|
![]() | ![]() |
selects a process from the processes in memory that are ready to execute and allocates the CPU to that process.
Then, how can we select a next process?
Linked List? Binary Tree? FIFO Queue? Priority Queue?
Non-preemptive scheduling
a process keeps the CPU until it releases it, either by terminating or by switching to the waiting state.
Preemptive scheduling
a process can be preempted by the scheduler.
When a process switches from the running to waiting state. non-preemptive
When a process switches from the running to ready state. preemptive or non-preemptive?
When a process switches from the waiting to ready state. preemptive or non-preemptive?
When a process terminates. non-preemptive
a module that gives control of the CPU's core to the process selected by the CPU scheduler.
switching context from one process to another
switching to user mode
jumping to the proper location to resume the user program
The dispathcer should be as fast as possible since it is invoked during every context switch.
How often do context switches occur?: 5, 30, 27 times per 1 second
$ vmstat 1 3 // 3 times check per 1 second $ vmstat 2 3 // 3 times check per 2 second
Non-preemptive scheduling: 62 times Preemptive scheduling: 0 times
CPU utilization: to keep the CPU as busy as possible.
Throughput: the number of processes completed per time unit.
Turnaround time:
how long does it take to execute a process?
from the time of submission to the time of completion.
the amount of time that a process spends waiting in the ready queue.
the sum of periods spend waiting in the ready queue.
decide which of the processes in the ready queue is to be allocated the CPU's core.
FCFS: First-Come, First-Served (old solution)
SJF: Shortest Job First(SRTF: Shortest Remaining Time First)
RR: Round-Robin
Priority-based
MLQ: Multi-Level Queue
MLFQ: Multi-Level Feedback Queue
First-Come, First-Served: the simplest CPU-scheduling algorithm.
The process that requests the CPU first is allocated the CPU first.
FCFS Scheduling can be easily implemented with a FIFO queue.
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds.
If the processes arrive in the order
Gantt Chart served by the FCFS policy |
---|
![]() |
Calculate the waiting time of this schedule.
Waiting Time for
Total Waiting Time:
Average Waiting Time: '
If the processes arrive in the order
Gantt Chart served by the FCFS policy |
---|
![]() |
Calculate the waiting time of this schedule.
Waiting Time for
Total Waiting Time:
Average Waiting Time:
Gantt Chart served by the FCFS policy |
---|
![]() |
Calculate the turnaround time of this schedule.
Turnaround Time for
Total Waiting Time:
Average Waiting Time:
Note that
The average waiting time under the FCFS policy
- is generally not minimal and may vary substantially
- if the processes' CPU-burst times vary greatly.
The FCFS scheduling algorithm is non-preemptive.
Note also that
The performance in a dynamic situation
What if we have one CPU-bound and many I/O-bound processes?
Convoy Effect
- all the other processes wait for the one big process to get off the CPU.
- results in lower CPU and device utilization than might be possible
- if the shorter processes were allowed to go first.
Shortest-Job-First: shortest-next-CPU-burst-first scheduling.
SFJ associates with each process, the length of the process's next CPU burst.
When the CPU is available, assign it to the process that has the smallest next CPU burst.
If two or more proceese are even, break the tie with the FCFS.
Gantt Chart served by the SJFS policy |
---|
![]() |
Calculate the waiting time of this schedule.
Waiting Time for
Total Waiting Time:
Average Waiting Time:
Calculate the turnaround time of this schedule.
Turnaround Time for
Total Waiting Time:
Average Waiting Time:
Note that
The SJF scheduling algorithm is probably optimal
- it gives the minimum average waiting time for a given set of processes.
Moving a short process before a long one
- decreases the waiting time of the short process
- more than it increases the waiting time of the long process.
- Consequently, the average waiting time decreases.
There is no way to know the length of the next CPU burst.
Try to approximate the SJF scheduling
We may be able to predict the length of the next CPU.
Pick a process with the shortest predicted CPU burst.
exponential average of the measured lengths of previous CPU burst.
, where
is the length of th CPU burst,
is our predicted value for the next CPU burst,
for
Note also that
SJF algorithm can be either preemptive or non-preemptive.
The choice arises
- when a new process arrives at the ready queue
- while a previous is still executing.
What if a newly arrived process is shortet than what is left of the currently executing process?
Shortest-Remaining-Time-First: Preemptive SJF scheduling
SRTF will preempt the currently running process, whereas a non-preemptive SJF will allow it to finish its CPU burst.
Gantt Chart served by the SRTF policy |
---|
![]() |
Calculate the waiting time of this schedule.
Total Waiting Time:
Average Waiting Time:
Round-Robin: preemptive FCFS with a time quantum.
A time quantum (or time slice) is a small unit of time.
The ready queue is treated as a circular queue.
The scheduler goes around the ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum.
One of two thins will happen
the process itself will release the CPU voluntarily
the scheduler will proceed to the next process in the ready queue.
the timer will go off and will cause an interrupt to the OS
a context switch will be executed
the process will be put at the tail of the ready queue.
When we use a time quantum of 4 milliseconds
Gantt Chart served by the RR policy |
---|
![]() |
Calculate the waiting time of this schedule.
Waiting Time for
Total Waiting Time:
Average Waiting Time:
Note that
The average waiting time under the RR policy is often long.
The RR scheduling algorithm is preemptive. (definitely)
- if a process's CPU burst exceeds one time quantum,
- that process is preempted and is put back in the ready queue.
The performance of the RR scheduling algorithm depends heavily on the size of the time quantum.
A priority is associated with each process, and the CPU is allocated to the process with the highest priority.
Processes with equal priority are scheduled in FCFS order.
Note that
The SJF is a special case of the priority based scheduling.
In this case, the priority is the inverse of the next CPU burst
Gantt Chart served by the Priority base policy |
---|
![]() |
Average Waiting Time:
Average Turnaround Time:
Priority scheduling can be either preemptive or non-preemptive.
The problem of starvation (indefinite blocking)
a blocked process: ready to run, but waiting for the CPU.
some low-priority processes may wait indefinitely.
A solution to the starvation problem is aging
gradually increase the priority of processes that wait in the system for a long time.
execute the highest-priority process and runs processes with the same priority using round-robin scheduling.
time quantum = 2
On most modern operating systems, it is kernel threads - not processes - that are being scheduled, and user threads are managed by a thread library.
So, the kernel is unaware of them, ultimately mapped to associated kernel threads.
Soft Realtime vs Hard Realtime
provide no guarantee as to when a critical real-time process will be scheduled
guarantee only that a critical process is preferred to noncritical one.
such as phone call
A task must be services by its deadline.
such as rock launching process