CS 4101 Tasks and Scheduling Prof ChungTa King

  • Slides: 23
Download presentation
CS 4101 嵌入式系統概論 Tasks and Scheduling Prof. Chung-Ta King Department of Computer Science National

CS 4101 嵌入式系統概論 Tasks and Scheduling Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan (Materials From Using the Free. RTOS Real Time Kernel, Richard Barry, Study of an operating system: Free. RTOS, Nicolas Melot, and http: //www. eecs. umich. edu/eecs/courses/eecs 373/Lec/RTOS 2. pptx) National Tsing Hua University

Outline • Tasks in Free. RTOS - Task states - Task creation and deletion

Outline • Tasks in Free. RTOS - Task states - Task creation and deletion • Task scheduler of Free. RTOS - Priority and ticks Delaying a task Suspending and resuming a task Idle task hook Changing priority National Tsing Hua University 1

Tasks in Free. RTOS • In Free. RTOS, computations are organized around tasks, which

Tasks in Free. RTOS • In Free. RTOS, computations are organized around tasks, which are the most basic unit of scheduling - A task is a thread • A task is a function that must return void and take a void pointer parameter: void ATask. Function(void *pv. Parameters); - A task normally runs in an infinite loop and must not return • You inform the scheduler of - The task’s resource needs (stack space, priority) - Any arguments the task needs National Tsing Hua University

Task States in Free. RTOS • Running: - Task is actually executing - Only

Task States in Free. RTOS • Running: - Task is actually executing - Only one task can exist in Running state at any one time (assuming 1 -core system) • Ready: - Task is ready to execute but a task of equal or higher priority is Running National Tsing Hua University 3

Task States in Free. RTOS • Blocked: - Task is waiting for some event

Task States in Free. RTOS • Blocked: - Task is waiting for some event • Time: if a task calls v. Task. Delay() it will be blocked until the delay period has expired • Resource: tasks can also block waiting for queue and semaphore events • Suspended: - Much like blocked, but not waiting for anything - Tasks will only enter or exit the suspended state when explicitly commanded to do so through the v. Task. Suspend() and v. Task. Resume() API calls respectively National Tsing Hua University 4

Task Creation • x. Task. Create(pv. Task. Code, pc. Name, us. Stack. Depth, pv.

Task Creation • x. Task. Create(pv. Task. Code, pc. Name, us. Stack. Depth, pv. Parameters, ux. Priority, px. Created. Task) - pv. Task. Code: pointer to task entry function, implemented to never return - pc. Name: a descriptive name for the task to facilitate debugging - us. Stack. Depth: size of task stack, specified as the number of variables that the stack can hold - pv. Parameters: pointer to parameters for the task - ux. Priority: priority at which the task should run; low numeric priority values denote low priority - pv. Created. Task: pass back a handle to reference the task National Tsing Hua University 5

Task Deletion • A created task commonly runs in an infinite loop, but it

Task Deletion • A created task commonly runs in an infinite loop, but it may invoke v. Task. Destroy(NULL) to end void v. Task. Delete(Task. Handle_t x. Task); - x. Task: handle of the task to be deleted; NULL to cause the calling task to be deleted - Remove a task from the RTOS kernels management void v. Other. Function(void) { Task. Handle_t x. Handle = NULL; // Create the task, storing the handle. x. Task. Create(v. Task. Code, "NAME", STACK_SIZE, NULL, tsk. IDLE_PRIORITY, &x. Handle); // Use the handle to delete the task. if(x. Handle != NULL) v. Task. Delete(x. Handle); } National Tsing Hua University 6

Outline • Tasks in Free. RTOS - Task states - Task creation and deletion

Outline • Tasks in Free. RTOS - Task states - Task creation and deletion • Task scheduler of Free. RTOS - Priority and ticks Delaying a task Suspending and resuming a task Idle task hook Changing priority National Tsing Hua University 7

Scheduler of Free. RTOS • Task scheduling: decide which task in “Ready” state has

Scheduler of Free. RTOS • Task scheduling: decide which task in “Ready” state has to be run at a given time • RTOS scheduler is started by calling void v. Task. Start. Scheduler(void); - RTOS kernel now has control over which tasks are executed and when (Arduino invokes it automatically) • An Idle task is automatically created by scheduler when v. Task. Start. Scheduler() is called - Because CPU always needs something to execute must always be one task that can enter Running state - The idle task has lowest priority (priority 0) to never prevent a higher priority task from running National Tsing Hua University 8

Task Priority • Free. RTOS only uses the priorities of tasks to decide which

Task Priority • Free. RTOS only uses the priorities of tasks to decide which task to run next • The ux. Priority parameter of x. Task. Create() assigns an initial priority to the task being created - Can be changed using v. Task. Priority. Set() - Low numeric priority values denote low priority tasks • Scheduler always selects the highest priority task that is able to run - When more than one task of the same priority is able to run, the scheduler uses round-robin National Tsing Hua University 9

Ticks • To select the next task to run, the scheduler has to be

Ticks • To select the next task to run, the scheduler has to be executed periodically - By a periodic interrupt called the tick interrupt • Free. RTOS API calls always specify time in tick interrupts (or just ‘ticks’) • ‘Tick count’ is the total number of tick interrupts that have occurred since the scheduler was started National Tsing Hua University 10

Sample Code for Task Priority static const char *pc. Text. For. Task 1 =

Sample Code for Task Priority static const char *pc. Text. For. Task 1 = “T 1 runningrn”; static const char *pc. Text. For. Task 2 = “T 2 runningtn”; int main(void) { /* Create the first task at priority 1. */ x. Task. Create(v. Task. Function, "Task 1", 1000, (void*)pc. Text. For. Task 1, 1, NULL); /* Create the second task at priority 2. */ x. Task. Create(v. Task. Function, "Task 2", 1000, (void*)pc. Text. For. Task 2, 2, NULL); /* Start scheduler so the tasks start executing. */ v. Task. Start. Scheduler(); return 0; } National Tsing Hua University 11

Running Multiple Periodic Tasks • It is inconvenient to run multiple periodic jobs using

Running Multiple Periodic Tasks • It is inconvenient to run multiple periodic jobs using a single Arduino loop, e. g. - Measure the light using the photoresistor at 1 Hz - Measure the sound intensity using microphone at 2. 5 Hz - Measure the distance of an object using the ultrasonic sensor at 0. 4 Hz • Even if we run each job in a Free. RTOS task, we still need a way to schedule the tasks at specified time Use blocked state with v. Task. Delay() National Tsing Hua University 12

Delaying a Task • void v. Task. Delay(const Tick. Type_t x. Ticks. To. Delay)

Delaying a Task • void v. Task. Delay(const Tick. Type_t x. Ticks. To. Delay) - Places the calling task into the Blocked state for a fixed number of tick interrupts - While in the Blocked state, the task will not use any processing time equivalent to timer interrupt - Note: Frequency of the periodic task depends on the path of the code, as well as other tasks and interrupt activities void v. Task. Function(void * pv. Parameters) { const Tick. Type_t x. Delay = 500/port. TICK_PERIOD_MS; for( ; ; ) { v. Toggle. LED(); v. Task. Delay(x. Delay); /* Block for 500 ms. */ } } National Tsing Hua University 13

Delaying a Task • void v. Task. Delay. Until(Tick. Type_t *px. Previous. Wake. Time,

Delaying a Task • void v. Task. Delay. Until(Tick. Type_t *px. Previous. Wake. Time, const Tick. Type_t x. Time. Increment); - Delay a task until a specified time to ensure a constant execution frequency • v. Task. Delay() specifies a time at which the task wishes to unblock relative to the time at which v. Task. Delay() is called • v. Task. Delay. Until() specifies an absolute time to unblock the task - px. Previous. Wake. Time: the time that task last left Blocked state • This time is used as a reference point to calculate the time at which the task should next leave the Blocked state - x. Time. Increment: frequency of the periodic task in ‘ticks’ - v. Task. Delay. Until() will return immediately without blocking if the specified wake time is already in the past National Tsing Hua University 14

Sample Code for Periodic Tasks void v. Task. Function(void * pv. Parameters){ // Perform

Sample Code for Periodic Tasks void v. Task. Function(void * pv. Parameters){ // Perform an action every 10 ticks Tick. Type_t x. Last. Wake. Time; const Tick. Type_t x. Frequency = 10; // Initialize the x. Last. Wake. Time variable with the current time. x. Last. Wake. Time = x. Task. Get. Tick. Count(); for( ; ; ) { // Wait for the next period. v. Task. Delay. Until(&x. Last. Wake. Time, x. Frequency); // Perform action here. } } National Tsing Hua University 15

Suspending and Resuming a Task • void v. Task. Suspend(Task. Handle_t x. Task. To.

Suspending and Resuming a Task • void v. Task. Suspend(Task. Handle_t x. Task. To. Suspend) - Suspend any task - Task will never get any microcontroller processing time, no matter what its priority - Calls to v. Task. Suspend() are not accumulative, i. e. calling v. Task. Suspend () twice on the same task still only requires one call to v. Task. Resume () to ready the suspended task • void v. Task. Resume(Task. Handle_t x. Task. To. Resume) - Resumes a suspended task - A task that has been suspended by one or more calls to v. Task. Suspend () will be made available for running again by a single call to v. Task. Resume () National Tsing Hua University 16

Sample Code for Suspending a Task void v. AFunction(void) { Task. Handle_t x. Handle;

Sample Code for Suspending a Task void v. AFunction(void) { Task. Handle_t x. Handle; // Create a task, storing the handle. x. Task. Create(v. Task. Code, "NAME", STACK_SIZE, NULL, tsk. IDLE_PRIORITY, &x. Handle); //. . . // Use the handle to suspend the created task v. Task. Suspend(x. Handle); //. . . // Created task will not run during this period //. . . // Resume the suspended task ourselves. v. Task. Resume(x. Handle); } National Tsing Hua University 17

Idle Task Hook • Recall: - An Idle task is created when v. Task.

Idle Task Hook • Recall: - An Idle task is created when v. Task. Start. Scheduler() is called so that there is at least always a task to run • Can add special functionality into the idle task through an idle hook (or call-back) function void v. Application. Idle. Hook(void); - A function that is automatically called by the idle task once per iteration of the idle task loop • Common uses for the Idle task hook include: - Executing low priority, background or continuous task - Measuring the amount of spare processing capacity - Placing the processor into a low power mode National Tsing Hua University 18

Changing the Priority of a Task • void v. Task. Priority. Set(Task. Handle_t px.

Changing the Priority of a Task • void v. Task. Priority. Set(Task. Handle_t px. Task, UBase. Type_t ux. New. Priority); - Set the priority of any task - px. Task: handle of the task whose priority is being modified (NULL for itself) - ux. New. Priority: the priority to which the task is to be set • Capped to the maximum available priority of (config. MAX_PRIORITIES – 1), where config. MAX_PRIORITIES is a compile time option set in the Free. RTOSConfig. h header file - A context switch will occur before the function returns if the priority being set is higher than the currently executing task • unsigned port. BASE_TYPE ux. Task. Priority. Get(x. Task. Handle px. Task); National Tsing Hua University 19

Sample Code for Changing Priority void v. Task 1(void *pv. Parameters) { unsigned port.

Sample Code for Changing Priority void v. Task 1(void *pv. Parameters) { unsigned port. BASE_TYPE ux. Priority; ux. Priority = ux. Task. Priority. Get(NULL); // my priority for( ; ; ) { v. Print. String(“Task 1 raises Task 2’s priorityrn" ); v. Task. Priority. Set(x. Task 2 Handle, (ux. Priority + 1)); /* For Task 1 to reach this point Task 2 must set its priority back down to below Task 1. */ } } void v. Task 2(void *pv. Parameters) { unsigned port. BASE_TYPE ux. Priority; ux. Priority = ux. Task. Priority. Get(NULL); for( ; ; ) { v. Print. String(“Task 2 lower its priorityrn"); v. Task. Priority. Set(NULL, (ux. Priority - 2)); } } National Tsing Hua University 20

Sample Code for Changing Priority x. Task. Handle x. Task 2 Handle; int main(void)

Sample Code for Changing Priority x. Task. Handle x. Task 2 Handle; int main(void) { // Create Task 1 at priority 2 x. Task. Create(v. Task 1, "Task 1", 1000, NULL, 2, NULL); // Create Task 2 at priority 1 x. Task. Create(v. Task 2, "Task 2", 1000, NULL, 1, &x. Task 2 Handle); // Start the scheduler v. Task. Start. Scheduler(); /* If all is well, then main() will never reach here as the scheduler will now be running the tasks. If main() does reach here, then it is likely that there was insufficient heap memory available for the idle task to be created. */ for( ; ; ); } National Tsing Hua University 21

Summary • Free. RTOS runs Fixed Priority Preemptive Scheduling - Each task is assigned

Summary • Free. RTOS runs Fixed Priority Preemptive Scheduling - Each task is assigned a priority Each task can exist in one of several states Only one task can exist in Running state at any one time The scheduler will always select the highest priority Ready state task to enter the Running state - Scheduling is made at each tick interrupt National Tsing Hua University 22