Processes and Operating Systems Chapter 6 COE 306

  • Slides: 63
Download presentation
Processes and Operating Systems Chapter 6 COE 306: Introduction to Embedded Systems Dr. Aiman

Processes and Operating Systems Chapter 6 COE 306: Introduction to Embedded Systems Dr. Aiman El-Maleh Computer Engineering Department College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals

Next. . . v Processes and Operating Systems v Real Time Operating System (RTOS)

Next. . . v Processes and Operating Systems v Real Time Operating System (RTOS) v The Scheduling Problem v Rate Monotonic Scheduling (RMS) v Earliest-Deadline-First Scheduling (EDFS) v Free. RTOS Processes and Operating Systems slide 2 COE 306– Introduction to Embedded System– KFUPM

Processes and Operating Systems v Processes and operating system are abstractions ² allow us

Processes and Operating Systems v Processes and operating system are abstractions ² allow us to build complex applications on microprocessors ² provide much greater flexibility to satisfy timing requirements v Let us switch the state of the processor between multiple tasks v Process defines the state of an executing program v A process is a unique execution of a program ² Several copies of a program may run simultaneously or at different times v A process has its own state: registers and memory ² Threads share the same address space Processes and Operating Systems slide 3 COE 306– Introduction to Embedded System– KFUPM

Processes and Operating Systems v The Operating System (OS) manages processes v OS provides

Processes and Operating Systems v The Operating System (OS) manages processes v OS provides the mechanism for switching execution between processes v Real-Time Operating System (RTOS) is OS that provides facilities for satisfying real-time requirements ² Allocates resources based on real-time requirements ² General-purpose OSs use other criteria, e. g. fairness v RTOS helps build more complex systems using several programs that run concurrently Processes and Operating Systems slide 4 COE 306– Introduction to Embedded System– KFUPM

Tasks and Processes v A task is a functional description of a connected set

Tasks and Processes v A task is a functional description of a connected set of operations v Task can also mean a collection of processes v Multiple tasks means multiple processes v Multiple processes help with timing complexity: ² multiple rates § multimedia § automotive ² asynchronous input § user interfaces § communication systems Processes and Operating Systems slide 5 COE 306– Introduction to Embedded System– KFUPM

Compression Unit Example v Variable data rates ² need to receive and send data

Compression Unit Example v Variable data rates ² need to receive and send data at different rates ² program may emit two bits for first byte and then seven bits for second byte ² must ensure to process inputs and outputs at the proper rates v Asynchronous input ² a compression mode button that disables or enables compression v Keeping up with input and output data while checking on button can introduce complex control code into program Processes and Operating Systems slide 6 COE 306– Introduction to Embedded System– KFUPM

Compression Unit Example v Sampling the button’s state too slowly can cause the machine

Compression Unit Example v Sampling the button’s state too slowly can cause the machine to miss a button depression entirely v Sampling it too frequently can cause the machine to incorrectly compress data due to executing later than expected v Variable execution times of code causes other code to execute later than expected v We need to be able to keep track of these two different tasks separately, applying different timing requirements to each v Complex control is usually quite difficult to verify for either functional or timing properties Processes and Operating Systems slide 7 COE 306– Introduction to Embedded System– KFUPM

Multi-Rate Systems v Multirate embedded computing systems are very common, including automobile engines, printers,

Multi-Rate Systems v Multirate embedded computing systems are very common, including automobile engines, printers, and cell phones v Certain operations must be executed periodically, and each operation is executed at its own rate v Tasks may be synchronous or asynchronous v Synchronous tasks may recur at different rates v Processes run at different rates based on computational needs of the tasks Processes and Operating Systems slide 8 COE 306– Introduction to Embedded System– KFUPM

Example: Engine Control v Tasks: ² spark control § Firing the spark plug periodically

Example: Engine Control v Tasks: ² spark control § Firing the spark plug periodically § Spark timing varies with engine speed ² regulate exhaust emissions ² crankshaft sensing ² fuel/air mixture ² oxygen sensor ² multimode control scheme § engine warm-up , cruise, climbing steep hills v Rates at which engine inputs and outputs must be handled range between 2 ms and 1 s Processes and Operating Systems slide 9 COE 306– Introduction to Embedded System– KFUPM

Typical Rates in Engine Controllers Variable Update period (ms) Engine spark timing 2 Throttle

