OperatingSystemConcepts 5 CPUScheduling
OperatingSystemConcepts 5 CPUScheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Preemptive
• Process release the CPU before it finish execution
• Example: Modern OS: Unix, Linux, Windows7
• Non-preemptive
• Process release CPU when:
• Running Waiting
• Running Terminated
• Example: MS Windows 3.1
© Copyright Information Technology Institute - 2018 8
Dispatcher
• Considerations
• Minimize maximum response time
• Minimize the variance of response times
© Copyright Information Technology Institute - 2018 12
SCHEDULING ALGORITHMS
• Shortest-Job First
• Priority
• Round-Robin
© Copyright Information Technology Institute - 2018 14
First-Come, First-Served (FCFS)
Scheduling
• Easily implemented
• Ready queue is FIFO
• Pn ready Pn PCB is linked to tail of queue
• Process at head of ready queue CPU
• Average waiting time is long!
PCB5 PCB2 PCB8 PCB4
head Next PCB Next PCB Next PCB NULL
tail . . . .
. . . .
. . . .
© Copyright Information Technology Institute - 2018 15
Example 1
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
© Copyright Information Technology Institute - 2018 16
Example 2
Process Burst Time
P1 3
P2 3
P3 24
• Suppose that the processes arrive in the order:
P1 , P2 , P3 The Gantt Chart for the schedule is:
P1 P2 P3
0 3 6 30
• Waiting time for P1 = 0; P2 =3; P3 = 6
• Average waiting time: (0 + 3 + 6)/3 = 3
© Copyright Information Technology Institute - 2018 17
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.
P4 P1 P3 P2
0 3 9 16 24
• Waiting time for P1 = 3; P2 =16; P3 =9; P4=0
• Average waiting time: (3 + 16 + 9 + 0)/4 = 7
© Copyright Information Technology Institute - 2018 19
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 7 8 12 16
0 2 4 5 7 11 16
0 7 11 15 16
P1 P2 P4 P2 P1 P3
0 2 5 9 10 15 16
• Average waiting time = (8 + 4 + 11 +0)/4 = 23/4