86
CPU Scheduling (Part II) Amir H. Payberah [email protected] Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 1 / 58

CPU Scheduling - Part2

Embed Size (px)

Citation preview

Page 1: CPU Scheduling - Part2

CPU Scheduling (Part II)

Amir H. [email protected]

Amirkabir University of Technology(Tehran Polytechnic)

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 1 / 58

Page 2: CPU Scheduling - Part2

Motivation

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 2 / 58

Page 3: CPU Scheduling - Part2

Reminder

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/28 3 / 58

Page 4: CPU Scheduling - Part2

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/28 4 / 58

Page 5: CPU Scheduling - Part2

Scheduling Criteria

I CPU utilization

I Throughput

I Turnaround time

I Waiting time

I Response time

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 5 / 58

Page 6: CPU Scheduling - Part2

Process 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/28 6 / 58

Page 7: CPU Scheduling - Part2

Thread Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 7 / 58

Page 8: CPU Scheduling - Part2

Thread Scheduling (1/2)

I Distinction between user-level and kernel-level threads.

I When threads supported by the OS, threads scheduled, not pro-cesses.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 8 / 58

Page 9: CPU Scheduling - Part2

Thread Scheduling (2/2)

I Process-Contention Scope (PCS)• In many-to-one and many-to-many models• Scheduling competition is within the process.

I System-Contention Scope (SCS)• In one-to-one model.• Scheduling competition among all threads in system.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 9 / 58

Page 10: CPU Scheduling - Part2

Thread Scheduling (2/2)

I Process-Contention Scope (PCS)• In many-to-one and many-to-many models• Scheduling competition is within the process.

I System-Contention Scope (SCS)• In one-to-one model.• Scheduling competition among all threads in system.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 9 / 58

Page 11: CPU Scheduling - Part2

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 10 / 58

Page 12: CPU Scheduling - Part2

Pthread Scheduling

I API allows specifying either PCS or SCS during thread creation.• PTHREAD SCOPE PROCESS schedules threads using PCS scheduling.• PTHREAD SCOPE SYSTEM schedules threads using SCS scheduling.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 11 / 58

Page 13: CPU Scheduling - Part2

Contention Scope

I pthread attr setscope and pthread attr getscope set/getcontention scope attribute in thread attributes object.

#include <pthread.h>

int pthread_attr_setscope(pthread_attr_t *attr, int scope);

int pthread_attr_getscope(const pthread_attr_t *attr, int *scope);

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 12 / 58

Page 14: CPU Scheduling - Part2

Pthread Scheduling API (1/2)

#include <pthread.h>

#include <stdio.h>

#define NUM_THREADS 5

int main(int argc, char *argv[]) {

int i, scope;

pthread_t tid[NUM THREADS];

pthread_attr_t attr;

/* get the default attributes */

pthread_attr_init(&attr);

/* first inquire on the current scope */

if (pthread_attr_getscope(&attr, &scope) != 0)

fprintf(stderr, "Unable to get scheduling scope\n");

else {

if (scope == PTHREAD_SCOPE_PROCESS)

printf("PTHREAD_SCOPE_PROCESS");

else if (scope == PTHREAD_SCOPE_SYSTEM)

printf("PTHREAD_SCOPE_SYSTEM");

else

fprintf(stderr, "Illegal scope value.\n");

}

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 13 / 58

Page 15: CPU Scheduling - Part2

Pthread Scheduling API (2/2)

/* set the scheduling algorithm to PCS or SCS */

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

/* create the threads */

for (i = 0; i < NUM_THREADS; i++)

pthread_create(&tid[i], &attr, runner, NULL);

/* now join on each thread */

for (i = 0; i < NUM_THREADS; i++)

pthread_join(tid[i], NULL);

}

/* Each thread will begin control in this function */

void *runner(void *param) {

/* do some work ... */

pthread_exit(0);

}

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 14 / 58

Page 16: CPU Scheduling - Part2

Multi-Processor Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 15 / 58

Page 17: CPU Scheduling - Part2

Multiple-Processor Scheduling

I CPU scheduling is more complex when multiple CPUs are available.

I Asymmetric multiprocessing• Only one processor does all scheduling decisions, I/O processing,