Typical Rates in Engine Controllers Variable Update period (ms) Engine spark timing 2 Throttle 2 Air flow 4 Battery voltage 4 Fuel flow 10 Recycled exhaust gas 25 Status switches 20 Air temperature 400 Barometric pressure 1000 Spark (dwell) 1 Fuel adjustment 8 Carburetor 25 Mode actuators 100 Processes and Operating Systems slide 10 COE 306– Introduction to Embedded System– KFUPM

Real-Time Systems v Perform a computation to conform to external timing constraints v Deadline

Real-Time Systems v Perform a computation to conform to external timing constraints v Deadline frequency: ² Periodic ² Aperiodic v Deadline type: ² Hard: failure to meet deadline causes system failure § Car Airbag system, car brakes ² Soft: failure to meet deadline causes degraded response § Room temperature control, car multimedia system ² Firm: late response is useless; Infrequent deadline misses are tolerable, but may degrade the system's quality of service § A digital cable set-top box frame decoder Processes and Operating Systems slide 11 COE 306– Introduction to Embedded System– KFUPM

Types of Process Timing Requirements v Release time: time at which process becomes ready

Types of Process Timing Requirements v Release time: time at which process becomes ready to execute v Deadline: time at which process must finish execution v Periodic process: a process executes every period ² e. g. Firing the spark plugs v Aperiodic process: executes on demand ² Processing a button press v Period : interval between process activations v Initiation Interval or Rate = 1/period Processes and Operating Systems slide 12 COE 306– Introduction to Embedded System– KFUPM

Types of Process Timing Requirements v Jitter: Allowable variation in task completion time ²

Types of Process Timing Requirements v Jitter: Allowable variation in task completion time ² Example: multimedia synchronization v What happens when a process misses a deadline? ² Can be catastrophic such as in an automotive control system ² a missed deadline in a telephone system may cause a temporary silence on the line v Example: Space Shuttle software error ² Space Shuttle’s first launch was delayed by a software timing error ² Change to one routine added delay that threw off start time calculation Processes and Operating Systems slide 13 COE 306– Introduction to Embedded System– KFUPM

Task Graphs v Tasks may have data dependencies---must execute in certain order v Task

Task Graphs v Tasks may have data dependencies---must execute in certain order v Task graph shows data/control dependencies between processes v Task: connected set of processes v Task set: One or more tasks v Task graph assumes that all processes in each task run at the same rate, tasks do not communicate v In reality, some amount of inter-task communication is necessary Processes and Operating Systems slide 14 COE 306– Introduction to Embedded System– KFUPM

Process Execution Characteristics v Process execution time Ti ² Execution time in absence of

Process Execution Characteristics v Process execution time Ti ² Execution time in absence of preemption ² Possible time units: seconds, clock cycles ² Worst-case, best-case execution time may be useful in some cases v Sources of variation: ² Data dependencies ² Memory system ² CPU pipeline Processes and Operating Systems slide 15 COE 306– Introduction to Embedded System– KFUPM

CPU Utilization v Processes and Operating Systems slide 16 COE 306– Introduction to Embedded

CPU Utilization v Processes and Operating Systems slide 16 COE 306– Introduction to Embedded System– KFUPM

Running Periodic Processes v Need code to control execution of processes v Simplest implementation:

Running Periodic Processes v Need code to control execution of processes v Simplest implementation: process = subroutine v While loop implementation ² Simplest implementation has one loop ² No control over execution timing while (TRUE) { p 1(); p 2(); } ² Pad the loop with NOP instructions ² Used by some video games in the 1970 s ² Broken: hard to determine execution time, conditionals, reevaluation upon code change Processes and Operating Systems slide 17 COE 306– Introduction to Embedded System– KFUPM

Running Periodic Processes v Timed loop implementation ² Encapsulate set of all processes in

Running Periodic Processes v Timed loop implementation ² Encapsulate set of all processes in a single function that implements the task set ² Use timer to control execution of the task void pall() { p 1(); p 2(); } ² No control over timing of individual processes v Multiple timers implementation ² Each task has its own function ² Each task has its own timer § May not have enough timers to implement all the rates Processes and Operating Systems slide 18 void A(){ /* rate A */ p 1(); p 3(); } void B(){ /* rate B */ p 2(); p 4(); p 5(); } COE 306– Introduction to Embedded System– KFUPM

