Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
CPU Scheduling (Part I)
Amir H. [email protected]
Amirkabir University of Technology(Tehran Polytechnic)
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 1 / 50
CPU Scheduling
I CPU scheduling is the basis of multiprogrammed OSs.
I By switching the CPU among processes, the OS makes the computermore productive.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 3 / 50
Basic Concepts
I In a single-processor system, only one process can run at a time.
I Others must wait until the CPU is free and can be rescheduled.
I The objective of multiprogramming is to have some process runningat all times, to maximize CPU utilization.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 4 / 50
Basic Concepts
I CPU-I/O burst cycle: process executionconsists of a cycle of CPU executionand I/O wait.
I CPU burst followed by I/O burst.
I CPU burst distribution is of mainconcern.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 5 / 50
Basic Concepts
I CPU-I/O burst cycle: process executionconsists of a cycle of CPU executionand I/O wait.
I CPU burst followed by I/O burst.
I CPU burst distribution is of mainconcern.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 5 / 50
Basic Concepts
I CPU-I/O burst cycle: process executionconsists of a cycle of CPU executionand I/O wait.
I CPU burst followed by I/O burst.
I CPU burst distribution is of mainconcern.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 5 / 50
CPU Scheduler
I Short-term scheduler selects from among the processes in readyqueue, and allocates the CPU to one of them.
I CPU scheduling decisions may take place when a process:1 Switches from running to waiting (e.g., an I/O request).2 Switches from running to ready (e.g., interrupt).3 Switches from waiting to ready (e.g., I/O completion).4 Terminates.
I For situations 1 and 4, there is no choice in terms of scheduling.A new process must be selected for execution. There is a choice,however, for situations 2 and 3.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 7 / 50
CPU Scheduler
I Short-term scheduler selects from among the processes in readyqueue, and allocates the CPU to one of them.
I CPU scheduling decisions may take place when a process:1 Switches from running to waiting (e.g., an I/O request).2 Switches from running to ready (e.g., interrupt).3 Switches from waiting to ready (e.g., I/O completion).4 Terminates.
I For situations 1 and 4, there is no choice in terms of scheduling.A new process must be selected for execution. There is a choice,however, for situations 2 and 3.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 7 / 50
CPU Scheduler
I Short-term scheduler selects from among the processes in readyqueue, and allocates the CPU to one of them.
I CPU scheduling decisions may take place when a process:1 Switches from running to waiting (e.g., an I/O request).2 Switches from running to ready (e.g., interrupt).3 Switches from waiting to ready (e.g., I/O completion).4 Terminates.
I For situations 1 and 4, there is no choice in terms of scheduling.A new process must be selected for execution. There is a choice,however, for situations 2 and 3.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 7 / 50
Dispatcher
I Dispatcher module gives control of the CPU to the process selectedby the short-term scheduler; this involves:
• Switching context• Switching to user mode• Jumping to the proper location in the user program to restart that
program
I Dispatch latency: time it takes for the dispatcher to stop one processand start another running.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 8 / 50
Dispatcher
I Dispatcher module gives control of the CPU to the process selectedby the short-term scheduler; this involves:
• Switching context• Switching to user mode• Jumping to the proper location in the user program to restart that
program
I Dispatch latency: time it takes for the dispatcher to stop one processand start another running.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 8 / 50
Scheduling Criteria (1/2)
I Different CPU-scheduling algorithms have different properties.
I The choice of a particular algorithm may favor one class of processesover another.
I CPU utilization: keep the CPU as busy as possible .
I Throughput: # of processes that complete their execution per timeunit .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 9 / 50
Scheduling Criteria (1/2)
I Different CPU-scheduling algorithms have different properties.
I The choice of a particular algorithm may favor one class of processesover another.
I CPU utilization: keep the CPU as busy as possible .
I Throughput: # of processes that complete their execution per timeunit .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 9 / 50
Scheduling Criteria (1/2)
I Different CPU-scheduling algorithms have different properties.
I The choice of a particular algorithm may favor one class of processesover another.
I CPU utilization: keep the CPU as busy as possible (Max).
I Throughput: # of processes that complete their execution per timeunit .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 9 / 50
Scheduling Criteria (1/2)
I Different CPU-scheduling algorithms have different properties.
I The choice of a particular algorithm may favor one class of processesover another.
I CPU utilization: keep the CPU as busy as possible (Max).
I Throughput: # of processes that complete their execution per timeunit .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 9 / 50
Scheduling Criteria (1/2)
I Different CPU-scheduling algorithms have different properties.
I The choice of a particular algorithm may favor one class of processesover another.
I CPU utilization: keep the CPU as busy as possible (Max).
I Throughput: # of processes that complete their execution per timeunit (Max).
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 9 / 50
Scheduling Criteria (2/2)
I Turnaround time: amount of time to execute a particular process .
I Waiting time: amount of time a process has been waiting in theready queue .
I Response time: amount of time it takes from when a request wassubmitted until the first response is produced, not output (for time-sharing environment) .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 10 / 50
Scheduling Criteria (2/2)
I Turnaround time: amount of time to execute a particular process(Min).
I Waiting time: amount of time a process has been waiting in theready queue .
I Response time: amount of time it takes from when a request wassubmitted until the first response is produced, not output (for time-sharing environment) .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 10 / 50
Scheduling Criteria (2/2)
I Turnaround time: amount of time to execute a particular process(Min).
I Waiting time: amount of time a process has been waiting in theready queue .
I Response time: amount of time it takes from when a request wassubmitted until the first response is produced, not output (for time-sharing environment) .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 10 / 50
Scheduling Criteria (2/2)
I Turnaround time: amount of time to execute a particular process(Min).
I Waiting time: amount of time a process has been waiting in theready queue (Min).
I Response time: amount of time it takes from when a request wassubmitted until the first response is produced, not output (for time-sharing environment) .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 10 / 50
Scheduling Criteria (2/2)
I Turnaround time: amount of time to execute a particular process(Min).
I Waiting time: amount of time a process has been waiting in theready queue (Min).
I Response time: amount of time it takes from when a request wassubmitted until the first response is produced, not output (for time-sharing environment) .
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 10 / 50
Scheduling Criteria (2/2)
I Turnaround time: amount of time to execute a particular process(Min).
I Waiting time: amount of time a process has been waiting in theready queue (Min).
I Response time: amount of time it takes from when a request wassubmitted until the first response is produced, not output (for time-sharing environment) (Min).
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 10 / 50
Scheduling Algorithms
I First-Come, First-Served Scheduling
I Shortest-Job-First Scheduling
I Priority Scheduling
I Round-Robin Scheduling
I Multilevel Queue Scheduling
I Multilevel Feedback Queue Scheduling
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 12 / 50
First-Come, First-Served (FCFS)Scheduling
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 13 / 50
FCFS Scheduling (1/2)
I Suppose that the processes arrive in theorder: P1, P2, P3
I The gantt chart for the schedule is:
I Waiting time for P1 = 0; P2 = 24; P3 = 27
I Average waiting time: 0+24+273 = 17
I FCFS scheduling algorithm is non-preemptive: process keeps theCPU until it releases the CPU, either requesting I/O.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 14 / 50
FCFS Scheduling (1/2)
I Suppose that the processes arrive in theorder: P1, P2, P3
I The gantt chart for the schedule is:
I Waiting time for P1 = 0; P2 = 24; P3 = 27
I Average waiting time: 0+24+273 = 17
I FCFS scheduling algorithm is non-preemptive: process keeps theCPU until it releases the CPU, either requesting I/O.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 14 / 50
FCFS Scheduling (1/2)
I Suppose that the processes arrive in theorder: P1, P2, P3
I The gantt chart for the schedule is:
I Waiting time for P1 = 0; P2 = 24; P3 = 27
I Average waiting time: 0+24+273 = 17
I FCFS scheduling algorithm is non-preemptive: process keeps theCPU until it releases the CPU, either requesting I/O.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 14 / 50
FCFS Scheduling (1/2)
I Suppose that the processes arrive in theorder: P1, P2, P3
I The gantt chart for the schedule is:
I Waiting time for P1 = 0; P2 = 24; P3 = 27
I Average waiting time: 0+24+273 = 17
I FCFS scheduling algorithm is non-preemptive: process keeps theCPU until it releases the CPU, either requesting I/O.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 14 / 50
FCFS Scheduling (2/2)
I Suppose that the processes arrive in the order: P2, P3, P1
I The gantt chart for the schedule is:
I Waiting time for P1 = 6; P2 = 0; P3 = 3
I Average waiting time: 6+0+33 = 3
I Much better than previous case.
I Convoy effect - short process behind long process: consider oneCPU-bound and many I/O-bound processes
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 15 / 50
FCFS Scheduling (2/2)
I Suppose that the processes arrive in the order: P2, P3, P1
I The gantt chart for the schedule is:
I Waiting time for P1 = 6; P2 = 0; P3 = 3
I Average waiting time: 6+0+33 = 3
I Much better than previous case.
I Convoy effect - short process behind long process: consider oneCPU-bound and many I/O-bound processes
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 15 / 50
FCFS Scheduling (2/2)
I Suppose that the processes arrive in the order: P2, P3, P1
I The gantt chart for the schedule is:
I Waiting time for P1 = 6; P2 = 0; P3 = 3
I Average waiting time: 6+0+33 = 3
I Much better than previous case.
I Convoy effect - short process behind long process: consider oneCPU-bound and many I/O-bound processes
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 15 / 50
FCFS Scheduling (2/2)
I Suppose that the processes arrive in the order: P2, P3, P1
I The gantt chart for the schedule is:
I Waiting time for P1 = 6; P2 = 0; P3 = 3
I Average waiting time: 6+0+33 = 3
I Much better than previous case.
I Convoy effect - short process behind long process: consider oneCPU-bound and many I/O-bound processes
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 15 / 50
FCFS Scheduling (2/2)
I Suppose that the processes arrive in the order: P2, P3, P1
I The gantt chart for the schedule is:
I Waiting time for P1 = 6; P2 = 0; P3 = 3
I Average waiting time: 6+0+33 = 3
I Much better than previous case.
I Convoy effect - short process behind long process: consider oneCPU-bound and many I/O-bound processes
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 15 / 50
Shortest-Job-First (SJF)Scheduling
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 16 / 50
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.• Use these lengths to schedule the process with the shortest time.
I SJF is optimal - gives minimum average waiting time for a given setof processes.
• The difficulty is knowing the length of the next CPU request.• Could ask the user (batch systems with long-term scheduling).
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 17 / 50
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.• Use these lengths to schedule the process with the shortest time.
I SJF is optimal - gives minimum average waiting time for a given setof processes.
• The difficulty is knowing the length of the next CPU request.• Could ask the user (batch systems with long-term scheduling).
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 17 / 50
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.• Use these lengths to schedule the process with the shortest time.
I SJF is optimal - gives minimum average waiting time for a given setof processes.
• The difficulty is knowing the length of the next CPU request.
• Could ask the user (batch systems with long-term scheduling).
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 17 / 50
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.• Use these lengths to schedule the process with the shortest time.
I SJF is optimal - gives minimum average waiting time for a given setof processes.
• The difficulty is knowing the length of the next CPU request.• Could ask the user (batch systems with long-term scheduling).
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 17 / 50
SJF Scheduling (2/2)
I The gantt chart for the schedule is:
I Waiting time for P1 = 3; P2 = 16; P3 = 9, P4 = 0
I Average waiting time: 3+16+9+04 = 7
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 18 / 50
SJF Scheduling (2/2)
I The gantt chart for the schedule is:
I Waiting time for P1 = 3; P2 = 16; P3 = 9, P4 = 0
I Average waiting time: 3+16+9+04 = 7
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 18 / 50
SJF Scheduling (2/2)
I The gantt chart for the schedule is:
I Waiting time for P1 = 3; P2 = 16; P3 = 9, P4 = 0
I Average waiting time: 3+16+9+04 = 7
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 18 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst2 τn+1 = predicted value for the next CPU burst3 α, 0 ≤ α ≤ 14 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst2 τn+1 = predicted value for the next CPU burst3 α, 0 ≤ α ≤ 14 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst
2 τn+1 = predicted value for the next CPU burst3 α, 0 ≤ α ≤ 14 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst2 τn+1 = predicted value for the next CPU burst
3 α, 0 ≤ α ≤ 14 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst2 τn+1 = predicted value for the next CPU burst3 α, 0 ≤ α ≤ 1
4 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst2 τn+1 = predicted value for the next CPU burst3 α, 0 ≤ α ≤ 14 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (1/2)
I Estimate the length, and pick process with shortest predicted nextCPU burst.
I The next CPU burst: predicted as an exponential average of themeasured lengths of previous CPU bursts.
1 tn = actual length of nth CPU burst2 τn+1 = predicted value for the next CPU burst3 α, 0 ≤ α ≤ 14 Define: τn+1 = αtn + (1− α)τn
I Commonly, α set to 12
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 19 / 50
Determining Length of Next CPU Burst (2/2)
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 20 / 50
Examples of Exponential Averaging
I α = 0
• τn+1 = τn• Recent history does not count
I α = 1
• τn+1 = tn• Only the actual last CPU burst counts
I If we expand the formula, we get:• τn+1 = αtn + (1− α)αtn−1 + · · ·
+(1− α)jαtn−j + · · ·+(1− α)n+1τ0
• Since both α and (1− α) are less than or equal to 1, eachsuccessive term has less weight than its predecessor.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 21 / 50
Examples of Exponential Averaging
I α = 0• τn+1 = τn• Recent history does not count
I α = 1
• τn+1 = tn• Only the actual last CPU burst counts
I If we expand the formula, we get:• τn+1 = αtn + (1− α)αtn−1 + · · ·
+(1− α)jαtn−j + · · ·+(1− α)n+1τ0
• Since both α and (1− α) are less than or equal to 1, eachsuccessive term has less weight than its predecessor.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 21 / 50
Examples of Exponential Averaging
I α = 0• τn+1 = τn• Recent history does not count
I α = 1
• τn+1 = tn• Only the actual last CPU burst counts
I If we expand the formula, we get:• τn+1 = αtn + (1− α)αtn−1 + · · ·
+(1− α)jαtn−j + · · ·+(1− α)n+1τ0
• Since both α and (1− α) are less than or equal to 1, eachsuccessive term has less weight than its predecessor.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 21 / 50
Examples of Exponential Averaging
I α = 0• τn+1 = τn• Recent history does not count
I α = 1• τn+1 = tn• Only the actual last CPU burst counts
I If we expand the formula, we get:• τn+1 = αtn + (1− α)αtn−1 + · · ·
+(1− α)jαtn−j + · · ·+(1− α)n+1τ0
• Since both α and (1− α) are less than or equal to 1, eachsuccessive term has less weight than its predecessor.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 21 / 50
Examples of Exponential Averaging
I α = 0• τn+1 = τn• Recent history does not count
I α = 1• τn+1 = tn• Only the actual last CPU burst counts
I If we expand the formula, we get:• τn+1 = αtn + (1− α)αtn−1 + · · ·
+(1− α)jαtn−j + · · ·+(1− α)n+1τ0
• Since both α and (1− α) are less than or equal to 1, eachsuccessive term has less weight than its predecessor.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 21 / 50
Preemptive SJF
I The SJF algorithm can be either preemptive or non-preemptive.
I Preemptive version called shortest-remaining-time-first
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 22 / 50
Preemptive SJF
I The SJF algorithm can be either preemptive or non-preemptive.
I Preemptive version called shortest-remaining-time-first
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 22 / 50
Example of Shortest-Remaining-Time-First
I Now we add the concepts of varying arrival times and preemptionto the analysis.
I The gantt chart for the schedule is:
I Average waiting time: (10−1)+(1−1)+(17−2)+(5−3)4 = 26
4 = 6.5
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 23 / 50
Example of Shortest-Remaining-Time-First
I Now we add the concepts of varying arrival times and preemptionto the analysis.
I The gantt chart for the schedule is:
I Average waiting time: (10−1)+(1−1)+(17−2)+(5−3)4 = 26
4 = 6.5
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 23 / 50
Priority Scheduling (1/2)
I A priority number (integer) is associated with each process.
I The CPU is allocated to the process with the highest priority.• Smallest integer = Highest priority• Preemptive and non-preemptive
I SJF is priority scheduling where priority is the inverse of predictednext CPU burst time.
I Problem: starvation - low priority processes may never execute
I Solution: aging - as time progresses increase the priority of theprocess
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 25 / 50
Priority Scheduling (1/2)
I A priority number (integer) is associated with each process.
I The CPU is allocated to the process with the highest priority.• Smallest integer = Highest priority• Preemptive and non-preemptive
I SJF is priority scheduling where priority is the inverse of predictednext CPU burst time.
I Problem: starvation - low priority processes may never execute
I Solution: aging - as time progresses increase the priority of theprocess
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 25 / 50
Priority Scheduling (1/2)
I A priority number (integer) is associated with each process.
I The CPU is allocated to the process with the highest priority.• Smallest integer = Highest priority• Preemptive and non-preemptive
I SJF is priority scheduling where priority is the inverse of predictednext CPU burst time.
I Problem: starvation - low priority processes may never execute
I Solution: aging - as time progresses increase the priority of theprocess
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 25 / 50
Priority Scheduling (1/2)
I A priority number (integer) is associated with each process.
I The CPU is allocated to the process with the highest priority.• Smallest integer = Highest priority• Preemptive and non-preemptive
I SJF is priority scheduling where priority is the inverse of predictednext CPU burst time.
I Problem: starvation - low priority processes may never execute
I Solution: aging - as time progresses increase the priority of theprocess
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 25 / 50
Priority Scheduling (1/2)
I A priority number (integer) is associated with each process.
I The CPU is allocated to the process with the highest priority.• Smallest integer = Highest priority• Preemptive and non-preemptive
I SJF is priority scheduling where priority is the inverse of predictednext CPU burst time.
I Problem: starvation - low priority processes may never execute
I Solution: aging - as time progresses increase the priority of theprocess
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 25 / 50
Priority Scheduling (2/2)
I The gantt chart for the schedule is:
I Average waiting time: 0+1+6+16+185 = 8.2
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 26 / 50
Priority Scheduling (2/2)
I The gantt chart for the schedule is:
I Average waiting time: 0+1+6+16+185 = 8.2
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 26 / 50
RR Scheduling (1/3)
I Each process gets a small unit of CPU time (time quantum q),usually 10-100 milliseconds.
I After this time has elapsed, the process is preempted and added tothe end of the ready queue.
I If there are n processes in the ready queue and the time quantumis q, then each process gets 1/n of the CPU time in chunks of atmost q time units at once.
I No process waits more than (n − 1)q time units.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 28 / 50
RR Scheduling (1/3)
I Each process gets a small unit of CPU time (time quantum q),usually 10-100 milliseconds.
I After this time has elapsed, the process is preempted and added tothe end of the ready queue.
I If there are n processes in the ready queue and the time quantumis q, then each process gets 1/n of the CPU time in chunks of atmost q time units at once.
I No process waits more than (n − 1)q time units.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 28 / 50
RR Scheduling (1/3)
I Each process gets a small unit of CPU time (time quantum q),usually 10-100 milliseconds.
I After this time has elapsed, the process is preempted and added tothe end of the ready queue.
I If there are n processes in the ready queue and the time quantumis q, then each process gets 1/n of the CPU time in chunks of atmost q time units at once.
I No process waits more than (n − 1)q time units.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 28 / 50
RR Scheduling (1/3)
I Each process gets a small unit of CPU time (time quantum q),usually 10-100 milliseconds.
I After this time has elapsed, the process is preempted and added tothe end of the ready queue.
I If there are n processes in the ready queue and the time quantumis q, then each process gets 1/n of the CPU time in chunks of atmost q time units at once.
I No process waits more than (n − 1)q time units.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 28 / 50
RR Scheduling (2/3)
I Timer interrupts every quantum to schedule next process.
I Typically, higher average turnaround than SJF, but better responsetime.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 29 / 50
RR Scheduling (3/3)
I Time quantum q = 4
I The gantt chart for the schedule is:
I Average waiting time: (10−4)+4+73 = 5.66
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 30 / 50
RR Scheduling (3/3)
I Time quantum q = 4
I The gantt chart for the schedule is:
I Average waiting time: (10−4)+4+73 = 5.66
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 30 / 50
Time Quantum and Context Switch Time
I q large ⇒ FIFO
I q small⇒ q must be large with respect to context switch, otherwiseoverhead is too high.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 31 / 50
Time Quantum and Context Switch Time
I q large ⇒ FIFO
I q small⇒ q must be large with respect to context switch, otherwiseoverhead is too high.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 31 / 50
Turnaround Time Varies With The Time Quantum
I Turnaround time depends on the size of the time quantum.
I The average turnaround time can be improved if most processesfinish their next CPU burst in a single time quantum.
I 80% of CPU bursts should be shorter than q.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 32 / 50
Multilevel Queue Scheduling (1/3)
I Ready queue is partitioned into separate queues, e.g.:• foreground (interactive)• background (batch)
I Process permanently in a given queue.
I Each queue has its own scheduling algorithm:• foreground: RR• background: FCFS
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 34 / 50
Multilevel Queue Scheduling (1/3)
I Ready queue is partitioned into separate queues, e.g.:• foreground (interactive)• background (batch)
I Process permanently in a given queue.
I Each queue has its own scheduling algorithm:• foreground: RR• background: FCFS
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 34 / 50
Multilevel Queue Scheduling (1/3)
I Ready queue is partitioned into separate queues, e.g.:• foreground (interactive)• background (batch)
I Process permanently in a given queue.
I Each queue has its own scheduling algorithm:• foreground: RR• background: FCFS
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 34 / 50
Multilevel Queue Scheduling (2/3)
I Scheduling must be done between the queues:
• Fixed priority scheduling, i.e., serve all from foreground then frombackground: possibility of starvation.
• Time slice: each queue gets a certain amount of CPU time, which itcan schedule amongst its processes, i.e.,80% to foreground in RR.20% to background in FCFS.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 35 / 50
Multilevel Queue Scheduling (2/3)
I Scheduling must be done between the queues:
• Fixed priority scheduling, i.e., serve all from foreground then frombackground: possibility of starvation.
• Time slice: each queue gets a certain amount of CPU time, which itcan schedule amongst its processes, i.e.,80% to foreground in RR.20% to background in FCFS.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 35 / 50
Multilevel Queue Scheduling (3/3)
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 36 / 50
Multilevel Feedback QueueScheduling
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 37 / 50
Multilevel Feedback Queue
I A process can move between the various queues, e.g.,• Aging can be implemented this way• If a process uses too much CPU time, it will be moved to a
lower-priority queue.
I Multilevel-feedback-queue scheduler defined by the following param-eters:
• Number of queues.• Scheduling algorithms for each queue.• When to upgrade/demote a process.• Which queue a process will enter when that process needs service.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 38 / 50
Multilevel Feedback Queue
I A process can move between the various queues, e.g.,• Aging can be implemented this way• If a process uses too much CPU time, it will be moved to a
lower-priority queue.
I Multilevel-feedback-queue scheduler defined by the following param-eters:
• Number of queues.• Scheduling algorithms for each queue.• When to upgrade/demote a process.• Which queue a process will enter when that process needs service.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 38 / 50
Multilevel Feedback Queue
I A process can move between the various queues, e.g.,• Aging can be implemented this way• If a process uses too much CPU time, it will be moved to a
lower-priority queue.
I Multilevel-feedback-queue scheduler defined by the following param-eters:
• Number of queues.
• Scheduling algorithms for each queue.• When to upgrade/demote a process.• Which queue a process will enter when that process needs service.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 38 / 50
Multilevel Feedback Queue
I A process can move between the various queues, e.g.,• Aging can be implemented this way• If a process uses too much CPU time, it will be moved to a
lower-priority queue.
I Multilevel-feedback-queue scheduler defined by the following param-eters:
• Number of queues.• Scheduling algorithms for each queue.
• When to upgrade/demote a process.• Which queue a process will enter when that process needs service.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 38 / 50
Multilevel Feedback Queue
I A process can move between the various queues, e.g.,• Aging can be implemented this way• If a process uses too much CPU time, it will be moved to a
lower-priority queue.
I Multilevel-feedback-queue scheduler defined by the following param-eters:
• Number of queues.• Scheduling algorithms for each queue.• When to upgrade/demote a process.
• Which queue a process will enter when that process needs service.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 38 / 50
Multilevel Feedback Queue
I A process can move between the various queues, e.g.,• Aging can be implemented this way• If a process uses too much CPU time, it will be moved to a
lower-priority queue.
I Multilevel-feedback-queue scheduler defined by the following param-eters:
• Number of queues.• Scheduling algorithms for each queue.• When to upgrade/demote a process.• Which queue a process will enter when that process needs service.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 38 / 50
Multilevel Feedback Queue - Example
I For example, three queues:• Q0: RR with time quantum 8 milliseconds• Q1: RR time quantum 16 milliseconds• Q2: FCFS
I 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.
I At Q1 job is again served FCFS and receives 16 additional millisec-onds.
• If it still does not complete, it is preempted and moved to queue Q2.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 39 / 50
Multilevel Feedback Queue - Example
I For example, three queues:• Q0: RR with time quantum 8 milliseconds• Q1: RR time quantum 16 milliseconds• Q2: FCFS
I 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.
I At Q1 job is again served FCFS and receives 16 additional millisec-onds.
• If it still does not complete, it is preempted and moved to queue Q2.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 39 / 50
Multilevel Feedback Queue - Example
I For example, three queues:• Q0: RR with time quantum 8 milliseconds• Q1: RR time quantum 16 milliseconds• Q2: FCFS
I 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.
I At Q1 job is again served FCFS and receives 16 additional millisec-onds.
• If it still does not complete, it is preempted and moved to queue Q2.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 39 / 50
Linux Scheduling Through Version 2.5
I Prior to kernel version 2.5, ran variation of standard UNIX schedulingalgorithm.
I Version 2.5 moved to constant order O(1) scheduling time.
I Ran in constant time regardless of the number of tasks in the system.
I Preemptive, priority based
I Worked well, but poor response times for interactive processes.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 41 / 50
Linux Scheduling Through Version 2.5
I Prior to kernel version 2.5, ran variation of standard UNIX schedulingalgorithm.
I Version 2.5 moved to constant order O(1) scheduling time.
I Ran in constant time regardless of the number of tasks in the system.
I Preemptive, priority based
I Worked well, but poor response times for interactive processes.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 41 / 50
Linux Scheduling Through Version 2.5
I Prior to kernel version 2.5, ran variation of standard UNIX schedulingalgorithm.
I Version 2.5 moved to constant order O(1) scheduling time.
I Ran in constant time regardless of the number of tasks in the system.
I Preemptive, priority based
I Worked well, but poor response times for interactive processes.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 41 / 50
Linux Scheduling Through Version 2.5
I Prior to kernel version 2.5, ran variation of standard UNIX schedulingalgorithm.
I Version 2.5 moved to constant order O(1) scheduling time.
I Ran in constant time regardless of the number of tasks in the system.
I Preemptive, priority based
I Worked well, but poor response times for interactive processes.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 41 / 50
Linux Scheduling Through Version 2.5
I Prior to kernel version 2.5, ran variation of standard UNIX schedulingalgorithm.
I Version 2.5 moved to constant order O(1) scheduling time.
I Ran in constant time regardless of the number of tasks in the system.
I Preemptive, priority based
I Worked well, but poor response times for interactive processes.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 41 / 50
Linux Scheduling in Version 2.6.23+ (1/3)
I Completely Fair Scheduler (CFS)
I n users want to share a resource, e.g., CPU.• Solution: allocate each 1
n of the shared resource.
CPU
I Generalized by max-min fairness.• Handles if a user wants less than its fair share.• E.g., user 1 wants no more than 20%.
I Generalized by weighted max-min fairness.• Give weights to users according to importance.• E.g., user 1 gets weight 1, user 2 weight 2.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 42 / 50
Linux Scheduling in Version 2.6.23+ (1/3)
I Completely Fair Scheduler (CFS)
I n users want to share a resource, e.g., CPU.• Solution: allocate each 1
n of the shared resource.
CPU
I Generalized by max-min fairness.• Handles if a user wants less than its fair share.• E.g., user 1 wants no more than 20%.
I Generalized by weighted max-min fairness.• Give weights to users according to importance.• E.g., user 1 gets weight 1, user 2 weight 2.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 42 / 50
Linux Scheduling in Version 2.6.23+ (1/3)
I Completely Fair Scheduler (CFS)
I n users want to share a resource, e.g., CPU.• Solution: allocate each 1
n of the shared resource.
CPU
I Generalized by max-min fairness.• Handles if a user wants less than its fair share.• E.g., user 1 wants no more than 20%.
I Generalized by weighted max-min fairness.• Give weights to users according to importance.• E.g., user 1 gets weight 1, user 2 weight 2.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 42 / 50
Linux Scheduling in Version 2.6.23+ (2/3)
I Quantum calculated based on nice value from -20 to +19.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 43 / 50
Linux Scheduling in Version 2.6.23+ (3/3)
I To run the next task: the scheduler selects the highest-priority taskbelonging to the highest-priority scheduling class.
I Standard Linux kernels implement two scheduling classes:1 A default scheduling class using the CFS scheduling algorithm.2 A real-time scheduling class.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 44 / 50
Linux Scheduling in Version 2.6.23+ (3/3)
I To run the next task: the scheduler selects the highest-priority taskbelonging to the highest-priority scheduling class.
I Standard Linux kernels implement two scheduling classes:1 A default scheduling class using the CFS scheduling algorithm.2 A real-time scheduling class.
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 44 / 50
Modifying the Nice Value
I nice() increments a process’s nice value by inc and returns thenewly updated value.
I Only processes owned by root may provide a negative value for inc.
#include <unistd.h>
int nice(int inc);
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 45 / 50
Retrieving and Modifying Priorities
I The getpriority() and setpriority() system calls allow aprocess to retrieve and change its own nice value or that ofanother process.
#include <sys/resource.h>
int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int priority);
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 46 / 50
Example
I Returns the current process’s priority.
int ret;
ret = getpriority(PRIO_PROCESS, 0);
printf("nice value is %d\n", ret);
I Sets the priority of all processes in the current process group to 10.
int ret;
ret = setpriority(PRIO_PGRP, 0, 10);
if (ret == -1)
perror("setpriority");
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 47 / 50
Summary
I CPU scheduling
I Scheduling criteria: cpu utilization, throughput, turnaround time,waiting time, response time
I Scheduling algorithms• FCFS• SJF• Priority• RR• Multilevel• Multilevel feedback
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 49 / 50
Summary
I CPU scheduling
I Scheduling criteria: cpu utilization, throughput, turnaround time,waiting time, response time
I Scheduling algorithms• FCFS• SJF• Priority• RR• Multilevel• Multilevel feedback
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 49 / 50
Summary
I CPU scheduling
I Scheduling criteria: cpu utilization, throughput, turnaround time,waiting time, response time
I Scheduling algorithms• FCFS• SJF• Priority• RR• Multilevel• Multilevel feedback
Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/26 49 / 50