and other system activities.• The other processors execute only user code.

I Symmetric multiprocessing (SMP)• Each processor is self-scheduling• All processes in common ready queue, or each has its own private

queue of ready processes.• Currently, the most common.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 16 / 58

Page 18: CPU Scheduling - Part2

Multiple-Processor Scheduling

I CPU scheduling is more complex when multiple CPUs are available.

I Asymmetric multiprocessing• Only one processor does all scheduling decisions, I/O processing,

and other system activities.• The other processors execute only user code.

I Symmetric multiprocessing (SMP)• Each processor is self-scheduling• All processes in common ready queue, or each has its own private

queue of ready processes.• Currently, the most common.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 16 / 58

Page 19: CPU Scheduling - Part2

Multiple-Processor Scheduling

I CPU scheduling is more complex when multiple CPUs are available.

I Asymmetric multiprocessing• Only one processor does all scheduling decisions, I/O processing,

and other system activities.• The other processors execute only user code.

I Symmetric multiprocessing (SMP)• Each processor is self-scheduling• All processes in common ready queue, or each has its own private

queue of ready processes.• Currently, the most common.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 16 / 58

Page 20: CPU Scheduling - Part2

Processor Affinity

I What happens to cache memory when a process has been runningon a specific processor, and then, the process migrates to anotherprocessor?

I Invalidating and repopulating caches is costly.

I Processor affinity: keep a process running on the same processor.

• Soft affinity: the OS attempts to keep a process on a singleprocessor, but it is possible for a process to migrate betweenprocessors.

• Hard affinity: allowing a process to specify a subset of processors onwhich it may run.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 17 / 58

Page 21: CPU Scheduling - Part2

Processor Affinity

I What happens to cache memory when a process has been runningon a specific processor, and then, the process migrates to anotherprocessor?

I Invalidating and repopulating caches is costly.

I Processor affinity: keep a process running on the same processor.

• Soft affinity: the OS attempts to keep a process on a singleprocessor, but it is possible for a process to migrate betweenprocessors.

• Hard affinity: allowing a process to specify a subset of processors onwhich it may run.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 17 / 58

Page 22: CPU Scheduling - Part2

Processor Affinity

I What happens to cache memory when a process has been runningon a specific processor, and then, the process migrates to anotherprocessor?

I Invalidating and repopulating caches is costly.

I Processor affinity: keep a process running on the same processor.

• Soft affinity: the OS attempts to keep a process on a singleprocessor, but it is possible for a process to migrate betweenprocessors.

• Hard affinity: allowing a process to specify a subset of processors onwhich it may run.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 17 / 58

Page 23: CPU Scheduling - Part2

Processor Affinity

I What happens to cache memory when a process has been runningon a specific processor, and then, the process migrates to anotherprocessor?

I Invalidating and repopulating caches is costly.

I Processor affinity: keep a process running on the same processor.• Soft affinity: the OS attempts to keep a process on a single

processor, but it is possible for a process to migrate betweenprocessors.

• Hard affinity: allowing a process to specify a subset of processors onwhich it may run.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 17 / 58

Page 24: CPU Scheduling - Part2

Processor Affinity

I What happens to cache memory when a process has been runningon a specific processor, and then, the process migrates to anotherprocessor?

I Invalidating and repopulating caches is costly.

I Processor affinity: keep a process running on the same processor.• Soft affinity: the OS attempts to keep a process on a single

processor, but it is possible for a process to migrate betweenprocessors.

• Hard affinity: allowing a process to specify a subset of processors onwhich it may run.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 17 / 58

Page 25: CPU Scheduling - Part2

NUMA and CPU Scheduling

I Non-Uniform Memory Access (NUMA): a CPU has faster access tosome parts of main memory than to other parts.

I Systems containing combined CPU and memory boards.

I A process that is assigned affinity to a particular CPU can be allo-cated memory on the board where that CPU resides.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 18 / 58

Page 26: CPU Scheduling - Part2

NUMA and CPU Scheduling

I Non-Uniform Memory Access (NUMA): a CPU has faster access tosome parts of main memory than to other parts.

I Systems containing combined CPU and memory boards.

