0% found this document useful (0 votes)
15 views

OS CH 3

Uploaded by

Uuii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

OS CH 3

Uploaded by

Uuii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter 3

CPU Scheduling

By Kindu T.

cpu scheduling 1
CPU Scheduling

Outlines
Cpu Scheduling
Scheduling Criteria
Scheduling Algorithms
FCFS
 Shortest Job First
 Priority
Round Robin
Multiple Processor Scheduling
Real-time Scheduling
cpu scheduling 2
CPU Scheduling
Scheduling?
When a computer has many programs, it frequently has multiple
processes opposing for the CPU at the same time.
 This situation occurs whenever two or more processes are
simultaneously in the ready state.
 If only one CPU is available, a choice has to be made which process
to run next.
The operating system that makes the choice is called the scheduler
and the algorithm is called the scheduling algorithm.

cpu scheduling 3
CPU Scheduler
• Selects from among the processes in memory that are ready to execute,
and allocates the CPU to one of them.
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
• Scheduling under 1 and 4 is non preemptive.
• All other scheduling is preemptive.

cpu scheduling 4
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)

cpu scheduling 5
CPU Scheduling
1.Non-preemptive Scheduling
•Once CPU has been allocated to a process, the process
keeps the CPU until
• Process exits OR
•Process switches to waiting state

cpu scheduling 6
CPU Scheduling
2.Preemptive Scheduling
Process can be interrupted and must release the CPU.
Need to coordinate access to shared data
Event completes: process can moves from blocked
to ready
Timer interrupts

cpu scheduling 7
CPU Scheduling
Goals for CPU scheduling:
• Maximize throughput and resource utilization.
• Need to overlap CPU and I/O activities.
• Minimize
 Response time
Waiting time
 Turnaround time.
• Share CPU in a fair way.

cpu scheduling 8
CPU Scheduling
Therefore our goal is to:
Maximize CPU Utilization
Maximize Throughput
Minimize Turnaround time
Minimize Waiting time
Minimize response time

cpu scheduling 9
CPU Scheduling
Scheduling Algorithms
There are different types of Algorithms:
1.First-Come First-Serve (FCFS)
2.Shortest Job First (SJF)
• Non-preemptive
• Pre-emptive
3.Priority
4.Round-Robin

cpu scheduling 10
First Come First Serve (FCFS) Scheduling
• Rule: Process that requests the CPU first is allocated the CPU
first.
• FCFS is a non-preemptive algorithm.
• Implementation - using FIFO queues
• Incoming process is added to the tail of the queue.
• Process selected for execution is taken from head of
queue.
• Performance metric - Average waiting time in queue.
• Gantt Charts are used to visualize schedules.
cpu scheduling 11
First Come First Serve (FCFS) Scheduling
• Example: 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
cpu scheduling 12
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.
• Convoy effect short process behind long process
cpu scheduling 13
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.
• Two schemes:
• Non preemptive – once CPU given to the process it cannot be preempted until
completes its CPU burst.
• Preemptive – if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is know
as the Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time for a given set
of processes.

cpu scheduling 14
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16
• Process Wait Time : Service Time - Arrival Time
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
cpu scheduling 15
Example of Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

• Average waiting time = (9 + 1 + 0 +2)/4 = 3


cpu scheduling 16
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest
integer  highest priority).
• Preemptive
• Non preemptive
• SJF is a priority scheduling where priority is the predicted next CPU
burst time.
• Problem  Starvation – low priority processes may never execute.
• Solution  Aging – as time progresses increase the priority of the
process.
cpu scheduling 17
Example of priority scheduling

• Waiting time of each process p1=19,p2=12,p3=0,p4=5.p5=16


• AVG watt time:(19+12+0+5+16)/5 = 10.4
cpu scheduling 18
Round Robin (RR)
• 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.
• If there are n processes in the ready queue and the time quantum is q,
then each process gets 1/n of the CPU time in chunks of at most q time
units at once. No process waits more than (n-1)q time units.
• Performance
• q large  FIFO
• q small  q must be large with respect to context switch, otherwise overhead
is too high.

cpu scheduling 19
Example: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

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


cpu scheduling 20
How a Smaller Time Quantum Increases Context Switches

cpu scheduling 21
Multilevel Queue
• Multiple-level queues are not an independent scheduling algorithm. They
make use of other existing algorithms to group and schedule jobs with
common characteristics.
 Multiple queues are maintained for processes with common characteristics.
 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.
• For example, CPU-bound jobs can be scheduled in one queue and all I/O-
bound jobs in another queue. The Process Scheduler then alternately selects
jobs from each queue and assigns them to the CPU based on the algorithm
assigned to the queue.

cpu scheduling 22
Real-Time Scheduling
• Hard real-time systems – required to complete a critical task within a
guaranteed amount of time.
• Soft real-time computing – requires that critical processes receive
priority over less fortunate ones.

cpu scheduling 23
Example-2 Using the ff table;
1. Draw Gantt chart for preemptive SJF?
2.Waiting time for each process?
3.Average waiting time? Process Arrival time Burst time

P1 8 1
P2 5 1
P3 2 7
P4 4 3
P5 2 8
P6 4 2
P7 3 5
OS-5th [By Lake F.] 24

OS-5th [By Lake F.] 25

You might also like