Topic 03 CPU Scheduling OS
Topic 03 CPU Scheduling OS
1. Basic Concepts
2. Scheduling Criteria
3. Scheduling Algorithms
1
1. Basic Concepts
2
CPU Scheduler
• The CPU scheduler selects from among the processes in ready queue,
and allocates a CPU core to one of them
• Queue may be ordered in various ways
• 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
• For situations 1 and 4, there is no choice in terms of scheduling. A
new process (if one exists in the ready queue) must be selected for
execution.
• For situations 2 and 3, however, there is a choice.
3
Preemptive and Nonpreemptive Scheduling
4
Preemptive Scheduling and Race Conditions
5
Dispatcher
• Dispatcher module gives control of
the CPU to the process selected by
the CPU scheduler; this involves:
• Switching context
• Switching to user mode
• Jumping to the proper location in the
user program to restart that program
• Dispatch latency – time it takes for
the dispatcher to stop one process
and start another running
6
2. Scheduling Criteria
7
Scheduling Algorithm Optimization Criteria
8
3. Scheduling Algorithms
9
3.1. 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
10
FCFS Scheduling (Cont.)
P2 P3 P1
0 3 6 30
11
3.2. 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
• Preemptive version called shortest-remaining-time-first
• The difficulty is knowing the length of the next CPU
request
• How do we determine the length of the next CPU burst?
• Could ask the user
• Estimate
12
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
13
Example of Shortest-remaining-time-first
14
Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
15
3.3. Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum q),
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.
• Timer interrupts every quantum to schedule next process
• Performance
• q large FIFO
• q small q must be large with respect to context switch, otherwise
overhead is too high
16
Example of RR with Time Quantum = 4
17
Example of RR with Time Quantum = 4
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
18
3.4. Priority Scheduling
19
Example of Priority Scheduling
20
Example of Priority Scheduling
21
Priority Scheduling w/ Round-Robin
ProcessA arri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
• Run the process with the highest priority. Processes with the same
priority run round-robin
22
Multilevel Queue
• With priority scheduling, have separate queues for each priority.
• Schedule the process in the highest-priority queue!
23
Multilevel Queue
• Prioritization based upon process type
24
Multilevel Feedback Queue
25
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 process enters queue Q0 which is served in RR
• When it gains CPU, the process receives 8 milliseconds
• If it does not finish in 8 milliseconds, the process is moved to queue Q1
• At Q1 job is again served in RR and receives 16 additional milliseconds
• If it still does not complete, it is preempted and moved to queue Q2
26
Review Questions
1. Describe how the process take place in the CPU scheduling decisions.
2. Differentiate between Preemptive and Non-preemptive Scheduling
3. Discuss the Scheduling Criteria
4. Consider the following processes, with the length of the CPU-burst time.
Process Burst Time Priority
P1 10 3
P2 3 1
P3 2 2
P4 1 4
The processes in the order P1, P2, P3, P4, all at time 0.
a. Draw the Gantt charts illustrating the execution of these processes using FCFS, SJF ,
RR (quantum = 1), and priority (a smaller priority number is the higher priority)
Scheduling.
b. Calculate the waiting time for each process and the average waiting time for each of the
scheduling algorithms in part a.
c. Which of the schedules is the minimal average waiting time?
5. Which of the following scheduling algorithms could result in starvation? and what is the
solution?
a. First-come, first-served
b. Shortest job first
c. Round robin
d. Priority 27
End of Topic 3: CPU Scheduling