Operating System Lect8
Operating System Lect8
Lecture #8
CPU Schdeduling
Objectives
• CPU Scheduling
• Types of Scheduling Algorithms
• Premptive and Non preemptive Scheduler
• Dispatcher
• Scheduling Criteria
CPU Scheduling
• The goal of the multi-programming system is to keep the CPU busy at all
times.
• CPU scheduling means to select the next process to be allocated to the
CPU whenever the CPU becomes idle.
• Also known as process scheduling or thread scheduling.
• The question is – When to call the short-term scheduler?
Four Circumstances
• A Process switches from running state to waiting state.
• Process switches from running state to ready state.
• Process switches from waiting state to ready state.
• A Process terminates.
Preemptive scheduling
• Preemptive scheduling is used when a process switches from running state to ready
state or from the waiting state to ready state.
• The resources (mainly CPU cycles) are allocated to the process for a limited amount
of time and then taken away, and the process is again placed back in the ready queue
if that process still has CPU burst time remaining.
• That process stays in the ready queue till it gets its next chance to execute.
• Under (2) and (3) the system forcefully takes back the CPU from the currently
running process to allocate it a new process. This is called preemptive scheduling.
This may be due to the arrival of a higher priority process or due to time-slice expiry.
Non-preemptive scheduling
• Non-preemptive Scheduling is used when a process terminates, or a process switches from
running to the waiting state. I
• n this scheduling, once the resources (CPU cycles) are allocated to a process, the process holds
the CPU till it gets terminated or reaches a waiting state.
• In the case of non-preemptive scheduling does not interrupt a process running CPU in the
middle of the execution. Instead, it waits till the process completes its CPU burst time, and then
it can allocate the CPU to another process.
• If you analyse points (1) and (4) closely, in both these situations the process voluntarily releases
the CPU. In (1) the process needs an I/O output devices so it releases the CPU. While in (4) the
process finishes and hence it releases the CPU. It is called non-preemptive scheduling.
Dispatcher
• A Dispatcher is that module of the operating system which gives control of the
CPU to the process. Short term scheduler only decides which process to allocate
to CPU. Whereas the dispatcher actually allocates the CPU to that process.
• It involves three steps:
• First, it performs Context switching
• Second, Switching to user mode
• Finally, Jumping to the proper location in the user program to restart that process.
Examples
• Algorithms based on preemptive scheduling are: Round Robin (RR),
Shortest Remaining Time First (SRTF), Priority (preemptive version), etc.
• Algorithms based on non-preemptive scheduling are:
Shortest Job First (SJF basically non preemptive) and
Priority (non preemptive version), etc.
Which is the best CPU scheduling
algorithm?
• CPU utilization – It means how much the CPU is busy. That is, for a given duration of
time how long was the CPU doing the processing. The CPU utilization should be high
(40-90%)
• Throughput – Throughput is the number of processes that complete execution in a given
time.
• Turnaround time – is the interval from the time of submission of the process to the time
of its completion
• Waiting time – is the time spent by the process in the ready queue (waiting for the CPU)
• Response time – is the time from the submission of the process until it gives the first
response.
• An algorithm is effective if it has high CPU utilization and throughput,
whereas low turnaround, waiting and response time.
CPU Scheduling Algorithms
• Some of the most commonly use CPU scheduling algorithms are:
• First Come First Serve(FCFS)
• Shortest Job First (SJF)
• Priority Scheduling
• Round Robin (RR)
First Come First Serve algorithm
• In First Come First Serve algorithm the process that arrives first is allocated the CPU first.
• A queue is maintained in the RAM (ready queue) and the processes are allocated to the
CPU as per their position in the queue i.e., the process first in the queue is allocated the
CPU first.
• Key Points of FCFS scheduling algorithm
• The job that arrives first is scheduled first
• Is non-preemptive algorithm
• Implemented using queue.
• Suffers from Convoy effect.
Process ID Arrival Time Burst Time Completion Turn Around Waiting Time
Time Time
1 0 40 40 40 0
2 1 3 43 42 39
3 1 1 44 43 42
Convoy Effect in FCFS
• FCFS may suffer from the convoy effect if the burst time of the first job is
the highest among all. As in the real life, if a convoy is passing through
the road then the other persons may get blocked until it passes completely.
This can be simulated in the Operating System also.
• If the CPU gets the processes of the higher burst time at the front end of
the ready queue then the processes of lower burst time may get blocked
which means they may never get the CPU if the job in the execution has a
very high burst time. This is called convoy effect or starvation.
Process ID Arrival Time Burst Time Completion Turn Around Waiting Time
Time Time
1 1 40 44 43 3
2 0 3 3 3 0
3 0 1 4 4 3
Any Query???