28
Dr. Hugh Melvin, Dept. of IT, NUI,G 1 Process Scheduling for RTS

Process Scheduling for RTS

Embed Size (px)

DESCRIPTION

Process Scheduling for RTS. RTS Scheduling Approach. RTS typically control multiple parameters concurrently Eg. Flight Control System Speed, altitude, inclination etc.. Options Use a single process Simple/limited approach but can be dangerous No OS required Use multiple processes - PowerPoint PPT Presentation

Citation preview

Page 1: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 1

Process Scheduling for RTS

Page 2: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 2

RTS Scheduling Approach

• RTS typically control multiple parameters concurrently– Eg. Flight Control System

• Speed, altitude, inclination etc..

• Options– Use a single process

• Simple/limited approach but can be dangerous• No OS required

– Use multiple processes• More complex but more robust

– Use threads• Light weight relative to processes

Page 3: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 3

Cyclic Executive Approach• Single Process

while(1){

Task 1;

Task 2;

..

..

Task n;

}

• Encapsulate all tasks within single infinite loop

Page 4: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 4

Cyclic Executive• Ok for very well defined / periodic tasks with bounded execution

times• Need to ensure that tasks cannot block and halt all others• May need to slow it down to meet particular task reqds

– Eg. Above example will run as fast as processor can handle tasks • Possible Strategy

– Run as fast as required by highest freq task– Use lower harmonics for remaining tasks

• Eg. Highest freq Task is 100 Hz – Other tasks at 50Hz, 25 Hz etc

• Possible use counters to control sequence– Possible use of timers to sleep

• Limited scope for aperiodic tasks eg. Interrupts• Hardware specificvery limited portability

Page 5: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 5

Cyclic Executive

• AS Example– Cyclic executive approach– Each AS station handles well defined tasks– Execution time of each task precisely known– Task schedule customised s.t.total task

runtime will facilitate/meet response time– Slack time in each cycle built in for interrupt

processing• If total task time exceeded repeatedly, timeout fault

Page 6: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 6

Multiple Process Approach• Each process is much more simple as handling a small

subset of overall system• Overall system

– More robust and reliable – More scalable

• Can run on multiprocessor machine– Modular

• Process code more portable– Protected

• Each process operates in isolation with dedicated memory less risk of overall system failure

• Downside– Requires an OS to schedule multiple processes– More memory reqd for process overhead– Communication between processes has to be explicitly done

(advantage!)

Page 7: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 7

Multiple Process• Generic Process States

– Dormant : Process created but not eligible to execute– Ready: Process released and eligible to execute but

not doing so• May be preempted• May have been blocked for resources move to ready• May have reached end of timeslice

– Executing: Process being executed– Suspended (Blocked): Process waiting for resource to

be freed or self-suspended– Terminated: Process finished execution

Page 8: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 8

Process State Diagram

Ready

Dormant Terminated

Blocked

Executing

Schedule Task

Pre-empt or Timeslice allocated

Blocked/ Self suspended

Resource freed

Resource freed

Preempted, timeslice up

Process terminated

Page 9: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 9

Scheduling for RTS• Fundamental OS function• More critical for RTS

– Allocating resources and scheduling tasks to ensure that deadlines are met

• Schedule is feasible iff all the tasks start after their release time and complete before their deadlines

• Scheduling Policy may be determined– Pre-run-time

• Schedule created offline• Not unlike Cyclic Executive approach

– Run-time• Schedule determined online as tasks arrive

– Needs to be done quickly!

Page 10: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 10

Scheduling for RTS• Static versus Dynamic Priority

– Static Priority Scheduling Alg.• Task priority does not change

– Rate Monotonic Alg.

– Dynamic Priority Scheduling Alg.• Priority can change over time

– Earliest Deadline First (EDF)

• Preemptive versus non-preemptive– Preemptive Schedule

• Task can be preempted by other tasks• Penalty of context switches

– Non preemptive• Task runs to completion unless blocked over resource

Page 11: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 11

Task Characteristics Ti

• Precedence Constraints – Other tasks reqd before task Ti can run

• Release Time of task Ti = ri

• Phase Φi of task Ti = release time of 1st instance of task Ti

• Execution time ei

of task Ti = time (worst case) for task to complete• Period pi

