Lect 3
Lect 3
CPU Scheduling
CPU Scheduling
● Scheduling
○ Basic Concepts of scheduling
○ Scheduling Criteria
○ Scheduling Algorithms
○ Algorithm Evaluation
CPU Scheduling
● In discussing process management and synchronization, we
talked about context switching among processes/threads on the
ready queue
● But we have glossed over the details of exactly which thread is
chosen from the ready queue
● Making this decision is called scheduling
● Scheduling is a task of selecting a process/thread from a ready
queue and letting it run on the CPU
○ This is done by a scheduler also called dispatcher
Types of Scheduler
● Non-preemptive scheduler
○ Process remains scheduled until voluntarily relinquishes
CPU
● Preemptive scheduler
○ Process may be descheduled at any time
When to schedule?
A B C
0 10 12 16
Time
SCHEDULING ALGORITHMS
1.First- Come, First-Served (FCFS) Scheduling
● Jobs are executed on first come, first serve basis.
● Easy to understand and implement.
● Poor in performance as average wait time is high.
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:
●
24 27 30
● Waiting time for P1 = 0; P2 = 24; P3 = 27
● Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
● The Gantt chart for the schedule is:
● Associate with each process the length of its next CPU burst
○ And then, 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 if all arrives simultaneously
○ The difficulty is knowing the length of the next CPU
request. One possibility
■ Could ask the user
Example of SJF
16 24
● Now we add the concepts of varying arrival times and preemption to the analysis
ProcessA arri Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
● Preemptive SJF Gantt Chart
10
● Average waiting time =
● WT for P1: (0-0) + (10-1)=9 WT for P2= 1 – 1= 0
● WT for P3: 17 – 2= 15 WT for P4: 5-3= 2
10 14 18 22 26 30
● 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
MULTIPROCESSOR SCHEDULING
Affinity and Load balancing
Multiple-Processor Scheduling