I A process that is assigned affinity to a particular CPU can be allo-cated memory on the board where that CPU resides.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 18 / 58

Page 27: CPU Scheduling - Part2

NUMA and CPU Scheduling

I Non-Uniform Memory Access (NUMA): a CPU has faster access tosome parts of main memory than to other parts.

I Systems containing combined CPU and memory boards.

I A process that is assigned affinity to a particular CPU can be allo-cated memory on the board where that CPU resides.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 18 / 58

Page 28: CPU Scheduling - Part2

Load Balancing

I If SMP, need to keep all CPUs loaded for efficiency.

I Load balancing attempts to keep workload evenly distributed.

I Push migration: periodic task checks load on each processor, and iffound pushes task from overloaded CPU to other CPUs.

I Pull migration: idle processors pulls waiting task from busy proces-sor.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 19 / 58

Page 29: CPU Scheduling - Part2

Load Balancing

I If SMP, need to keep all CPUs loaded for efficiency.

I Load balancing attempts to keep workload evenly distributed.

I Push migration: periodic task checks load on each processor, and iffound pushes task from overloaded CPU to other CPUs.

I Pull migration: idle processors pulls waiting task from busy proces-sor.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 19 / 58

Page 30: CPU Scheduling - Part2

Load Balancing

I If SMP, need to keep all CPUs loaded for efficiency.

I Load balancing attempts to keep workload evenly distributed.

I Push migration: periodic task checks load on each processor, and iffound pushes task from overloaded CPU to other CPUs.

I Pull migration: idle processors pulls waiting task from busy proces-sor.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 19 / 58

Page 31: CPU Scheduling - Part2

Load Balancing

I If SMP, need to keep all CPUs loaded for efficiency.

I Load balancing attempts to keep workload evenly distributed.

I Push migration: periodic task checks load on each processor, and iffound pushes task from overloaded CPU to other CPUs.

I Pull migration: idle processors pulls waiting task from busy proces-sor.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 19 / 58

Page 32: CPU Scheduling - Part2

Multicore Processors

I Place multiple processor cores on same physical chip.

I Faster and consumes less power.

I Memory stall: when a processor accesses memory, it spends a sig-nificant amount of time waiting for the data to become available.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 20 / 58

Page 33: CPU Scheduling - Part2

Multithreaded Multicore System

I Multiple threads per core also growing.• Takes advantage of memory stall to make progress on another

thread while memory retrieve happens.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 21 / 58

Page 34: CPU Scheduling - Part2

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 22 / 58

Page 35: CPU Scheduling - Part2

CPU Affinity

I sched setaffinity() and sched getaffinity() sets/gets theCPU affinity of the process specified by pid.

#define _GNU_SOURCE

#include <sched.h>

int sched_setaffinity(pid_t pid, size_t len, cpu_set_t *set);

int sched_getaffinity(pid_t pid, size_t len, cpu_set_t *set);

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 23 / 58

Page 36: CPU Scheduling - Part2

CPU Affinity Macros

I CPU ZERO() initializes set to be empty.

I CPU SET() adds the CPU cpu to set.

I CPU CLR() removes the CPU cpu from set.

I CPU ISSET() returns true if the CPU cpu is a member of set.

#define _GNU_SOURCE

#include <sched.h>

void CPU_ZERO(cpu_set_t *set);

void CPU_SET(int cpu, cpu_set_t *set);

void CPU_CLR(int cpu, cpu_set_t *set);

int CPU_ISSET(int cpu, cpu_set_t *set);

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 24 / 58

Page 37: CPU Scheduling - Part2

CPU Affinity Macros

I The process identified by pid runs on any CPU other than the firstCPU of a four-processor system.

cpu_set_t set;

CPU_ZERO(&set);

CPU_SET(1, &set);

CPU_SET(2, &set);

CPU_SET(3, &set);

sched_setaffinity(pid, sizeof(set), &set);

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 25 / 58

Page 38: CPU Scheduling - Part2

Real-Time CPU Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 26 / 58

Page 39: CPU Scheduling - Part2

Minimizing Latency

I When an event occurs, the system must respond to and service itas quickly as possible.

I Event latency: the amount of time that elapses from when an eventoccurs to when it is serviced.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 27 / 58