Running Periodic Processes v Timer + counter implementation int p 2 count = 0;

Running Periodic Processes v Timer + counter implementation int p 2 count = 0; void pall(){ ² Use a software count to divide the timer p 1(); ² Only works for clean multiples of the timer if (p 2 count >= 2) { period p 2(); p 2 count = 0; v Implementing processes } ² All of these implementations are else p 2 count++; inadequate. p 3(); ² Need better control over timing } ² Need a better mechanism than subroutines ² Real-time operating systems (RTOS) Processes and Operating Systems slide 19 COE 306– Introduction to Embedded System– KFUPM

Operating Systems v The operating system controls resources ² who gets the CPU ²

Operating Systems v The operating system controls resources ² who gets the CPU ² when I/O takes place ² how much memory is allocated v The most important resource is the CPU itself ² CPU access controlled by the scheduler Processes and Operating Systems slide 20 COE 306– Introduction to Embedded System– KFUPM

Real-Time Operating Systems v Solves the main problems of a cooperative multitasking system v

Real-Time Operating Systems v Solves the main problems of a cooperative multitasking system v Executes processes based on timing requirements provided by system designer v Based on two basic concepts: ² Preemption: the ability to interrupt a process to switch to another ² Context switching: switching execution and CPU state between processes Processes and Operating Systems slide 21 COE 306– Introduction to Embedded System– KFUPM

State of a Process v A process can be in one of three states:

State of a Process v A process can be in one of three states: ² executing on the CPU ² ready to run ² waiting for I/O, another process, timer, next period v The operating system selects the next executing process v At most one executing process Processes and Operating Systems slide 22 COE 306– Introduction to Embedded System– KFUPM

Preemptive Execution v Kernel: part of the OS. Determines which process runs v The

Preemptive Execution v Kernel: part of the OS. Determines which process runs v The kernel is activated periodically by the timer v The timer period is known as the time quantum ² The smallest time unit in which CPU activity can be controlled Processes and Operating Systems slide 23 COE 306– Introduction to Embedded System– KFUPM

Context Switching v Context: The set of registers that define a process v Context

Context Switching v Context: The set of registers that define a process v Context Switching: Switching the registers from one process to another ² Timer interrupt: transfer control from a process to kernel ² Kernel saves current process context ² Kernel selects next process (scheduling) ² Kernel restores next process context v Context switching code is usually assembly code v Task priorities make selecting processes flexible and fast Processes and Operating Systems slide 24 COE 306– Introduction to Embedded System– KFUPM

Context Switching in FREERTOS Processes and Operating Systems slide 25 COE 306– Introduction to

Context Switching in FREERTOS Processes and Operating Systems slide 25 COE 306– Introduction to Embedded System– KFUPM

The Scheduling Problem v Choosing the order of running processes is known as scheduling

The Scheduling Problem v Choosing the order of running processes is known as scheduling v Workstations try to avoid starving processes of CPU access ² Fairness = access to CPU v Embedded systems must meet deadlines ² Low-priority processes may not run for a long time v Scheduling feasibility ² Resource constraints make schedulability analysis NP-hard ² Must show that the deadlines are met for all timings of resource requests Processes and Operating Systems slide 26 COE 306– Introduction to Embedded System– KFUPM

Scheduling Feasibility v Assume: ² All processes run periodically on a single CPU ²

Scheduling Feasibility v Assume: ² All processes run periodically on a single CPU ² Constant process execution times v Require: ² T (period) ≥ Si Ti ² Can’t use more than 100% of the CPU. v Hyperperiod: least common multiple (LCM) of the task periods ² Must look at the hyperperiod schedule to find all task interactions ² Hyperperiod can be very long if task periods are not chosen carefully Processes and Operating Systems slide 27 COE 306– Introduction to Embedded System– KFUPM

Hyperperiod Example v Long hyperperiod: v Shorter hyperperiod: ² P 1 7 ms ²