= interval between consecutive instances of task Ti (presuming periodic)• Absolute Deadline Di

= instant by which task must be complete• Relative Deadline di

.. Relative to release time ri

Page 12: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 12

Task Characteristics Ti

pi

ri

ei

Φi

di

Di

Page 13: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 13

Simplifications• All tasks are periodic

• Relative deadline is equal to period

• No precedence constraints

• No task has any non-preemptible sections

• Cost of preemption is zero

• Non CPU resources are infinite eg. Memory / I/O

limited use in the real world!

Page 14: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 14

Rate Monotonic Scheduling• Run time scheduling, Static priority and Preemptive• Priority inversely related to period

• Eg. Given task Ti and Tj where pi < pj

– Priority of task Ti greater than Tj

• Scheduling decision made when– Current task execution complete– New task released

• Recall: In real world, RTS tasks with higher freq typically require shortest response times and are the most critical

• Recall task Ti utilisation ui = ei / pi

– Overall CPU Utilisation U =

n

iiu

1

Page 15: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 15

RM Example

Task e p u

T1 1 4 0.25

T2 2 5 0.4

T3 5 20 0.25

All Tasks released at time 0 Priority T1 > T2 > T3

Overall U = 0.9Sequence1st inst Task 1 runs to completion1st inst Task 2 runs to completion1st inst Task 3 runs for 1 unit ..at Eu=4, Task 1 released preempts Task 32nd inst Task 1 runs to completion ..at Eu =5, Task 2 released2nd inst Task 2 runs to completion1st inst Task 3 runs for 1 unit .. At Eu = 8, Task 1 released preempts Task33rd inst Task 1 runs to completion1st inst Task 3 runs for 1 unit .. At Eu = 10, 3rd inst of Task 2 released preempts 3 ..At Eu = 15, 1st inst Task 3 completes.. CPU idle Eu 18-20At Eu = 20, all 3 tasks released .. Cycle repeats

1 2 2 3 1 2 2 3 1 3 2 2 1 3 3 2 1 2 1

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Execution Units Eu

Page 16: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 16

RM Scheduling

• General schedulability test– If overall CPU utilisation U <= n(21/n -1)

• where n = no of tasks• RM will definitely produce feasible schedule

– However• RM may produce feasible schedule where

– U > n(21/n -1)

• i.e. Sufficient but not necessary condition• Recall Example: CPU U = 0.9 but still schedulable

– Depends on particular task characteristics

• May need to perform further schedulability analysis• As Task no increases, bound 69%

Page 17: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 17

Utilisation Bound for RM Alg.

No of Tasks

Bound

1 2 3 4 5 6 10

1

0.730.83

0.78 0.69

Page 18: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 30

Earliest Deadline First (EDF)• Dynamic priority and preemptable• Ready task whose absolute deadline is the

earliest given highest priority• Task priorities are reevaluated when tasks

released/completed• EDF is an optimal uniprocessor sched alg• If all tasks are periodic

– Task 1…n ; CPU U = – If U <=1, then task set is EDF schedulable

n

iiu

1

Page 19: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 31

EDF Example

Task e p u

T1 1 4 0.25

T2 2 5 0.4

T3 5 20 0.25

All Tasks released at time 0 Overall U = 0.9Sequence1st inst Task 1 runs 1st as earliest deadline of 41st inst Task 2 runs to completion1st inst Task 3 runs for 1 unit ..note: Deadline is 20 ..at Eu=4, Task 1 rel. preempy Task 3 as deadline is 82nd inst Task 1 runs to completion ..at Eu =5, Task 2 released2nd inst Task 2 runs to completion as deadline is 101st inst Task 3 runs for 1 unit .. At Eu = 8, Task 1 released preempts Task33rd inst Task 1 runs to completion1st inst Task 3 runs for 1 unit.. At Eu =10, Task 2 rel.. Preempts task 3 as deadline is 15..At Eu =12, Task 1 runs as deadline 16 < 20At Eu =15 Task 2 released and runs with deadline 20At Eu =16 Task 1 rel with deadline 20 no preemption

1 2 2 3 1 2 2 3 1 3 2 2 1 3 3 2 2 1 1

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Execution Units Eu

Page 20: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 32

EDF Example

Task e p u

T1 1 3 0.33