Page 40: CPU Scheduling - Part2

Minimizing Latency

I When an event occurs, the system must respond to and service itas quickly as possible.

I Event latency: the amount of time that elapses from when an eventoccurs to when it is serviced.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 27 / 58

Page 41: CPU Scheduling - Part2

Soft vs. Hard Real-Time

I Soft real-time• No guarantee as to when a critical real-time process will be

scheduled.• They guarantee only that the process will be given preference over

noncritical processes.

I Hard real-time• The task must be serviced by its deadline.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 28 / 58

Page 42: CPU Scheduling - Part2

Soft vs. Hard Real-Time

I Soft real-time• No guarantee as to when a critical real-time process will be

scheduled.• They guarantee only that the process will be given preference over

noncritical processes.

I Hard real-time• The task must be serviced by its deadline.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 28 / 58

Page 43: CPU Scheduling - Part2

Real-Time CPU Scheduling (1/2)

I Two types of latencies affect performance:1 Interrupt latency: time from arrival of interrupt to start of routine

that services interrupt.

2 Dispatch latency: time for schedule to take current process off CPUand switch to another.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 29 / 58

Page 44: CPU Scheduling - Part2

Real-Time CPU Scheduling (1/2)

I Two types of latencies affect performance:1 Interrupt latency: time from arrival of interrupt to start of routine

that services interrupt.2 Dispatch latency: time for schedule to take current process off CPU

and switch to another.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 29 / 58

Page 45: CPU Scheduling - Part2

Real-Time CPU Scheduling (2/2)

I Conflict phase of dispatch latency:1 Preemption of any process running in kernel mode.2 Release by low-priority process of resources needed by high-priority

processes.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 30 / 58

Page 46: CPU Scheduling - Part2

Periodic Processes

I Periodic processes require CPU at constant intervals.• Processing time t, deadline d , period p• 0 ≤ t ≤ d ≤ p• Rate of periodic task is 1

p

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 31 / 58

Page 47: CPU Scheduling - Part2

Priority-Based Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 32 / 58

Page 48: CPU Scheduling - Part2

Priority-Based Scheduling

I For real-time scheduling, scheduler must support preemptive andpriority-based scheduling.

I Only guarantees soft real-time.

I For hard real-time must also provide ability to meet deadlines.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 33 / 58

Page 49: CPU Scheduling - Part2

Priority-Based Scheduling

I For real-time scheduling, scheduler must support preemptive andpriority-based scheduling.

I Only guarantees soft real-time.

I For hard real-time must also provide ability to meet deadlines.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 33 / 58

Page 50: CPU Scheduling - Part2

Priority-Based Scheduling

I For real-time scheduling, scheduler must support preemptive andpriority-based scheduling.

I Only guarantees soft real-time.

I For hard real-time must also provide ability to meet deadlines.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 33 / 58

Page 51: CPU Scheduling - Part2

Rate-Monotonic Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 34 / 58

Page 52: CPU Scheduling - Part2

Rate-Monotonic Scheduling

I A priority is assigned based on the inverse of its period.

I Shorter periods = higher priority

I Longer periods = lower priority

I Assign a higher priority to tasks that require the CPU more often.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 35 / 58

Page 53: CPU Scheduling - Part2

Rate-Monotonic Scheduling

I A priority is assigned based on the inverse of its period.

I Shorter periods = higher priority

I Longer periods = lower priority

I Assign a higher priority to tasks that require the CPU more often.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 35 / 58

Page 54: CPU Scheduling - Part2

Rate-Monotonic Scheduling

I A priority is assigned based on the inverse of its period.

I Shorter periods = higher priority

I Longer periods = lower priority

I Assign a higher priority to tasks that require the CPU more often.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 35 / 58

Page 55: CPU Scheduling - Part2

Rate-Monotonic Example 1 (1/4)

I Two processes: P1 and P2

I p1 = 50 and p2 = 100

I t1 = 20 and t2 = 35

I d1 = d2 = complete its CPU burst by the start of its next period

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 36 / 58

Page 56: CPU Scheduling - Part2

Rate-Monotonic Example 1 (2/4)

I Is it possible to schedule these tasks so that each meets its deadlines?

