04 Process Part3
04 Process Part3
Amir H. Payberah
[email protected]
Nov. 8, 2023
CPU Scheduling
1 / 45
CPU Scheduling
2 / 45
CPU Scheduling
2 / 45
Basic Concepts
3 / 45
Basic Concepts
I Others must wait until the CPU is free and can be rescheduled.
3 / 45
Basic Concepts
I Others must wait until the CPU is free and can be rescheduled.
3 / 45
Basic Concepts
4 / 45
Basic Concepts
4 / 45
Basic Concepts
4 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
CPU Scheduler
I CPU scheduler selects from among the processes in ready queue, and
allocates the CPU to one of them.
5 / 45
Scheduling Criteria
6 / 45
Scheduling Criteria
6 / 45
Scheduling Criteria
6 / 45
Scheduling Criteria
6 / 45
Scheduling Criteria
I Waiting time: amount of time a process has been waiting in the ready
queue (Min).
6 / 45
Scheduling Criteria
I Waiting time: amount of time a process has been waiting in the ready
queue (Min).
I Response time: amount of time it takes from when a request was sub-
mitted until the first response is produced (Min).
6 / 45
Scheduling Algorithms
7 / 45
Scheduling Algorithms
I Shortest-Job-First Scheduling
I Priority Scheduling
I Round-Robin Scheduling
8 / 45
First-Come, First-Served (FCFS)
Scheduling
9 / 45
FCFS Scheduling (1/2)
10 / 45
FCFS Scheduling (1/2)
10 / 45
FCFS Scheduling (1/2)
10 / 45
FCFS Scheduling (1/2)
10 / 45
FCFS Scheduling (1/2)
I Convoy effect: all the other processes wait for the one big process to get
off the CPU.
10 / 45
FCFS Scheduling (2/2)
11 / 45
FCFS Scheduling (2/2)
11 / 45
FCFS Scheduling (2/2)
11 / 45
FCFS Scheduling (2/2)
11 / 45
Shortest-Job-First (SJF)
Scheduling
12 / 45
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.
13 / 45
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.
I Use these lengths to schedule the process with the shortest time.
13 / 45
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.
I Use these lengths to schedule the process with the shortest time.
I SJF is optimal: gives minimum average waiting time for a given set of
processes.
13 / 45
SJF Scheduling (1/2)
I Associate with each process the length of its next CPU burst.
I Use these lengths to schedule the process with the shortest time.
I SJF is optimal: gives minimum average waiting time for a given set of
processes.
13 / 45
SJF Scheduling (2/2)
14 / 45
SJF Scheduling (2/2)
14 / 45
SJF Scheduling (2/2)
14 / 45
SJF Scheduling (2/2)
14 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
1. tn = actual length of nth CPU burst
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
1. tn = actual length of nth CPU burst
2. τn+1 = predeicted value for the next CPU burst
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
1. tn = actual length of nth CPU burst
2. τn+1 = predeicted value for the next CPU burst
3. τn+1 = αtn + (1 − α)τn , where 0 ≤ α ≤ 1
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
1. tn = actual length of nth CPU burst
2. τn+1 = predeicted value for the next CPU burst
3. τn+1 = αtn + (1 − α)τn , where 0 ≤ α ≤ 1
I α = 0 then τn+1 = τ
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
1. tn = actual length of nth CPU burst
2. τn+1 = predeicted value for the next CPU burst
3. τn+1 = αtn + (1 − α)τn , where 0 ≤ α ≤ 1
I α = 0 then τn+1 = τ
I α = 1 then τn+1 = tn
15 / 45
Determining Length of Next CPU Burst
I Estimate the length, and pick process with shortest predicted next CPU
burst.
I The next CPU burst
1. tn = actual length of nth CPU burst
2. τn+1 = predeicted value for the next CPU burst
3. τn+1 = αtn + (1 − α)τn , where 0 ≤ α ≤ 1
I α = 0 then τn+1 = τ
I α = 1 then τn+1 = tn
1
I Commonly, α set to 2
15 / 45
Preemptive SJF
16 / 45
Preemptive SJF
16 / 45
Example of Shortest-Remaining-Time-First
I Now we add the concepts of varying arrival times and preemption to the
analysis.
17 / 45
Example of Shortest-Remaining-Time-First
I Now we add the concepts of varying arrival times and preemption to the
analysis.
17 / 45
Example of Shortest-Remaining-Time-First
I Now we add the concepts of varying arrival times and preemption to the
analysis.
(10−1)+(1−1)+(17−2)+(5−3) 26
I Average waiting time: 4 = 4 = 6.5
17 / 45
Priority Scheduling
18 / 45
Priority Scheduling (1/2)
19 / 45
Priority Scheduling (1/2)
19 / 45
Priority Scheduling (1/2)
19 / 45
Priority Scheduling (1/2)
19 / 45
Priority Scheduling (1/2)
19 / 45
Priority Scheduling (2/2)
20 / 45
Priority Scheduling (2/2)
20 / 45
Priority Scheduling (2/2)
0+1+6+16+18
I Average waiting time: 5 = 8.2
20 / 45
Round-Robin (RR)
Scheduling
21 / 45
RR Scheduling (1/2)
I Each process gets a small unit of CPU time (time quantum q), usually
10-100 milliseconds.
22 / 45
RR Scheduling (1/2)
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 to the
end of the ready queue.
22 / 45
RR Scheduling (1/2)
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 to the
end of the ready queue.
I 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.
22 / 45
RR Scheduling (1/2)
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 to the
end of the ready queue.
I 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.
22 / 45
RR Scheduling (2/2)
I Time quantum q = 4
23 / 45
RR Scheduling (2/2)
I Time quantum q = 4
23 / 45
RR Scheduling (2/2)
I Time quantum q = 4
(10−4)+4+7
I Average waiting time: 3 = 5.66
23 / 45
Time Quantum and Context Switch Time (1/2)
24 / 45
Time Quantum and Context Switch Time (1/2)
I Performance
• q large ⇒ FIFO
• q small ⇒ q must be large with respect to context switch, otherwise
overhead is too high.
24 / 45
Time Quantum and Context Switch Time (2/2)
25 / 45
26 / 45
Linux Scheduling (1/2)
I Completely Fair Scheduler (CFS)
CPU
I n users want to share a resource, e.g., CPU.
1
• Solution: allocate each n of the shared resource.
27 / 45
Linux Scheduling (1/2)
I Completely Fair Scheduler (CFS)
CPU
I n users want to share a resource, e.g., CPU.
1
• Solution: allocate each n of the shared resource.
27 / 45
Linux Scheduling (1/2)
I Completely Fair Scheduler (CFS)
CPU
I n users want to share a resource, e.g., CPU.
1
• Solution: allocate each n of the shared resource.
27 / 45
Linux Scheduling (2/2)
28 / 45
Modifying the Nice Value
I nice() increments a process’s nice value by inc and returns the newly
updated value.
I Only processes owned by root may provide a negative value for inc.
#include <unistd.h>
29 / 45
Retrieving and Modifying Priorities
#include <sys/resource.h>
30 / 45
Thread Scheduling
31 / 45
Thread Scheduling (1/2)
32 / 45
Thread Scheduling (1/2)
32 / 45
33 / 45
Pthread Scheduling
34 / 45
Pthread Scheduling
#include <pthread.h>
34 / 45
Pthread Scheduling API
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
35 / 45
Multi-Processor Scheduling
36 / 45
Multiple-Processor Scheduling
I Asymmetric multiprocessing
37 / 45
Multiple-Processor Scheduling
I Asymmetric multiprocessing
• Only one processor does all scheduling decisions, I/O processing, and
other system activities.
• The other processors execute only user code.
37 / 45
Multiple-Processor Scheduling
I Asymmetric multiprocessing
• Only one processor does all scheduling decisions, I/O processing, and
other system activities.
• The other processors execute only user code.
37 / 45
Processor Affinity
38 / 45
Processor Affinity
38 / 45
Processor Affinity
38 / 45
39 / 45
CPU Affinity
#define _GNU_SOURCE
#include <sched.h>
40 / 45
CPU Affinity Macros
#define _GNU_SOURCE
#include <sched.h>
41 / 45
CPU Affinity Macros
I The process identified by pid runs on any CPU other than the first
CPU of a four-processor system.
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(1, &set);
CPU_SET(2, &set);
CPU_SET(3, &set);
42 / 45
Summary
43 / 45
Summary
I CPU scheduling
44 / 45
Summary
I CPU scheduling
44 / 45
Summary
I CPU scheduling
44 / 45
Summary
I CPU scheduling
44 / 45
Summary
I CPU scheduling
44 / 45
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
45 / 45