Hyperperiod Example v Long hyperperiod: v Shorter hyperperiod: ² P 1 7 ms ² P 1 8 ms ² P 2 11 ms ² P 2 12 ms ² P 3 15 ms ² P 3 16 ms ² LCM = 1155 ms ² LCM = 48 ms v Example: ² P 1 period 1 ms, CPU time 0. 1 ms ² P 2 period 1 ms, CPU time 0. 2 ms ² P 3 period 5 ms, CPU time 0. 3 ms Processes and Operating Systems slide 28 COE 306– Introduction to Embedded System– KFUPM

Round-Robin Scheduling v All the processes are kept on a list and scheduled one

Round-Robin Scheduling v All the processes are kept on a list and scheduled one after the other v Time slices are assigned to each process in equal portions and in circular order v Common in general-purpose Oses v Schedule process only if ready ² Always test processes in the same order v Start round-robin again after finishing a round Processes and Operating Systems slide 29 COE 306– Introduction to Embedded System– KFUPM

Priority-Based Scheduling v Each process has a priority v CPU goes to highest-priority process

Priority-Based Scheduling v Each process has a priority v CPU goes to highest-priority process that is ready v Priorities determine scheduling policy: ² fixed priority ² time-varying priorities v Example: ² each process has a fixed priority (1 highest) ² highest-priority ready process gets CPU ² process continues until done Processes and Operating Systems slide 30 COE 306– Introduction to Embedded System– KFUPM

Priority-Based Scheduling Example P 3 ready t=18 P 2 ready t=0 P 1 ready

Priority-Based Scheduling Example P 3 ready t=18 P 2 ready t=0 P 1 ready t=15 P 2 0 P 1 10 Processes and Operating Systems slide 31 20 P 2 30 P 3 40 60 50 COE 306– Introduction to Embedded System– KFUPM time

Scheduling Metrics v How do we evaluate a scheduling policy: ² Ability to satisfy

Scheduling Metrics v How do we evaluate a scheduling policy: ² Ability to satisfy all deadlines ² CPU utilization---percentage of time devoted to useful work ² Scheduling overhead---time required to make scheduling decision Processes and Operating Systems slide 32 COE 306– Introduction to Embedded System– KFUPM

Rate Monotonic Scheduling (RMS) v RMS is widely-used, analyzable scheduling policy v A static

Rate Monotonic Scheduling (RMS) v RMS is widely-used, analyzable scheduling policy v A static scheduling policy: processes have fixed priorities v RMS Model ² All processes run on single CPU ² Context switching time is ignored ² No data dependencies between processes ² Process execution time is constant ² All deadlines are at the end of the period ² Highest-priority ready process runs first v Priority assignment: The process with the shortest period is assigned the highest priority Processes and Operating Systems slide 33 COE 306– Introduction to Embedded System– KFUPM

Rate Monotonic Scheduling (RMS) v RMS always provides a feasible schedule if such a

Rate Monotonic Scheduling (RMS) v RMS always provides a feasible schedule if such a schedule exists with fixed priority v Optimal static assignment ² No fixed-priority scheme does better ² Highest CPU utilization while ensuring that all processes meet their deadlines v Ti is computation time of process i v i is period of process i Released Execution time Ti Absolute deadline i Processes and Operating Systems slide 34 COE 306– Introduction to Embedded System– KFUPM

Rate Monotonic Scheduling Example v What if execution times become 2, 3, 3? Processes

Rate Monotonic Scheduling Example v What if execution times become 2, 3, 3? Processes and Operating Systems slide 35 COE 306– Introduction to Embedded System– KFUPM

Rate Monotonic Analysis (RMA) § There are cases where first relationship can be satisfied

Rate Monotonic Analysis (RMA) § There are cases where first relationship can be satisfied and second cannot. v If P 1 has higher priority, required § There are no cases where second relationship can be constraint on CPU time: satisfied and first cannot § Consider T 1=2 and T 2=2, 1=3, 2=6. v If P 2 has higher priority, required constraint on CPU time: Processes and Operating Systems slide 36 COE 306– Introduction to Embedded System– KFUPM

RMS CPU utilization v Utilization for n processes is: v Given n processes and

RMS CPU utilization v Utilization for n processes is: v Given n processes and ratio between any two periods less than 2, RMS CPU utilization upper bound: ² n = 2; U ≤ 0. 83 ² n = 3; U ≤ 0. 78 ² n ; U ≤ ln 2 ≈ 0. 69, 31% idle time v RMS may not be able to use 100% of CPU, even with zero context switch overhead Processes and Operating Systems slide 37 COE 306– Introduction to Embedded System– KFUPM