I Measure the CPU utilization: tipi

I t1p1

= 2050 = 0.4

I t2p2

= 35100 = 0.35

I 40% + 35% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 37 / 58

Page 57: CPU Scheduling - Part2

Rate-Monotonic Example 1 (2/4)

I Is it possible to schedule these tasks so that each meets its deadlines?

I Measure the CPU utilization: tipi

I t1p1

= 2050 = 0.4

I t2p2

= 35100 = 0.35

I 40% + 35% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 37 / 58

Page 58: CPU Scheduling - Part2

Rate-Monotonic Example 1 (2/4)

I Is it possible to schedule these tasks so that each meets its deadlines?

I Measure the CPU utilization: tipi

I t1p1

= 2050 = 0.4

I t2p2

= 35100 = 0.35

I 40% + 35% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 37 / 58

Page 59: CPU Scheduling - Part2

Rate-Monotonic Example 1 (2/4)

I Is it possible to schedule these tasks so that each meets its deadlines?

I Measure the CPU utilization: tipi

I t1p1

= 2050 = 0.4

I t2p2

= 35100 = 0.35

I 40% + 35% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 37 / 58

Page 60: CPU Scheduling - Part2

Rate-Monotonic Example 1 (3/4)

I Suppose we assign P2 a higher priority than P1.• P1 misses its deadline.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 38 / 58

Page 61: CPU Scheduling - Part2

Rate-Monotonic Example 1 (4/4)

I Suppose we assign P1 a higher priority than P2.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 39 / 58

Page 62: CPU Scheduling - Part2

Rate-Monotonic Example 2 (1/3)

I Two processes: P1 and P2

I p1 = 50 and p2 = 80

I t1 = 25 and t2 = 35

I d1 = d2 = complete its CPU burst by the start of its next period

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 40 / 58

Page 63: CPU Scheduling - Part2

Rate-Monotonic Example 2 (2/3)

I Is it possible to schedule these tasks so that each meets its deadlines?

I t1p1

= 2550 = 0.5

I t2p2

= 3580 = 0.44

I 50% + 44% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 41 / 58

Page 64: CPU Scheduling - Part2

Rate-Monotonic Example 2 (2/3)

I Is it possible to schedule these tasks so that each meets its deadlines?

I t1p1

= 2550 = 0.5

I t2p2

= 3580 = 0.44

I 50% + 44% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 41 / 58

Page 65: CPU Scheduling - Part2

Rate-Monotonic Example 2 (3/3)

I Suppose we assign P1 a higher priority than P2.• P2 misses its deadline.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 42 / 58

Page 66: CPU Scheduling - Part2

Earliest-Deadline-First (EDF)Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 43 / 58

Page 67: CPU Scheduling - Part2

Earliest Deadline First Scheduling

I Priorities are assigned according to deadlines.

I The earlier the deadline, the higher the priority.

I The later the deadline, the lower the priority.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 44 / 58

Page 68: CPU Scheduling - Part2

EDF Example (1/3)

I Two processes: P1 and P2

I p1 = 50 and p2 = 80

I t1 = 25 and t2 = 35

I d1 = d2 = complete its CPU burst by the start of its next period

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 45 / 58

Page 69: CPU Scheduling - Part2

EDF Example (2/3)

I Is it possible to schedule these tasks so that each meets its deadlines?

I t1p1

= 2550 = 0.5

I t2p2

= 3580 = 0.44

I 50% + 44% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 46 / 58

Page 70: CPU Scheduling - Part2

EDF Example (2/3)

I Is it possible to schedule these tasks so that each meets its deadlines?

I t1p1

= 2550 = 0.5

I t2p2

= 3580 = 0.44

I 50% + 44% < 100%

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 46 / 58

Page 71: CPU Scheduling - Part2

EDF Example (3/3)

I Suppose we assign P1 a higher priority than P2.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 47 / 58

Page 72: CPU Scheduling - Part2

Proportional Share Scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 48 / 58

Page 73: CPU Scheduling - Part2

Proportional Share Scheduling

I T shares are allocated among all processes in the system.

I An application receives N shares where N < T .

I This ensures each application will receive NT of the total processor

time.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 49 / 58