T2 2 5 0.4

T3 2 10 0.2

All Tasks released at time 0 Overall U = 0.93Sequence1st inst Task 1 runs 1st as earliest deadline of 31st inst Task 2 runs to completion .. At Eu=3, 2nd inst Task 1 rel & runs as deadline is 6 1st inst Task 3 runs ..at Eu=5, Task 2 rel. doesn’t preempt Task 3 as deadline is also 10 .. Task 3 completes .. At Eu = 6, Task 1 released deadline 9 runs .. At Eu = 7 Task 2 runs to completion .. At Eu =9, Task 1 rel deadline 12 ..runs .. At Eu =10, Task 3 rel. deadline 20, Task 2 also rel with deadline 15 Task 2 runs .. At Eu =15, Task 1 and 2 released with deadlines 16,20 Task 1 runs .. At Eu=16, Task 2 runs .. At Eu = 18 Task 1 rel with deadline 21 but Task 3 has deadline 20 Task 3 runs

1 2 2 1 3 3 1 2 2 1 2 2 1 3 3 1 2 2 3 3 1

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Execution Units Eu

Page 21: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 33

EDF vs RM• With RM, priorities fixed

– Lowest period tasks guaranteed proc time• Only higher period tasks will miss deadlines

– In overload conditions, same lower priority tasks lose out– Bound on CPU utilisation must be considered

• EDF, dynamic priority– More flexible.. Less predictable – All tasks may miss deadlines – Schedulable if CPU U <=1– May react poorly to overload condition

Page 22: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 34

Conventional OS Scheduling• Time Sharing Emphasis

– Balance needs of interactive and CPU intensive traffic• Complex alg

– Constantly adjust process priority• CPU intensive processes lose priority as they run• As a process waits, priority is increased will get CPU slot

sooner

• Unix command nice– nice -20 process_high_priority– nice +20 process_low_priority– nice will work but underlying sched alg unchanged

• Windows RealTime Threads – Somewhat equivalent

Page 23: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 35

POSIX.4 RT Scheduling • Defines two main scheduling policies

– SCHED_FIFO and SCHED_RR• Each have attributes

– Also have SCHED_OTHER– Currently a single attribute = priority struct sched_param{

int sched_priority;

}

– Eg. Could implement EDF by extending structure to include• struct timespec sched_deadline;• struct timespec sched_timerequired;

Page 24: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 36

POSIX.4 RT Scheduling

• SCHED_FIFO– Simple priority based preemptive scheduler– Most common in RTS– FIFO used to schedule processes within each

priority level– If no other process exists at higher priority,

process runs until complete• Next process at that priority (if present) then

allocated CPU• Highest priority process guaranteed processor time

Page 25: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 37

POSIX.4 RT Scheduling

• SCHED_RR– Round robin used to timeslice among

processes at same priority level– System provided timeslice– Use for lower priority tasks

Page 26: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 38

POSIX.4 RT Scheduling

• Setting scheduling policy and attribute#include <sched.h>

struct sched_param scheduling_parameters;

int scheduling_policy;

int i;

scheduling_parameters.sched_priority=17;

i=sched_setscheduler(getpid( ),SCHED_FIFO, &scheduling_parameters);

• getpid( ) used to determine process ID– Process set to FIFO, priority 17

Page 27: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 39

POSIX.4 RT Scheduling

• Process priority ranges differ among OS– Need this info before setting priority levelint sched_rr_min, sched_rr_max;int sched_fifo_min, sched_fifo_max;

sched_rr_min=sched_get_priority_min(SCHED_RR);sched_rr_max=sched_get_priority_max(SCHED_RR);sched_fifo_min=sched_get_priority_min(SCHED_FIFO);sched_fifo_max=sched_get_priority_max(SCHED_FIFO);– Eg. 256 priority levels

• FIFO range 128-255• RR range 0-127

Page 28: Process Scheduling for RTS

Dr. Hugh Melvin, Dept. of IT, NUI,G 40

POSIX.4 RT Scheduling

Eg.

#include<sched.h>

int i;

struct sched_param my_sched_params;

// determine max FIFO priority level

my_sched_params.sched_priority= sched_get_priority_max(SCHED_FIFO);

// Set priority

i=sched_setparam(getpid (),&my_sched_params);