RMS CPU Utilization Example v Utilization = 1/4 + 2/6 + 3/12 = 0.

RMS CPU Utilization Example v Utilization = 1/4 + 2/6 + 3/12 = 0. 83 Processes and Operating Systems slide 38 COE 306– Introduction to Embedded System– KFUPM

Response Time v Response time ² Duration from release time to finish time Response

Response Time v Response time ² Duration from release time to finish time Response Time P 1(1, 4) P 2(2, 5) P 3(2, 10) Processes and Operating Systems slide 39 5 10 15 COE 306– Introduction to Embedded System– KFUPM

Response Time v Response Time (ri) [Audsley et al. , 1993] ² HP(Pi) :

Response Time v Response Time (ri) [Audsley et al. , 1993] ² HP(Pi) : a set of higher-priority tasks than Pi v Real-time system is schedulable under RMS if and only if ri ≤ i for all processes Pi(Ti, i) Processes and Operating Systems slide 40 COE 306– Introduction to Embedded System– KFUPM

Response Time v Response time computation for P 3 ² r 3 = 2

Response Time v Response time computation for P 3 ² r 3 = 2 + 0 = 2 ² r 3 = 2 + 1 + 2 = 5 ² r 3 = 2 + 2 = 6 ² r 3 = 2 + 2 + 4 = 8 Processes and Operating Systems slide 41 COE 306– Introduction to Embedded System– KFUPM

RMS- Schedulability Check v A set of n processes is schedulable on a uniprocessor

RMS- Schedulability Check v A set of n processes is schedulable on a uniprocessor by the RMS algorithm if the processor utilization (utilization test): v This condition is sufficient, but not necessary ² If U is less than or equal to the given bound, a schedule exists ² If there is a schedule, U could be greater than the bound v Example: ² P 1: (T 1=1, 1=2), P 2: (T 2=2, 2=4) ² There is a schedule with U=100% Processes and Operating Systems slide 42 COE 306– Introduction to Embedded System– KFUPM

Another RMS Example P 1: T 1=1, 1=4 P 2: T 2=2, 2=5 P

Another RMS Example P 1: T 1=1, 1=4 P 2: T 2=2, 2=5 P 3: T 3=2, 3=7 P 1(1, 4) P 2(2, 5) P 3(2, 7) n = 3; U ≤ 0. 78 Processes and Operating Systems slide 43 5 10 15 U =0. 936 COE 306– Introduction to Embedded System– KFUPM

Another RMS Example P 1(1, 4) P 2(2, 5) P 3(2, 7) n =

Another RMS Example P 1(1, 4) P 2(2, 5) P 3(2, 7) n = 3; U ≤ 0. 78 Processes and Operating Systems slide 44 5 10 15 U =0. 936 COE 306– Introduction to Embedded System– KFUPM