Page 74: CPU Scheduling - Part2

Proportional Share Example

I Three processes: P1, P2, and P3

I T = 100

I P1 is assigned 50, P2 is assigned 15, and P3 is assigned 20.

I P1 will have 50% of total processor time, P2 will have 15%, and P3

will have 20%.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 50 / 58

Page 75: CPU Scheduling - Part2

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 51 / 58

Page 76: CPU Scheduling - Part2

POSIX Real-Time Scheduling

I API provides functions for managing real-time threads/processes:

I Defines two scheduling classes for real-time threads:1 SCHED FIFO: scheduled using a FCFS strategy with a FIFO queue.

There is no time-slicing for threads/processes of equal priority.2 SCHED RR: similar to SCHED FIFO except time-slicing occurs for

threads/processes of equal priority.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 52 / 58

Page 77: CPU Scheduling - Part2

POSIX Real-Time Scheduling

I API provides functions for managing real-time threads/processes:

I Defines two scheduling classes for real-time threads:1 SCHED FIFO: scheduled using a FCFS strategy with a FIFO queue.

There is no time-slicing for threads/processes of equal priority.2 SCHED RR: similar to SCHED FIFO except time-slicing occurs for

threads/processes of equal priority.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 52 / 58

Page 78: CPU Scheduling - Part2

Setting the Linux Scheduling Policy

I sched getscheduler() and sched setscheduler() manipulatethe scheduling policy.

#include <sched.h>

struct sched_param {

/* ... */

int sched_priority;

/* ... */

};

int sched_getscheduler(pid_t pid);

int sched_setscheduler(pid_t pid, int policy, const struct sched_param *sp);

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 53 / 58

Page 79: CPU Scheduling - Part2

Get Priority

int policy;

/* get our scheduling policy */

policy = sched_getscheduler(0);

switch(policy) {

case SCHED_OTHER:

printf("Policy is normal\n");

break;

case SCHED_RR:

printf("Policy is round-robin\n");

break;

case SCHED_FIFO:

printf("Policy is first-in, first-out\n");

break;

case -1:

perror("sched_getscheduler");

break;

default:

fprintf(stderr, "Unknown policy!\n");

}

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 54 / 58

Page 80: CPU Scheduling - Part2

Set Priority

struct sched_param sp = { .sched_priority = 1 };

int ret;

ret = sched_setscheduler(0, SCHED_RR, &sp);

if (ret == -1) {

perror("sched_setscheduler");

return 1;

}

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 55 / 58

Page 81: CPU Scheduling - Part2

Summary

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 56 / 58

Page 82: CPU Scheduling - Part2

Summary

I Thread scheduling: PCS and SCS

I Multi-processor scheduling: SMP, processor affinity, load balancing

I Real-time scheduling: soft and hard real times

I Real-time scheduling algorithms• Priority-based• Rate-monotonic• Earliest-deadline-first• Proportional share scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 57 / 58

Page 83: CPU Scheduling - Part2

Summary

I Thread scheduling: PCS and SCS

I Multi-processor scheduling: SMP, processor affinity, load balancing

I Real-time scheduling: soft and hard real times

I Real-time scheduling algorithms• Priority-based• Rate-monotonic• Earliest-deadline-first• Proportional share scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 57 / 58

Page 84: CPU Scheduling - Part2

Summary

I Thread scheduling: PCS and SCS

I Multi-processor scheduling: SMP, processor affinity, load balancing

I Real-time scheduling: soft and hard real times

I Real-time scheduling algorithms• Priority-based• Rate-monotonic• Earliest-deadline-first• Proportional share scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 57 / 58

Page 85: CPU Scheduling - Part2

Summary

I Thread scheduling: PCS and SCS

I Multi-processor scheduling: SMP, processor affinity, load balancing

I Real-time scheduling: soft and hard real times

I Real-time scheduling algorithms• Priority-based• Rate-monotonic• Earliest-deadline-first• Proportional share scheduling

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 57 / 58

Page 86: CPU Scheduling - Part2

Questions?

Acknowledgements

Some slides were derived from Avi Silberschatz slides.

Amir H. Payberah (Tehran Polytechnic) CPU Scheduling 1393/7/28 58 / 58