CPU Scheduling

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 39

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

• Max CPU utilization


• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
Algorithms
• FCFS(First come first serve)
• SJF(shortest job first)
• Priority scheduling
• Round robin scheduling
• Multilevel queue
• Multilevel feedback queue.
FCFS
• First in, first out (FIFO), also known as first
come, first served (FCFS), is the simplest
scheduling algorithm. FIFO simply queues
processes in the order that they arrive in the
ready queue.
In this, the process that comes first will be
executed first and next process starts only
after the previous gets fully executed.
First-Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

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

• Waiting time for P1 = 6; P2 = 0; P3 = 3


• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case
FCFS when different arrival time
exist
Case : check arrival time and burst
time
Convoy effect in SJF
Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its next CPU burst. Use these lengths to
schedule the process with the shortest time
• SJF is optimal – gives minimum average waiting time for a given set of processes
Example of SJF
Process Arrival Time Burst Time
P1 0.0 6
P2 0.0 8
P3 0.0 7
P4 0.0 3
• SJF scheduling chart

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

• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

• Typically, higher average turnaround than SJF, but better response


Time Quantum and Context Switch
Multilevel Queue
• Processes are easily classified into :
foreground
background
• These two types of processes have different response time requirements and
different scheduling needs.
• Multilevel queue partitions ready queue into several queues.
– foreground – RR
– background – FCFS
Multilevel Queue Scheduling
Multilevel Feedback Queue
• In previous case processes are permanently assigned to a queue when
they enter the system. Processes can not move from one queue to other.
• A process can move between the various queues. Idea is to separate
processes according to characteristics of CPU bursts. If process uses too
much CPU time, it will be moved to the lower priority queue.
• This scheme leaves IO bound and interactive process in higher priority
queues.
• Aging can be implemented this way. A process that waits too long can be
shifted one level up.
• Multilevel-feedback-queue scheduler defined by the following parameters:
– number of queues
– scheduling algorithms for each queue
– method used to determine when to upgrade a process
– method used to determine when to demote a process
– method used to determine which queue a process will enter when that
process needs service
Example of Multilevel Feedback
Queue
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served FCFS. When it gains CPU, job
receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to
queue Q1.
– At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still
does not complete, it is preempted and moved to queue Q2.
Multilevel Feedback Queues

You might also like