CPU Scheduling
CPU Scheduling
CPU Scheduling
ROBIN MATHUR
CSE DEPTT, LPU
CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Thread Scheduling
• Multiple-Processor Scheduling
• Operating Systems Examples
• Algorithm Evaluation
Objectives
• To introduce CPU scheduling
• To describe various CPU-scheduling algorithms
• To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a
particular system
Basic Concepts
• CPU scheduling is the basis of multiprogramming.
• Success of CPU scheduling depends upon some observed properties of
process.
• Process execution consists of a CPU execution and IO wait. Process
alternates between these two states.
• CPU burst: controlled by CPU(execution).
• IO burst: controlled by IO(execution).
• Process execution begins with CPU burst. This is followed by IO wait.
Then another CPU burst and IO burst and so on. Eventually CPU burst will
end with the system request to terminate execution.
• CPU scheduler : when CPU becomes idle OS must select one of the
processes from the ready queue to be executed. (short term scheduler)
Alternating Sequence of CPU And I/O Bursts
CPU Scheduler
• Selects from among the processes in memory that are ready to execute, and
allocates the CPU to one of them
• Preemptive
• Non-preemptive
Dispatcher
• Dispatcher module gives control of the CPU to the process selected by
the short-term scheduler; this involves:
– switching context
– switching to user mode
– jumping to the proper location in the user program to start/restart
that program
• Dispatch latency – time it takes for the dispatcher to stop one process
and start another running
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete their execution per time unit
• Turnaround time – amount of time to execute a particular process
• Waiting time – amount of time a process has been waiting in the ready
queue
• Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment)
Scheduling Algorithm Optimization Criteria
P1 P2 P3
0 24 27 30
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
IMPORTANT FORMULA
• Completion Time: Time at which process completes its execution.
• Turn Around Time: Time Difference between completion time and arrival
time. Turn Around Time = Completion Time – Arrival Time
TAT = CT - AT
• Waiting Time(W.T): Time Difference between turn around time and burst
time.
Waiting Time = Turn Around Time – Burst Time
WT= TAT – BT or TAT = WT + BT
• FCFS is non preemptive. Once CPU has been allocated to a process that process
keeps the CPU until it terminates or IO request comes. This is particularly
troublesome for time sharing systems where each user needs to get a share of CPU
at regular intervals. It would be disastrous to allow one process to keep CPU for an
extended period.
• Convoy effect short process behind long process. All processes are waiting for one
big process to get off the CPU. This effect results in lower CPU utilization.
FCFS Scheduling (Cont)
Suppose that the processes arrive in the order
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
P4 P1 P3 P2
0 3 9 16 24
• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
Shortest remaining time first
(SRTF) or preemptive SJF
• This Algorithm is the preemptive version of SJF scheduling. In
SRTF, the execution of the process can be stopped after
certain amount of time. At the arrival of every process, the
short term scheduler schedules the process with the least
remaining burst time among the list of available processes
and the running process.
• Once all the processes are available in the ready queue, No
preemption will be done and the algorithm will work as SJF
scheduling.
• The context of the process is saved in the Process Control
Block when the process is removed from the execution and
the next process is scheduled. This PCB is accessed on the
next execution of this process.
Shortest remaining time first
(SRTF) or preemptive SJF
Pros:
Maximum throughput
Min waiting time and TAT
Cons:
• The real difficulty with the SJF algorithm is knowing the length of the next
CPU request.
• For long term scheduler, SJF can be utilized.
• Not implemented for short term scheduler as there is no way to predict
the burst time.
• Starvation to longer jobs
Practice problem SRTF
SRTF Practice problem solution
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority
• Equal priority processes are treated in FCFS manner.
• No general agreement whether 0 is highest or lowest priority.
• But in this we assume; (smallest integer highest priority)-> BUT CHECK PROBLEM !
• Priorities can be defined internally or externally.
• Internally defined priorities use some measurable quantities. Eg time limits,
memory requirements, number of open files etc.
• Externally: importance of the process and other political factors.
Priority Scheduling – Non
preemptive case
– Preemptive will preempt the CPU if priority of newly arrived process is higher
than the currently running process.
– Non-preemptive
• SJF is a priority scheduling where priority is the predicted according to the shortest
CPU burst time
• Problem Starvation – low priority processes may never execute
• Solution Aging – as time progresses increase the priority of the process
Practice problem priority
scheduling
Round Robin (RR)
• RR scheduling is specially designed for the time sharing systems.
• It is similar to the FCFS scheduling but preemption is added to switch
between the processes.
• Each process gets a small unit of CPU time (time quantum), usually 10-
100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• New processes are added to the tail of the queue. CPU scheduler
picks the first process from the ready queue, sets a timer to interrupt
after 1 time quantum and dispatches the process.
• RR approach is called processor sharing.
Example of RR with Time
Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30