Process Scheduling - Module2
Process Scheduling - Module2
Module 2
Introduction
● By switching the CPU among processes, the OS can make the computer
more productive
● When more than one process is runnable, the operating system must decide
which one first.
● The part of the operating system concerned with the above decision is called
the scheduler
● Process execution consists of a cycle of a CPU time burst and an I/O time
burst (i.e. wait).
● Processes alternate between these two states (i.e., CPU burst and I/O
burst). Eventually, the final CPU burst ends with a system request to
terminate execution
Figure: Alternating sequence of CPU and I/O bursts & CPU
Burst Histogram
CPU Scheduler
● Whenever the CPU becomes idle, the OS must select the processes in the
ready queue to be executed
● The selection process is carried out by the short-term scheduler (or CPU
Scheduler)
● The scheduler selects the process from the processes in memory that are
ready to execute and allocate the CPU to that process
Preemptive & Non-Preemptive Scheduling
● CPU scheduling decision may take place under the following four
circumstances:
1. When a process switches from running to waiting state.
For ex: as a result of I/O request or the invocation of wait for the termination
of one of the child processes
2. When a process switches from running to ready state.
For ex: when an interrupt occurs
3. When a process switches from waiting to ready state.
For example at completion of I/O
4. When a processes switches from running to terminated state.
Continued
4. Waiting time –The sum of amount of time a process wait in ready queue
(doesn’t affect the amount of time during which a process executes or does I/O;)
5. Turnaround time – The amount of time to execute a particular process from
the time of submission through the time of completion. (completion time – arrival
time)
Scheduling Algorithm Optimization Criteria
● Max CPU utilization
● Max throughput
● Min turnaround time
● Min waiting time
● Min response time (minimize the variance)
Scheduling Algorithms
● CPU Scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU
➢ Terminologies for CPU scheduling algorithms:
1. Arrival Time: Time at which the process arrives in the ready queue.
2. Completion Time: Time at which process completes its execution.
3. Burst Time: Time required by a process for CPU execution.
4. Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
5. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
First-Come, First-Served Scheduling
Ex.1 Assume processes arrives in the order P1,P2,P3 at t=0. Show the FCFS
result on Gantt chart.
Process Burst time
P1 24
P2 3
P3 3
Gantt chart:
The average waiting time= ((0-0)+(24-0)+(27-0))/3= 17 ms
The average turnaround time=((24-0)+(27-0)+(30-0))/3= 27 ms
Ex.2 Suppose that the processes arrive in the order: P2 , P3 , P1
The Gantt chart for the schedule is:
P4 P1 P3 P2
0 3 9 16 24
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
Examples on SJF
Ex #1: Non-Preemptive SJF(simultaneous arrival)
P1 0.0 6
P2 0.0 4
P3 0.0 1
P4 0.0 5
p1 p2 p3 p4
= (0 + 6 + 3 + 7)/4 = 4ms
= ( 7 + 10 + 4 + 11)/4 = 8ms
Ex #3: Preemptive SJF ( SRTF)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
The Gantt chart for the schedule is:
● The SJF algorithm is a special case of the general priority scheduling algorithm.
● A priority number (integer) is associated with each process.
● The CPU is allocated to the process with the highest priority (smallest CPU burst =
highest priority)
● Priority scheduling can be either preemptive or non-preemptive.
○ A preemptive approach will preempt the CPU if the priority of the newly-arrived
process is higher than the priority of the currently running process
○ A non-preemptive approach will simply put the new process (with the highest
priority) at the head of the ready queue
Contd
● The main problem with priority scheduling is starvation, that is, low priority
processes may never get a chance to execute.
● A solution is aging. As time progresses, the priority of a process waiting for
the long time in the ready queue is increased
Problems on priority Scheduling
Ex. Consider the following set of processes arriving at t=0, in the order p1, p2,p3,p4,p5
with the burst time as shown. Using priority scheduling we would schedule these
processes as shown in the Gantt chart.(Non-preemptive)
Process Burst time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Gantt chart
Ex. Process Burst time (Assume time quantum=4ms, arrival time is t=0)
P1 24
P2 3
P3 3
The Gantt chart is:
● One rule of thumb is that 80% of the CPU bursts should be shorter than the
time quantum.
Problems on Round Robin Scheduling
Ex.2: Process Burst time (Assume time quantum=20ms, arrival time is t=0)
P1 53
P2 17
P3 68
P4 24
Ex #4: Calculate the average waiting time and turn around time for the Process
P1,P2,P3,P4,P5 using FCFS and Round Robin Scheduling algorithm (time quantum=2ms)
Process Arrival Time Burst Time
P1 0.0 3
P2 2.0 3
P3 3.0 2
P4 5.0 4
P5 7.0 2
Ex #5: Calculate the average waiting time and turn around time for the Process
P1,P2,P3,P4 using Round Robin Scheduling algorithm (time quantum=4ms)
Process Arrival Time Burst Time
P1 0 9
P2 1 5
P3 2 3
P4 3 4
1. Draw Gantt charts to illustrate the execution of the processes using the
following scheduling algorithm:
(1) FCFS, (2) nonpreemptive SJF, (3) preemptive SJF,
(4) nonpreemptive priority, (5) preemptive priority, and
(6) RR with time quantum=2
1. Calculate the average turnaround time when using each of the above
scheduling algorithms
2. Count the number of context switches when using each of the above
scheduling algorithms
Multilevel Queue scheduling
● Multi-level queue scheduling is used when processes can be classified into groups.
● For example, foreground (interactive) processes and background (batch)
processes
○ The two types of processes have different response-time requirements and so may have
different scheduling needs.
○ Also, foreground processes may have priority (externally defined) over background
processes.
● A multi-level queue scheduling algorithm partitions the ready queue into several
separate queues.
● The processes are permanently assigned to one queue, generally based on some
property of the process such as memory size, process priority, or process type .
Contd
● The foreground queue may have absolute priority over the background
queue.
● One example of a multi-level queue are the five queues as shown below.
● Each queue has absolute priority over lower priority queues. For example, no
process in the batch queue can run unless the queues above it are empty. And
whenever a new interactive process enters while executing batch processes, the
batch process would be preempted.
● However, this can result in starvation for the processes in the lower priority
queues.
● The solution is to introduce time slice among the queues.
● Each queue gets a certain portion of the CPU time, which it can then
schedule among its various processes.
o The foreground queue can be given 80% of the CPU time for RR
scheduling.
o The background queue can be given 20% of the CPU time for FCFS
scheduling.
Multilevel Feedback Queue scheduling
● The multilevel queue scheduling has the advantage of low scheduling
overhead, but it is inflexible. The multilevel feedback-queue scheduling
algorithm, in contrast, allows a process to move between queues.
● The idea is to separate processes according to the characteristics of their CPU
bursts. If a process uses too much CPU time,it will be moved to a lower-
priority queue. This scheme leaves I/O-bound and interactive processes in the
higher-priority queues.
● A process that waits too long in a lower-priority queue may be moved to a
higher-priority queue. This form of aging prevents starvation.
Contd
● Efficient use of the CPUs requires load balancing to keep the workload
evenly distributed.
● The POSIX Pthread that API allows specifying either PCS or SCS during
thread creation