Another RMS Example Deadline Miss ! P 1(1, 4) P 2(2, 5) P 3(2,

Another RMS Example Deadline Miss ! P 1(1, 4) P 2(2, 5) P 3(2, 7) n = 3; U ≤ 0. 78 Processes and Operating Systems slide 45 5 10 15 U =0. 936 COE 306– Introduction to Embedded System– KFUPM

Earliest-Deadline-First Scheduling v EDFS: dynamic priority scheduling scheme v Process closest to its deadline

Earliest-Deadline-First Scheduling v EDFS: dynamic priority scheduling scheme v Process closest to its deadline has highest priority v Requires recalculating processes priorities at every timer interrupt v Can achieve 100% utilization; higher utilization than RMS v On each timer interrupt ² compute time to deadline; choose process closest to deadline v Optimal scheduling algorithm ² if there is a schedule for a set of real-time tasks, EDF can schedule it ² Real-time system is schedulable under EDFS iff ∑Ui ≤ 1 Processes and Operating Systems slide 46 COE 306– Introduction to Embedded System– KFUPM

EDFS Example P 1: T 1=1, 1=4 P 2: T 2=2, 2=5 P 3:

EDFS Example P 1: T 1=1, 1=4 P 2: T 2=2, 2=5 P 3: T 3=2, 2=7 P 1(1, 4) P 2(2, 5) P 3(2, 7) Processes and Operating Systems slide 47 5 10 15 COE 306– Introduction to Embedded System– KFUPM

EDFS Example P 1(1, 4) P 2(2, 5) 5 10 15 P 3(2, 7)

EDFS Example P 1(1, 4) P 2(2, 5) 5 10 15 P 3(2, 7) Processes and Operating Systems slide 48 COE 306– Introduction to Embedded System– KFUPM

EDFS Example P 1(1, 4) P 2(2, 5) P 3(2, 7) Processes and Operating

EDFS Example P 1(1, 4) P 2(2, 5) P 3(2, 7) Processes and Operating Systems slide 49 5 10 15 COE 306– Introduction to Embedded System– KFUPM

EDFS Example P 1(1, 4) P 2(2, 5) P 3(2, 7) Processes and Operating

EDFS Example P 1(1, 4) P 2(2, 5) P 3(2, 7) Processes and Operating Systems slide 50 5 10 15 COE 306– Introduction to Embedded System– KFUPM

EDFS – Overload Conditions v Domino effect during overload conditions ² Example: P 1(3,

EDFS – Overload Conditions v Domino effect during overload conditions ² Example: P 1(3, 4), P 2(3, 5), P 3(3, 6), P 4(3, 7) Deadline Miss ! P 1 0 P 2 P 3 P 4 3 5 6 7 Better schedules : P 1 0 P 3 3 Processes and Operating Systems slide 51 P 1 5 6 7 0 P 4 3 5 COE 306– Introduction to Embedded System– KFUPM 6 7

Free. RTOS v Free. RTOS is a real-time kernel/scheduler on top of which MCU

Free. RTOS v Free. RTOS is a real-time kernel/scheduler on top of which MCU applications can be built to meet their hard real-time requirements. ² Allows MCU applications be organized as a collection of independent threads of execution. ² Decides which thread should be executed by examining the priority assigned to each thread. § Assume a single core MCU, where only a single thread can be executing at one time. v In Free. RTOS, each thread of execution is called a ‘task’. Processes and Operating Systems slide 52 COE 306– Introduction to Embedded System– KFUPM

Task States in Free. RTOS v A task can exist in one of the

Task States in Free. RTOS v A task can exist in one of the following states: ² Running ² Ready ² Blocked § A task is said to be in the Blocked state if it is currently waiting for either a temporal or external event. ² Susepended § tasks only enter or exit the Suspended state when explicitly commanded to do so through the v. Task. Suspend() and x. Task. Resume() API calls Processes and Operating Systems slide 53 COE 306– Introduction to Embedded System– KFUPM

Free. RTOS Task Functions v Tasks are implemented as C functions. ² Its prototype

Free. RTOS Task Functions v Tasks are implemented as C functions. ² Its prototype must return void and take a void pointer parameter as the following void ATask. Function (void *pv. Parameters); v Each task is a small program in its own right. ² Has an entry point ² Normally runs forever within an infinite loop ² Does not exit ² Must not contain a ‘return’ statement ² Must not be allowed to execute past the end of the function ² If a task is no longer required, it should be explicitly deleted. Processes and Operating Systems slide 54 COE 306– Introduction to Embedded System– KFUPM

Free. RTOS Task Functions Processes and Operating Systems slide 55 COE 306– Introduction to

Free. RTOS Task Functions Processes and Operating Systems slide 55 COE 306– Introduction to Embedded System– KFUPM

Creating Tasks v x. Task. Create() API function ² the most fundamental component in

Creating Tasks v x. Task. Create() API function ² the most fundamental component in a multitasking system § Probably the most complex of all API functions port. BASE_TYPE x. Task. Create( pd. TASK_CODE pv. Task. Code, const signed char * const pc. Name, unsigned short us. Stack. Depth, void *pv. Parameters, unsigned port. BASE_TYPE ux. Priority, x. Task. Handle *px. Created. Task ); Processes and Operating Systems slide 56 COE 306– Introduction to Embedded System– KFUPM

Task Parameters v pv. Task. Code ² a pointer to the function (just the

Task Parameters v pv. Task. Code ² a pointer to the function (just the function name) that implements the task. v pc. Name ² A descriptive name for the task. It is not used by Free. RTOS, but a debugging aid. v us. Stack. Depth ² Each task has its own unique stack that is allocated by the kernel to the task when the task is created. ² The value specifies the number of words the task stack can hold. Processes and Operating Systems slide 57 COE 306– Introduction to Embedded System– KFUPM

Task Parameters v pv. Parameters ² The value assigned to pv. Parameters will be

Task Parameters v pv. Parameters ² The value assigned to pv. Parameters will be the values passed into the task v ux. Priority ² defines the priority at which the task will execute. ² Priorities can be assigned from 0, which is the lowest priority, to (config. MAX_PRIOIRTIES-1), which is the highest priority. v px. Created. Task ² pass out a handle to the created task; to be used to refer the created task in API calls. § E. g. , change the task priority or delete the task ² Set to NULL if no use for the task handle Processes and Operating Systems slide 58 COE 306– Introduction to Embedded System– KFUPM

Tasks Creation Examples void v. Task. Function 1(void *pv. Parameters); void v. Task. Function

Tasks Creation Examples void v. Task. Function 1(void *pv. Parameters); void v. Task. Function 2(void *pv. Parameters); const char *pc. Text. For. Task 1 = "Task 1 is runningn"; const char *pc. Text. For. Task 2 = "Task 2 is runningn"; x. Task. Create(v. Task. Function 1, "Task 1", 240, (void*) pc. Text. For. Task 1, 1, NULL); x. Task. Create(v. Task. Function 2, "Task 2", 240, (void*) pc. Text. For. Task 2, 2, NULL); v. Task. Start. Scheduler(); Processes and Operating Systems slide 59 COE 306– Introduction to Embedded System– KFUPM

Running a Periodic Task v v. Task. Delay. Until() API Function void v. Task.

Running a Periodic Task v v. Task. Delay. Until() API Function void v. Task. Delay. Until( port. Tick. Type *px. Previous. Wake. Time, port. Tick. Type x. Time. Increment); ) v Parameters to v. Task. Delay. Until() ² specify the exact tick count value at which the calling task should be moved from the Blocked state into the Ready state. ² to be used when a fixed execution period is required. v Example ² v. Task. Delay. Until(&x. Last. Wake. Time, (50 / port. TICK_RATE_MS)); Processes and Operating Systems slide 60 COE 306– Introduction to Embedded System– KFUPM

v. Task. Delay. Until() prototype v px. Previous. Wake. Time ² Holds the time

v. Task. Delay. Until() prototype v px. Previous. Wake. Time ² Holds the time at which the task left the Blocked state. ² Be used as a reference point to compute the time at which the task next leaves the Blocked state. ² The variable pointed by px. Previous. Wake. Time is updated automatically, not to be modified by application code, other than when the variable is first initialized. v x. Time. Increment ² Assume that v. Task. Delay. Util() is being used to implement a task that executes periodically and with a fixed frequency – set by x. Time. Increment. ² Be specified in ‘ticks’. The constant port. TICK_RATE_MS can be used to convert ms to ticks. Processes and Operating Systems slide 61 COE 306– Introduction to Embedded System– KFUPM

Task Example void v. Task. Function 1(void *pv. Parameters) { char *pc. Task. Name;

Task Example void v. Task. Function 1(void *pv. Parameters) { char *pc. Task. Name; port. Tick. Type x. Last. Wake. Time; pc. Task. Name = (char *) pv. Parameters; x. Last. Wake. Time = x. Task. Get. Tick. Count(); for (; ; ) { v. Print. String(pc. Task. Name); v. Print. String. And. Number("Task 1 Run @", x. Last. Wake. Time); v. Task. Delay. Until(&x. Last. Wake. Time, (50 / port. TICK_RATE_MS)); } } Processes and Operating Systems slide 62 COE 306– Introduction to Embedded System– KFUPM

Free. RTOS v An open-source RTOS available at http: //www. freertos. org ² Supports

Free. RTOS v An open-source RTOS available at http: //www. freertos. org ² Supports 34 architectures ² 107, 000 downloads a year ² Professionally developed, strictly quality controlled ² Free to use in commercial products ² Used in market sectors from toys to aircraft navigation ² Browsing the source code § http: //sourceforge. net/projects/freertos/ § Files, pick version § Free. RTOS/Source/ § portable/GCC/ARM 7_LPC 23 xx (compiler / architecture) Processes and Operating Systems slide 63 COE 306– Introduction to Embedded System– KFUPM