Chapter 4: Task Management

Slides:



Advertisements
Similar presentations
Chapter 3 Process Description and Control
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Real-Time Library: RTX
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Chapter 12 Memory Management. The dynamic memory allocation can be achieved by using malloc() and free() function Using malloc() and free(0 in an embedded.
Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II.
Chapter 14 80x86, Large Model Port Porting to real mode and for the large model –Real mode: 1Mb memory space (pointers in this model require 32 bits) –In.
Process Description and Control Module 1.0. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Processes CSCI 444/544 Operating Systems Fall 2008.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Computer Organization and Architecture
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
MicroC/OS-II Embedded Systems Design and Implementation.
Process Description and Control A process is sometimes called a task, it is a program in execution.
Real-Time Operating Systems Suzanne Rivoire November 20, 2002
ΜC/OS-III Tasks Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: CMPBooks.
System Calls 1.
國立台灣大學 資訊工程學系 Introduction to uC/OS-II
Kernel structure & Task management © P.J.Fondse April 2010.
The  C/OS-II Real-Time Operating System.  C/OS-II Real-time kernel –Portable, scalable, preemptive RTOS –Ported to over 90 processors Pronounced “microC.
2-1 The critical section –A piece of code which cannot be interrupted during execution Cases of critical sections –Modifying a block of memory shared by.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Overview Task State Diagram Task Priority Idle Hook AND Co-Routines
RTX - 51 Objectives  Resources needed  Architecture  Components of RTX-51 - Task - Memory pools - Mail box - Signals.
Java Thread and Memory Model
MicroC/OS-II S O T R.  MicroC/OS-II (commonly termed as µC/OS- II or uC/OS-II), is the acronym for Micro-Controller Operating Systems Version 2.  It.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
uC/OS-II Real-Time Systems Concepts Kernel Structure Task Management
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Lab 3 Department of Computer Science and Information Engineering National Taiwan University Lab3 – μC/OS 2015/10/13/ 13 1.
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 1 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Chapter 7: Semaphore Management Seulki Lee 1. Semaphore?  A protocol mechanism offered by most multitasking kernels  Control access to a shared resource.
Chapter 3: Kernel Structure 1. Objectives To understand what a task is. To learn how μ C/OS-II manages tasks. To know how an interrupt service routine.
Real Time Operating Systems Johan Dams
Chapter 13: Porting μC/OS-II 1. Outline Requirements Hardware Software Tasks of Porting µC/OS-II OS_CPU_C.H OS_CPU_C.C OS_CPU_A.ASM Testing a port 2.
Chapter 3: Kernel Structure 1. Objectives To understand what a task is. To learn how μ C/OS-II manages tasks. To know how an interrupt service routine.
Chapter 1: Getting Started with μC/OS-II 1. kernel Introduction 2 LinuxμC/OS-II Task (process) kernel Device driver User mode (0-3G) (Kernel mode) 3G-4G.
Chapter 5: Time Management 1. Time Management OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() 2.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 5 Time Management use clock tick to provide a periodic interrupt to keep track of time delays and timeouts The frequency of the clock tick depends.
Lab 5 Department of Computer Science and Information Engineering National Taiwan University Lab5 – μC/OS 2016/5/31/ 13 1.
LPC2148's RTOS Bruce Chhuon 4/10/07. What is a Real Time Operating System? ● A Real Time Operating System (RTOS) manages hardware and software resources.
Chapter 3: Kernel Structure
Chapter 1: Getting Started with μC/OS-II
Processes and threads.
Process concept.
Chapter 13: Porting μC/OS-II
PROCESS MANAGEMENT IN MACH
Topics Covered What is Real Time Operating System (RTOS)
Process Description and Control
Run-time organization
System Structure and Process Model
Introduction to uC/OS-II
Operating Systems.
Process Description and Control
Process Description and Control
Process Description and Control
Computer System Laboratory
Mr. M. D. Jamadar Assistant Professor
Chapter 6: Event Control Block (ECB)
Chapter 3: Process Management
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Presentation transcript:

Chapter 4: Task Management

Objectives To know (and trace the codes of) the services: Task prototype Create a task Task stacks Stack checking Delete a task Change a task’s priority Suspend a task Resume a task Query task information

Tasks A task could be periodic or aperiodic. void YourTask (void *pdata) { for (;;) { /* USER CODE */ Call one of uC/OS-II’s services: OSMboxPend(); OSQPend(); OSSemPend(); OSTaskDel(OS_PRIO_SELF); OSTaskSuspend(OS_PRIO_SELF); OSTimeDly(); OSTimeDlyHMSM(); } void YourTask (void *pdata) { /* USER CODE */ OSTaskDel(OS_PRIO_SELF); } A job is created on demand. A task consists of periodically invoked jobs

Creating a Task You must create at least one task before multitasking is started. Calling OSInit() and OSStatInit() will implicitly create 2 tasks (it must be done before OSStart()). An ISR can not create a task. ??? Related data structures are created according to the given parameters. A Task Control Block (TCB). The stack of the created task. The priority table. After a new task is created, the scheduler is called if multitasking is enabled.

OSTaskCreate() task: a pointer-to-function points to the entry point of a task (note the syntax). pdata: a parameter passed to the task. ptos: a pointer points to the top-of-stack. prio: task priority. id: task id, for future extension. pbos: a pointer points to the bottom-of-stack. stk_size: the stack size in the number of elements (OS_STK bytes each) 1 word under x86 pext: an user-defined extension to the TCB. opt: the options specified to create the task.

OSTaskCreate() Check that the task priority is valid Set up the task stack OSTaskStkInit() Obtain and initialize OS_TCB from TCB pool OSTCBInit() Call OSTaskCreateHook() to extend the functionality Call OSSched() If the task is created with multitasking started

Occupying a priority table slot and re-enable interrupts immediately. OSTaskCreate() INT8U OSTaskCreate (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT8U prio) { OS_STK *psp; INT8U err; OS_ENTER_CRITICAL(); if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { OSTCBPrioTbl[prio] = (OS_TCB *)1; OS_EXIT_CRITICAL(); psp = (OS_STK *)OSTaskStkInit(task, pdata, ptos, 0); Occupying a priority table slot and re-enable interrupts immediately. Stack initialization is a hardware-dependant implementation. (Because of the growing direction...)

Create a corresponding TCB and connect it with the priority table. OSTaskCreate() err = OS_TCBInit(prio, psp, (OS_STK *)0, 0, 0, (void *)0, 0); if (err == OS_NO_ERR) { OS_ENTER_CRITICAL(); OSTaskCtr++; OS_EXIT_CRITICAL(); if (OSRunning == TRUE) { OS_Sched(); } } else { OSTCBPrioTbl[prio] = (OS_TCB *)0; /*????*/ return (err); return (OS_PRIO_EXIST); Create a corresponding TCB and connect it with the priority table. If the task is created with multitasking started, the scheduler is called.

!!!!!!!!

OSTaskCreateExt() task: a pointer-to-function points to the entry point of a task (note the syntax). pdata: a parameter passed to the task. ptos: a pointer points to the top-of-stack. prio: task priority. id: task id, for future extension. pbos: a pointer points to the bottom-of-stack. stk_size: the stack size in the number of elements (OS_STK bytes each) 1 word under x86 pext: an user-defined extension to the TCB. opt: the options specified to create the task.

OSTaskCreateExt() More flexibility, at the expense of overhead The first four arguments are the same as OSTaskCreate() id, assign a unique identifier for the task pbos, a pointer that can perform stack checking stk_size, specifies the size of the stack pext, a pointer to extend the OS_TCB opt, specifies whether stack checking is performed

OSTaskCreateExt() The stack is required to be cleared The stack grows toward low address, so the starting address is bos. The stack grows toward high address, so the starting address is tos.

OSTaskCreateExt()

Task Stacks Stack grows either toward high memory address or low memory space under different processors. Operations over stacks might not be byte-wise. The element size might not be 1 byte (16 bits under x86). Defined by a macro OS_STK.

Task Stacks OS_STK TaskStack[TASK_STACK_SIZE]; #if OS_STK_GROWTH == 0 OSTaskCreate(task, pdata, &TaskStack[0], prio); #else OSTaskCreate(task, pdata, &TaskStack[TASK_STACK_SIZE-1], prio); #endif OS_STK_GROWTH == 0 Stacks grow toward high addresses. OS_STK_GROWTH == 1 Stacks grow toward low addresses.

Task Stacks The stack must be a contiguous memory space. Stack space can be statically declared variables or dynamically allocated space (malloc()???).

Task Stacks Fragmentation might cause memory allocations (for stacks) failed.

Stack Checking Stack checking intends to determine the maximum run-time usage of stacks. Computes the amount of free stack space by “walking” from the bottom of the stack until a nonzero value is found

To do stack checking Set OS_TASK_CREATE_EXT to 1 in OS_CFG.h Create your tasks by using OSTaskCreateExt() with options OS_TASK_OPT_STK_CHK + OS_TASK_OPT_SRK_CLR and give the tasks reasonably large stacks. Call OSTaskStkChk() to determine the stack usage of a certain task. Reduce the stack size if possible, once you think you had run enough simulations.

Stack Checking

For either stack growing direction… Counting from BOS until a non-zero element is encountered.

Deleting a Task Deleting a task means that the data structures (e.g., TCB) corresponding to the task-to-delete would be removed from main-memory. The code for the task will not be deleted Return to the “DORMANT” state Deleting a task is slightly more complicated than creating it since every resources/objects held by the task must be returned to the operating system. OSTaskDelReq()

Procedures Argument checking Prevent from deleting an idle task Prevent from deleting a task from within an ISR Verify that the task to be deleted does exist Remove the task from ready list or other lists Add a preemption point Set .OSTCBStat/.OSTCBDly to OS_STAT_RDY/0 OS_ENTER_CRITICAL(); OS_EXIT_CRITICAL(); Call OSTaskDelHook() Remove the OS_TCB from the priority table Call OSSched()

INT8U OSTaskDel (INT8U prio) { OS_EVENT *pevent; OS_FLAG_NODE *pnode; OS_TCB *ptcb; BOOLEAN self; if (OSIntNesting > 0) { return (OS_TASK_DEL_ISR); } OS_ENTER_CRITICAL(); if (prio == OS_PRIO_SELF) { prio = OSTCBCur->OSTCBPrio; ptcb = OSTCBPrioTbl[prio]; if (ptcb != (OS_TCB *)0) { if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0x00) { OSRdyGrp &= ~ptcb->OSTCBBitY; pevent = ptcb->OSTCBEventPtr; if (pevent != (OS_EVENT *)0) { if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) { pevent->OSEventGrp &= ~ptcb->OSTCBBitY; pnode = ptcb->OSTCBFlagNode; if (pnode != (OS_FLAG_NODE *)0) { OS_FlagUnlink(pnode); We do not allow to delete a task within ISR’s, because the ISR might currently interrupts that task. Clear the corresponding bit of the task-to-delete in the ready list. If the row are all 0’s, then clear the RdyGrp bit also. Remove the task from the event control block since we no longer wait for the event.

ptcb->OSTCBStat = OS_STAT_RDY; if (OSLockNesting < 255) { Prevent tickISR from making this task ready when interrupts are re-enabled later. ptcb->OSTCBDly = 0; ptcb->OSTCBStat = OS_STAT_RDY; if (OSLockNesting < 255) { OSLockNesting++; } OS_EXIT_CRITICAL(); OS_Dummy(); OS_ENTER_CRITICAL(); if (OSLockNesting > 0) { OSLockNesting--; OSTaskDelHook(ptcb); OSTaskCtr--; OSTCBPrioTbl[prio] = (OS_TCB *)0; if (ptcb->OSTCBPrev == (OS_TCB *)0) { ptcb->OSTCBNext->OSTCBPrev = (OS_TCB *)0; OSTCBList = ptcb->OSTCBNext; } else { ptcb->OSTCBPrev->OSTCBNext = ptcb->OSTCBNext; ptcb->OSTCBNext->OSTCBPrev = ptcb->OSTCBPrev; ptcb->OSTCBNext = OSTCBFreeList; OSTCBFreeList = ptcb; OS_Sched(); return (OS_NO_ERR); return (OS_TASK_DEL_ERR); Prevent this task from being resumed since we are not in the ready list now (a “ready task can not be resumed). (clear the OS_STATE_SUSPEND bit) Lock the scheduler. Interrupts are re-enabled for a while (note that the scheduler is locked). *What does OS_dummy() do? Remove the task from the double linked list

OSTaskDelReq() Tell the task that owns memory buffers or semaphore to delete itself Procedures If the task’s priority is OS_PRIO_SELF Return the flag of OSTCBDelReq Otherwise Set OSTCBDelReq of the task to OS_TASK_DEL_REQ

Example void RequestorTask(void *pdata) { for (;;) { /*application code*/ if (the task “TaskToBeDeleted()” needs to be deleted) { while(OSTaskDelReq(TASK_TO_DEL_PRIO) != OS_TASK_NOT_EXIT) OSTimeDly(1); } DelReq = 0 TCB (ReqTask) void TaskToBeDeleted(void *pdata) { while(1) { /*application code*/ if (OSTaskDelReq(OS_PRIO_SELF) == OS_TASK_DEL_REQ) { /*release any owned resources; de-allocated any dynamic memory; */ OSTaskDel(OS_PRIO_SELF); } else { /*application code)*/ } DelReq = 1 TCB (TaskDel)

Changing a Task’s Priority When you create a new task, you assign the task a priority At run time, you can change this priority dynamically by calling OSTaskChangePrio Cannot change the priority of the idle task INT8U OSTaskChangePrio(INT8U oldPrio, INT8U newPrio) INT8U OSTaskChangePrio(INT8U TaskId, INT8U newPrio)

Procedures Reserve the new priority by OSTCBPrioTbl[newprio] = (OS_TCB *) 1; Remove the task from the priority table Insert the task into new location of the priority table Change the OS_TCB of the task Call OSSched()

in ready queue the task is ready to run

in waiting queue

Suspending a Task A suspended task can only resumed by calling the OSTaskResume() function call If a task being suspended is also waiting for time to expire, the suspension needs to be removed and the time needs to expire in order for the task ready to run. INT8U OSTaskSuspend(INT8U prio) INT8U OSTaskResume(INT8U prio)

Procedure – OSTaskSuspend() Check the input priority Remove the task from the ready list Set the OS_STAT_SUSPEND flag in OS_TCB Call OSSched()

INT8U OSTaskSuspend (INT8U prio) { BOOLEAN self; OS_TCB *ptcb; OS_ENTER_CRITICAL(); /* See if suspending self*/ if (prio == OS_PRIO_SELF) { prio = OSTCBCur->OSTCBPrio; self = TRUE; } else if (prio == OSTCBCur->OSTCBPrio) { } else { self = FALSE; } ptcb = OSTCBPrioTbl[prio]; /* Task to suspend must exist*/ if (ptcb == (OS_TCB *)0) { OS_EXIT_CRITICAL(); return (OS_TASK_SUSPEND_PRIO); Call OS_Sched()?

/* Make task not ready*/ if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0x00) { OSRdyGrp &= ~ptcb->OSTCBBitY; } /* Status of task is 'SUSPENDED'*/ ptcb->OSTCBStat |= OS_STAT_SUSPEND; OS_EXIT_CRITICAL(); /* Context switch only if SELF*/ if (self == TRUE) { OS_Sched(); return (OS_NO_ERR);

Procedure – OSTaskResume() Check the input priority Clear the OS_STAT_SUSPEND bit in the OSTCBStat field Set OSTCBDly to 0 Call OSSched()

Resuming a Task INT8U OSTaskResume (INT8U prio) { OS_TCB *ptcb; OS_ENTER_CRITICAL(); ptcb = OSTCBPrioTbl[prio]; /* Task to suspend must exist*/ if (ptcb == (OS_TCB *)0) { OS_EXIT_CRITICAL(); return (OS_TASK_RESUME_PRIO); }

/* Task must be suspended */ if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) != 0x00) { /* Remove suspension */ if (((ptcb->OSTCBStat &= ~OS_STAT_SUSPEND) == OS_STAT_RDY) && /* Must not be delayed */ (ptcb->OSTCBDly == 0)) { /* Make task ready to run */ OSRdyGrp |= ptcb->OSTCBBitY; OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX; OS_EXIT_CRITICAL(); OS_Sched(); } else { } return (OS_NO_ERR); return (OS_TASK_NOT_SUSPENDED);

Getting Information About a Task OSTaskQuery return a copy of the contents of the desired task’s OS_TCB To call OSTaskQuery, your application must allocate storage for an OS_TCB Only you this function to SEE what a task is doing don’t modify the contains (OSTCBNext, OSTCBPrev)

INT8U OSTaskQuery (INT8U prio, OS_TCB *pdata) { OS_TCB *ptcb; OS_ENTER_CRITICAL(); if (prio == OS_PRIO_SELF) { prio = OSTCBCur->OSTCBPrio; } ptcb = OSTCBPrioTbl[prio]; /* Task to query must exist*/ if (ptcb == (OS_TCB *)0) { OS_EXIT_CRITICAL(); return (OS_PRIO_ERR); /* Copy TCB into user storage area*/ memcpy(pdata, ptcb, sizeof(OS_TCB)); return (OS_NO_ERR);