Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Real-Time Library: RTX
CSCI 4717/5717 Computer Architecture
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 5 Processes and Threads Copyright © 2008.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
MicroC/OS-II Embedded Systems Design and Implementation.
Using Two Queues. Using Multiple Queues Suspended Processes Processor is faster than I/O so all processes could be waiting for I/O Processor is faster.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
ΜC/OS-III Tasks Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
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.
Scheduling Basic scheduling policies, for OS schedulers (threads, tasks, processes) or thread library schedulers Review of Context Switching overheads.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
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.
Scheduling in µC/OS-III Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
RTX - 51 Objectives  Resources needed  Architecture  Components of RTX-51 - Task - Memory pools - Mail box - Signals.
ECGR-6185 µC/OS II Nayana Rao University of North Carolina at Charlotte.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
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.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Message Passing Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
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.
Synchronization Akos Ledeczi EECE 6354, Fall 2013 Vanderbilt University.
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.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
WORKING OF SCHEDULER IN OS
Getting Started with Micriμm’s μC/OS-III Kernel
Chapter 1: Getting Started with μC/OS-II
REAL-TIME OPERATING SYSTEMS
Processes and threads.
Process concept.
Chapter 13: Porting μC/OS-II
Advanced Operating Systems CIS 720
Process Management Process Concept Why only the global variables?
CS 6560: Operating Systems Design
Topics Covered What is Real Time Operating System (RTOS)
CS703 – Advanced Operating Systems
OPERATING SYSTEMS CS3502 Fall 2017
Scheduler activations
Computer Architecture
Interrupt and Time Management in µC/OS-III
Getting Started with the µC/OS-III Real Time Kernel
Intro to Processes CSSE 332 Operating Systems
Threads and Locks.
Structure of Processes
Computer System Overview
Scheduling in µC/OS-III
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Operating Systems.
Process Description and Control
BIC 10503: COMPUTER ARCHITECTURE
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
EE 472 – Embedded Systems Dr. Shwetak Patel.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Computer System Overview
Computer System Overview
Michael Blinn Ben Hejl Jane McHugh Matthew VanMater
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University
Presentation transcript:

Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University µC/OS-III Tasks Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University

Multitasking Multiple sequential tasks (threads) Creates the illusion of having multiple CPUs Pro: Helps in creating modular, better structured code that is simpler to write and understand Con: Brings in unique problems and hard to find bugs

Tasks Run-to-completion Infinite loop Call any function Must call OSTaskDel() at the end Infinite loop Inside the loop it MUST call a RT service (function) to wait for an event and hence, block. Otherwise, it would be the only task running (not necessarily, if there was a higher priority task that becomes ready due to an interrupt. For example, the idle task does not block. But user tasks should not do this. To do something during idle time, you can hook into the idle task itself.) Call any function Reentrant functions

Task Creation OSTaskCreate() 1: stack 2: the OS can initialize the stack with 0s 3: the OS puts the CPU registers on the stack 4: and sets the stack pointer (SP) to this location (top of the stack) and saves it in the TCB 5: the rest of the TCB (Task Control Block) is initialized OSTaskCreateHook() TCB is placed in the Ready list

Task-Resource Interaction Code. A regular C function, but not allowed to return. Priority. Lower number = higher priority OS_PRIO_MAX: no of levels Multiple tasks at the same priority are allowed CPU registers. As if it had the CPU all to itself. Stack. Needed for preemptive kernels. Store local variables, function calls, used by ISRs, etc. Context switch Allocating the stack Global variables. Need protection! IO devices. Need protection!

Stack size Manual stack size calculation: All function call nesting (return address plus all arguments plus all local variables) All ISR nesting (full CPU context and variables used for each) Multiply by 2 to be safe Tedious and unreliable Dynamic stack size calculation Give tasks large stacks and monitor actual usage: Initialize stack with zeros An extra low priority task can “walk” the stack and store highest usage for all other tasks

Stack overflow detection Hardware support: .StkLimitPtr Can be set close to the end of the stack Desired value stored in TCB OS context switch mechanism takes care of setting .StkLimitPtr corresponding to the new Task Software: .OSTaskSWHook() for CPUs with no hardware check Checked at context switch only, so user needs to leave enough slack

