Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady {...
Transcript of Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady {...
![Page 1: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/1.jpg)
Task Scheduling & Real-Time Operating
Systems
Alexander Nelson
October 14, 2019
University of Arkansas - Department of Computer Science and Computer Engineering
![Page 2: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/2.jpg)
Review – Tasks
Task – Unique continuously executing behavior
Concurrent Tasks – Tasks that execute during same time window
Concurrency is difficult:
• Many embedded systems are single-core
• Limited resources for task switching
• Full OS not common
1
![Page 3: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/3.jpg)
Managing Concurrent Tasks
How do you manage concurrent tasks?
2
![Page 4: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/4.jpg)
Managing Concurrent Tasks
How do you manage concurrent tasks?
Periodic Tasks – Concurrent FSM w/ known timing
What about aperiodic tasks?
3
![Page 5: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/5.jpg)
Managing Concurrent Tasks
How do you manage concurrent tasks?
Periodic Tasks – Concurrent FSM w/ known timing
What about aperiodic tasks?
Real-Time Operating Systems
4
![Page 6: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/6.jpg)
Roles of an Operating System
Process or Task Management
• Creation, deletion, suspension, resumption
• Manage interprocess communication
• Manage deadlocks
Memory Management
• Track & control tasks in memory
• Monitor memory locality w.r.t processes
• Administer dynamic memory allocation if used
I/O System Management
• Manage access to I/O
• Provide consistent framework for I/O access
5
![Page 7: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/7.jpg)
Roles of an Operating System
File System Management
• File creation, deletions, access
• Other storage maintenance
System Protection
• Restrict access to certain resources based on privilege
Networking - For distributed applications
• Facilitates remote scheduling of tasks
• Provides interprocess communications across a network
Command Interpretation
• Accessing I/O devices through devices drivers, interface with
user to accept and interpret command and dispatch task
6
![Page 8: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/8.jpg)
Real-Time Operating System
RTOS – Real-Time Operating System
Real Time – has time constraints that must be met
Predictability – OS will complete tasks by deadline
Predictability/Reliability more important than speed
7
![Page 9: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/9.jpg)
Review: Time Constraints
Time Constraint Categories:
• Hard Real-Time
• Soft Real-Time
• Firm Real-Time
8
![Page 10: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/10.jpg)
Review: Time Constraints
Time Constraint Categories:
• Hard Real-Time – Unmet condition = System Failure
• Soft Real-Time – Unmet condition = System Degradation
• Firm Real-Time – Degradation leading to failure
9
![Page 11: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/11.jpg)
Real-Time Operating System Traits
In order to meet constraints, the following RTOS traits are
advantageous
• Scheduling Algorithms supported
• Inter-process communication methods
• Preempting (time-based)
• Separate process address space
• Memory protection
• Low memory footprint (both RAM and program memory)
• Timing precision
• Debugging and Tracing
10
![Page 12: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/12.jpg)
Scheduling
Timeliness – “quality of being done or occurring at a favorable or
useful time”
Timeliness RTOS – Completing/updating tasks to meet real-time
constraints
Achieved by scheduling algorithms
11
![Page 13: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/13.jpg)
Scheduling
Scheduling – “Arrange to plan to take place at a particular time”
Scheduling (Computing) – “Method by which work specified is
assigned to resources to complete the work”
For RTOS, scheduling enables the perception of concurrent
execution & meeting of deadlines
12
![Page 14: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/14.jpg)
Terminology
Some useful terminology:
• Execution Time – Amount of time process requires to
complete
• Persistence – Amount of time b/w start and termination of
task
• Time-Share – CPU shares resources between tasks
• Time Slice – Short interval of time when processor deals w/
one task
• Preemption – A task is interrupted so another can process
13
![Page 15: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/15.jpg)
Scheduling Algorithms
Some common scheduling disciplines:
• First come, first served (FCFS)
• Priority Scheduling
• Shortest Time First
• Round-Robin
14
![Page 16: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/16.jpg)
First Come, First Served
First Come, First Served (FCFS)
Exactly what it sounds like
Arrival time dictates order, regardless of:
• Priority
• Execution Time
Task runs to completion
15
![Page 17: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/17.jpg)
First Come, First Served
Advantages:
• Easy to implement
• Low switching overhead
• No starvation
• No Preemption
Why might this be bad for a Real-Time system?
16
![Page 18: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/18.jpg)
Priority Scheduling
Priority Scheduling – Higher priority tasks preempt lower priority
tasks
Low latency for high priority tasks, potential low priority starvation
17
![Page 19: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/19.jpg)
Shortest Remaining Time First
Shortest Remaining Time – Shorter tasks preempt longer tasks
High task throughput, potential task starvation
18
![Page 20: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/20.jpg)
Earliest Deadline First
Earliest Deadline First – Task with next deadline is executed,
preempting allowed
Starvation of tasks with long deadlines
19
![Page 21: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/21.jpg)
Round Robin
Round Robin – Each task gets a set length time-slice for execution
No starvation, no guarantees to meet deadlines
20
![Page 22: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/22.jpg)
Choosing Scheduling Algorithm
For RTOS, must evaluate constraints and conditions
• Task priorities?
• Length of tasks
• Variable length tasks?
• Switching overhead, including memory
No single best scheduling algorithm
21
![Page 23: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/23.jpg)
Multi-Tasking Challenges
![Page 24: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/24.jpg)
Preempting
Preempting – interrupt a currently executing task
To preempt a task:
• Save state of process
• Save program counter
• Save any registers
These in total are called the “context”
“Context switching” – saving of one state and loading another
Context switching creates overhead in multitasking/preemption
22
![Page 25: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/25.jpg)
Threads
Thread – smallest set of information needed to run a task
Threads are objects/structures that hold task information
Makes context switching easier
Multithreading – process of running multiple threads on the same
system
23
![Page 26: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/26.jpg)
Threads
Ideally, each process should have its own private section of memory
to work with, called its address space
Along with hardware support (memory protection unit MPU) an
OS may be able to enforce that process do not access memory
outside their address space
Organizational Concepts:
• Multi-process execution – multiple distinct processes running
in separate threads
• Multi-threaded process – a process with several execution
threads (likely sharing memory and managing resource use
internally)
• Note – intraprocess thread context switching is typically less
expensive than interprocess context switching
24
![Page 27: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/27.jpg)
Thread Reentrancy
Reentrant – Function can be interrupted in exeuction and run
again (re-entered) before previous invocations complete
Counterexample:
int tmp;
void swap(int* x, int* y){
tmp = *x;
*x = *y;
*y = tmp; /* invoke isr() here. */
}
25
![Page 28: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/28.jpg)
Thread-Safe
Thread-Safe code – Other threads or processes can run safely at
same time
For single core, a different task can preempt and execute fully with
no effect on other tasks
26
![Page 29: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/29.jpg)
Thread Safe Counterexample
int tmp;
void swap(int* x, int* y){
int s; /* Save global variable. */
s = tmp;
tmp = *x;
*x = *y;
*y = tmp; /* Hardware interrupt isr() here. */
tmp = s; /* Restore global variable. */
}
void isr(){
int x = 1, y = 2;
swap(&x, &y);
}
27
![Page 30: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/30.jpg)
Reentrant and Thread Safe
void swap(int* x, int* y){
int tmp;
tmp = *x;
*x = *y;
*y = tmp; /* Hardware interrupt isr() here. */
}
void isr(){
int x = 1, y = 2;
swap(&x, &y);
}
tmp variable allocated on stack instead of heap
Thread safe & reentrant28
![Page 31: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/31.jpg)
Multitasking Coding Practices
Dangerous:
• Multiple calls access same variable/resource
e.g. Global variables, process variables, pass-by-ref, shared
resources
Safe:
• Local Variables – only using local variables makes code
reentrant
29
![Page 32: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/32.jpg)
Implementing RTOS
![Page 33: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/33.jpg)
Philosophies
Most common design philosophies:
• Event-Driven – Context switch on event of higher priority
• Time-Sharing – Context switch on regularly clocked interrupt
Time-sharing gives smoother multitasking at added overhead cost
Time-sharing may miss deadlines if timing ill-defined
30
![Page 34: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/34.jpg)
Kernel
Kernel – “Core” OS functions
• Perform Scheduling – Handled by scheduler
• Dispatch Processes – Handled by the dispatcher
• Facilitate inter-process communication
Must structure tasks to pass between scheduler/dispatcher/OS
31
![Page 35: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/35.jpg)
Kernel Design Strategies
Some common kernel designs (see if these look familiar):
• Polled Loop – Single instruction tests a flag for event
• Cyclic Executive – Round robin execution of short processes
to give illusion of concurrency
• Cooperative Multitasking – Multiple tasks executed in
state-driven fashion (code-driven finite state automata)
• Interrupt-Driven System – Main program is a single
jump-to-self instruction (e.g. while(1);)
Short processes executed by ISRs
• Foreground-Background – Interrupts do timely processing of
external events – Set up process for main program
32
![Page 36: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/36.jpg)
Kernel Design Strategies
If none of those strategies are sufficient, a full-featured RTOS may
be needed
Full-featured RTOS - extension of foreground-background pattern
Adds additional functionality, such as:
• Choice of scheduling algorithm
• OS handles interrupt & pass to relevant tasks
• Support intra-process/task communication
• etc.
33
![Page 37: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/37.jpg)
Task Control Block (TCB)
OS needs to keep track of tasks
Process/Task Control Block – structure containing
tasks/processes
Tasks in TCB stored in a “Job Queue”
34
![Page 38: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/38.jpg)
Basic Task Control Block
typedef struct tcb{
void(*taskPtr)(void * taskDataPtr); //Task function & argument
void *taskDataPtr; //Pointer for data passing
void *stackPtr; //Individual Task’s stack
unsigned short priority; //Priority Information
struct TCB * nextPtr; //If linked list
struct TCB *prevPtr; //If doubly linked list
} TCB;
35
![Page 39: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/39.jpg)
Task Control Block
TCB should be generic
Task can be anything, so generic template is needed
Each task written as function, conforming to same generic interface
e.g.
void aTask(void * taskDataPtr){
//Task Code
}
36
![Page 40: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/40.jpg)
Task Control Block – Data
Task’s data is stored in customized container
Task must know the structure
OS refers to data block with generic pointer
typedef struct taskData{
int taskData0;
char taskData1;
...
} TASKDATA;
37
![Page 41: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/41.jpg)
Task States
A task is typically in one of three states:
• Running – Active task, currently executed by CPU
• Ready – Waiting, ready to be executed
• Blocked – Waiting for an event (e.g. I/O, timing)
Tasks that are ready/blocked are in the TCB job queue
38
![Page 42: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/42.jpg)
Simple TCB
Queue is array
of function
pointers
Round robin
execution of
functions in
queue
39
![Page 43: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/43.jpg)
Structure TCB
Queue is array
of TCB
structures
Round robin
execution with
data sharing
40
![Page 44: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/44.jpg)
Scheduler
Scheduler – determine what tasks should run
Tasks in TCB ready queue are selected based on scheduling
algorithm
Choose algorithm based on your system! – No one-size-fits-all
scheduling algorithm
41
![Page 45: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/45.jpg)
Dispatcher
Dispatcher – Gives control of CPU to process selected by scheduler
Also handles interrupts Must:
• Switch Context
• Call function for new task
Dispatch Latency – Amount of time to stop one process & start
another
42
![Page 46: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/46.jpg)
RTOS Issues
![Page 47: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/47.jpg)
Timer Overrun
Tasks have a non-negligible finite execution time
If execution time + overhead > tick frequency, can miss deadlines
Need a construct for detecting timer overruns
43
![Page 48: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/48.jpg)
Detect Overrun
unsigned char processingRdyTasks = 0;
void TimerISR() {
unsigned char i;
if (processingRdyTasks) {
printf("Period too short to complete tasks.\r\n");
return;
}
processingRdyTasks = 1;
for (i = 0; i < tasksNum; ++i) { // Heart of scheduler
if ( tasks[i].elapsedTime >= tasks[i].period ) { // Ready
tasks[i].state = tasks[i].TickFct(tasks[i].state);
tasks[i].elapsedTime = 0;
}
tasks[i].elapsedTime += tasksPeriodGCD;
}
processingRdyTasks = 0;
} 44
![Page 49: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/49.jpg)
Utilization
Utilization – period of time in use
Can be used to predict if timer overrun likely to occur
Utilization = time-per-task-tick / task period
e.g.
Time-per-task-tick = 50ms, task period = 500ms:
Utilization = 50/500 = 10%
If utilization > 100%, overrun will occur
45
![Page 50: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/50.jpg)
Calculating task-time
Calculating the time for a task is difficult
Some tasks (e.g. sorting) have a stochastic runtime
Worst Case Execution Time (WCET) – useful to make sure no
deadlines missed
Calculating WCET can be done through static (no execution) or
dynamic (measured execution) means
46
![Page 51: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/51.jpg)
Reducing Utilization
Utilization is composed of two factors:
• WCET Task Time
• Tick Frequency
To reduce utilization, one of these factors must change
WCET Task Time – Optimize Code or split function into two states
Tick Frequency – Decrease tick frequency
What are some advantages/disadvantages of these strategies?
47
![Page 52: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/52.jpg)
Multitasking & Utilization
Utilization for multiple tasks = sum(time-per-task) / task period
One long task can greatly impact tick frequency for other tasks
48
![Page 53: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/53.jpg)
Utilization & Multitasking Different Periods
hyperperiod – LCM of the task’s periods
During the hyperperiod, the tasks will line up to execute at same
time
Thus, utilization during hyperperiod is the same as if they have the
same period
49
![Page 54: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/54.jpg)
Jitter
Jitter – Delay between time task was ready & when it starts
executing
Causes:
• Other tasks executing/ready
• Overhead in scheduling/dispatching
50
![Page 55: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/55.jpg)
Jitter
What does that mean practically?
1
1https://stackoverflow.com/a/23352577 51
![Page 56: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/56.jpg)
Jitter
Timing assumptions are dangerous when multitasking
Best-case/worst-case analysis
Determine what the smallest/largest jitter can/will be
Design real-time system with respect to time-constraints & jitter
52
![Page 57: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/57.jpg)
Rate-Monotonic Scheduling
Some scheduling is based on task’s period/rate
Deadline is to process task before scheduled again
e.g. Sampling from A/D
Jitter + Multitasking + Hyperperiod can cause potential missed
deadlines
53
![Page 58: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/58.jpg)
Priority Queue
To handle priority in scheduling, a queue structure can hold tasks
in order of priority
How would you design a task-insert function based on an Earliest
Deadline First scheduling into a queue?
54
![Page 59: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/59.jpg)
Preemptive vs. Non-Preemptive Scheduling
If timing can guarantee deadlines – Non-preemptive much easier to
handle
Non-preemptive – no interrupting
Preemptive – interrupts can occur
For preemptive, need to determine if nested interrupts are allowed
55
![Page 60: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/60.jpg)
Preemptive Scheduling Dangers
Dangers of preemptive scheduling:
• Stack Overflow – Too many interrupts & context switches
overflow the stack
• Task Corruption – Interrupt modifies variables needed by
interrupted task
56
![Page 61: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/61.jpg)
Critical Section
Critical Section – section of code that must occur sequentially &
not interrupted
Can disable interrupts surrounding critical section
Critical section should be short and necessary
Long critical sections can break timing constraints
Unnecessary critical sections may be better to roll back & restart
than hold up execution
57
![Page 62: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/62.jpg)
Power & Sleep Modes
Instead of a while(1) or while(!TimerFlag), a sleep() may be used
Sleep modes typically reduce active power consumption
Table 1: MSP430fr2633 Sleep Modes (16MHz)
Mode Description Typ Max
Active Executing 1000µA 3480µA
LPM0 CPU off 420µA —
LPM3 CPU & Main Oscillator off 1.18µA 1.65µA
LPM4 Everything off (must be woken
by external interrupt)
0.49µA —
Apple Watch (v1) battery capacity = 205mAh – 205 hours of
operation at 1mA average consumption
58
![Page 63: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/63.jpg)
Sleep Modes
Sleep modes often have varying wake-up times – jitter overhead
Timed events can use timer interrupts to wake from sleep (&
account for wake-up)
59
![Page 64: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/64.jpg)
Duty Cycle Calculation
When talking about sleep modes, “duty-cycles” are times when
controller is active
Reduce Power = Reduce duty-cycle time
60
![Page 65: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/65.jpg)
Duty Cycle
Duty Cycle analysis useful to determine total power consumption
Average power = Average active power × duty cycle %
+ Average sleep power × 1 - duty cycle %
Example:
2% duty cycle, LPM3 sleep, typical active power:
Average Power = 1000µA× 0.02 + 1.18µA× 0.98
Average Power = 20µA + 1.156µA = 21.156µA
Active Power still consumes ≈ 94.5% power despite active only 2%
of the time
61
![Page 66: Task Scheduling & Real-Time Operating Systemsahnelson/CSCE4114/lectures/lecture14.pdfReady { Waiting, ready to be executed Blocked { Waiting for an event (e.g. I/O, timing) Tasks that](https://reader030.fdocuments.net/reader030/viewer/2022040320/5e48d75dc5089448543156f7/html5/thumbnails/66.jpg)
Supercapacitors
Recent debate – battery vs. supercapacitor
Battery stores energy through chemical reaction
Supercapacitor stores energy through capacitance (a medium’s
ability to hold charge)
Battery holds more charge – degrades over time, charges slower
Supercapcitor holds less charge – rechargeable more times, charges
quickly
62