Task States (conceptual) Dormant: OS does not know about it (yet or any more) Ready: all, but the highest priority task that are available for running Running: highest priority task available for running Pending: waiting for an event Interrupted: ISR (possibly nested). ISR can call scheduler (OSIntExit(), so the current task may be switched out. ISRs are only allowed to make Post calls.

Task States (actual) Delay: task call one of the delay functions Pend timeout: the task only blocks for a max time limit and then goes back to ready state Suspend: task can suspend itself (or another task) by OSTaskSuspend(). The only way to return from one of the suspended states is by calling OSTaskResume(). This creates four new states.

Task Control Block (TCB) struct os_tcb { CPU_STK *StkPtr; /* Pointer to current top of stack */ void *ExtPtr; /* Pointer to user definable data for TCB extension */ CPU_STK *StkLimitPtr; /* Pointer used to set stack 'watermark' limit */ OS_TCB *NextPtr; /* Pointer to next TCB in the TCB list */ OS_TCB *PrevPtr; /* Pointer to previous TCB in the TCB list */ OS_TCB *TickNextPtr; OS_TCB *TickPrevPtr; OS_TICK_SPOKE *TickSpokePtr; /* Pointer to tick spoke if task is in the tick list */ CPU_CHAR *NamePtr; /* Pointer to task name */ CPU_STK *StkBasePtr; /* Pointer to base address of stack */ OS_TASK_PTR TaskEntryAddr; /* Pointer to task entry point address */ void *TaskEntryArg; /* Argument passed to task when it was created */ OS_PEND_DATA *PendDataTblPtr; /* Pointer to list containing objects pended on */ OS_OBJ_QTY PendDataTblEntries; /* Size of array of objects to pend on */ CPU_TS TS; /* Timestamp */

Task Control Block (TCB) #if OS_MSG_EN > 0u void *MsgPtr; /* Message received */ OS_MSG_SIZE MsgSize; #endif #if OS_CFG_TASK_Q_EN > 0u OS_MSG_Q MsgQ; /* Message queue associated with task */ #if OS_CFG_TASK_PROFILE_EN > 0u CPU_TS MsgQPendTime; /* Time it took for signal to be received */ CPU_TS MsgQPendTimeMax; /* Max amount of time it took for signal to be received */ #if OS_CFG_FLAG_EN > 0u OS_FLAGS FlagsPend; /* Event flag(s) to wait on */ OS_OPT FlagsOpt; /* Options (See OS_OPT_FLAG_xxx) */ OS_FLAGS FlagsRdy; /* Event flags that made task ready to run */ #if OS_CFG_TASK_REG_TBL_SIZE > 0u OS_REG RegTbl[OS_CFG_TASK_REG_TBL_SIZE]; /* Task specific registers */

Task Control Block (TCB) OS_SEM_CTR SemCtr; /* Task specific semaphore counter */ #if OS_CFG_TASK_PROFILE_EN > 0u CPU_TS SemPendTime; /* Time it took for signal to be received */ CPU_TS SemPendTimeMax; /* Max amount of time it took for signal to be received */ #endif #if OS_CFG_TASK_SUSPEND_EN > 0u OS_NESTING_CTR SuspendCtr; /* Nesting counter for OSTaskSuspend() */ CPU_STK_SIZE StkSize; /* Size of task stack (in number of stack elements) */ #if OS_CFG_STAT_TASK_STK_CHK_EN > 0u CPU_STK_SIZE StkUsed; /* Number of stack elements used from the stack */ CPU_STK_SIZE StkFree; /* Number of stack elements free on the stack */ OS_OPT Opt; /* Task options as passed by OSTaskCreate() */

Task Control Block (TCB) /* DELAY / TIMEOUT */ OS_TICK TickCtrPrev; /* Previous time when task was ready */ OS_TICK TickCtrMatch; /* Absolute time when task is going to be ready */ OS_TICK TickRemain; /* Number of ticks remaining for a match (updated at ... */ /* ... run-time by OS_StatTask() */ OS_TICK TimeQuanta; OS_TICK TimeQuantaCtr; #if OS_CFG_TASK_PROFILE_EN > 0u OS_CPU_USAGE CPUUsage; /* CPU Usage of task (0-100%) */ OS_CTX_SW_CTR CtxSwCtr; /* Number of time the task was switched in */ CPU_TS CyclesDelta; /* value of OS_TS_GET() - .CyclesStart */ CPU_TS CyclesStart; /* Snapshot of cycle counter at start of task resumption */ OS_CYCLES CyclesTotal; /* Total number of # of cycles the task has been running */ #endif #ifdef CPU_CFG_INT_DIS_MEAS_EN CPU_TS IntDisTimeMax; /* Maximum interrupt disable time */ #if OS_CFG_SCHED_LOCK_TIME_MEAS_EN > 0u CPU_TS SchedLockTimeMax; /* Maximum scheduler lock time */

Task Control Block (TCB) OS_STATE PendOn; /* Indicates what task is pending on */ OS_STATUS PendStatus; /* Pend status */ OS_STATE TaskState; /* See OS_TASK_STATE_xxx */ OS_PRIO Prio; /* Task priority (0 == highest) */ #if OS_CFG_DBG_EN > 0u OS_TCB *DbgPrevPtr; OS_TCB *DbgNextPtr; CPU_CHAR *DbgNamePtr; #endif };

Idle Task Lowest priority task Not blocking void OS_IdleTask (void *p_arg) { while (DEF_ON) { CPU_CRITICAL_ENTER(); OSIdleTaskCtr++; #if OS_CFG_STAT_TASK_EN > 0u OSStatTaskCtr++; #endif CPU_CRITICAL_EXIT(); OSIdleTaskHook(); } Lowest priority task Not blocking Hook allows the user to put the processor in a low power mode, for example. (interrupts typically wake up CPU.)

Tick Task Hardware timer interrupts current task Tick ISR takes care of hardware and OS related issues and notifies Tick Task (Post) ISR returns, depending on priorities Tick Task runs eventually. Tick Task usually has relatively high priority.

Tick Task cont’d. Tickwheel: stores all tasks waiting for a time event (delayed or pending with a timeout) Optimized to make task handling fast: only one of the spokes needs to be looked at any one time List is ordered by CtrMatch so only first (few) items needs to be looked at Tick Task removes tasks whose delay (timeout) expired

Statistics Task µC/OS-III can provide run time statistics: Overall CPU utilization Per task CPU utilization Per task stack usage OS_CFG_STAT_TASK_EN in os_cfg.h Must call OSStatTaskCPUUsageInit() at the beginning of the first task before creating any more tasks Statistics are stored in each task’s TCB

Timer Task Timers are countdown counters that perform a user-specific action via a callback function OS_CFG_TMR_EN in os_cgf.h Callback is called from the Timer Task context Driven by Tick ISR but typically signaled at a lower rate

ISR Handler Task µC/OS-III can defer event posts from ISRs if the OF_CFG_ISR_POST_DEFERRED_EN is set Used when critical sections are handled by locking/unlocking the scheduler (as opposed to disabling interrupts) ISR stores posts in a queue, the ISR Handler Task actually make the posts Has the highest priority (no other task can have it) Used to minimize interrupt disable time