Preemptive Scheduling

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

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Chapter 3 Process Description and Control
Process management Information maintained by OS for process management  process context  process control block OS virtualization of CPU for each process.
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.
Real-Time Library: RTX
Chapter 6 Limited Direct Execution
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
1 Operating Systems and Protection CS Goals of Today’s Lecture How multiple programs can run at once  Processes  Context switching  Process.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CSE Fall Introduction - 1 What is an Embedded Systems  Its not a desktop system  Fixed or semi-fixed functionality (not user programmable)
1 Computer System Overview OS-1 Course AA
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
OS Spring’03 Introduction Operating Systems Spring 2003.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
Process Description and Control A process is sometimes called a task, it is a program in execution.
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.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Introduction to Embedded Systems
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.
Introduction to Processes CS Intoduction to Operating Systems.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Chapter 3 Process Description and Control
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Processes Introduction to Operating Systems: Module 3.
Processes and Process Control 1. Processes and Process Control 2. Definitions of a Process 3. Systems state vs. Process State 4. A 2 State Process Model.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Multi-Tasking The Multi-Tasking service is offered by VxWorks with its real- time kernel “WIND”.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
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.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Preemptive Scheduling
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.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Processes and threads.
Process concept.
Process Management Process Concept Why only the global variables?
CS 6560: Operating Systems Design
Scheduler activations
Mechanism: Limited Direct Execution
Intro to Processes CSSE 332 Operating Systems
Threads, SMP, and Microkernels
Process Description and Control
Lecture Topics: 11/1 General Operating System Concepts Processes
Process Description and Control
Lecture 4- Threads, SMP, and Microkernels
Threads Chapter 4.
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Process Description and Control
Operating Systems: A Modern Perspective, Chapter 6
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CS510 Operating System Foundations
Presentation transcript:

Preemptive Scheduling Lecture 18 Embedded Systems

Big Picture Methods learned so far What we will learn next We’ve been using a foreground/background system Interrupt service routines run in foreground Task code runs in background Limitations Must structure task functions to run to completion, regardless of “natural program structure” – can only yield processor at end of task Response time of task code is not easily controlled, in worst case depends on how long each other task takes to run What we will learn next How to share processor flexibly among multiple tasks, while not requiring restructuring of code Goal: share MCU efficiently Embedded Systems: To simplify our program design by allowing us to partition design into multiple independent components PCs/Workstations/Servers: To allow multiple users to share a computer system Embedded Systems

Example: Secure Answering Machine (SAM) FLASH MEMORY ARRAY PAGE SIZE = BUFFER SIZE BUFFER 1 BUFFER 2 I/O INTERFACE SI SO SCK Testing the limits of our cooperative round-robin scheduler Secure Answering Machine Stores encrypted voice messages in serial Flash memory Want to delete messages fully, not just remove entry from directory (as with file systems for PCs) Also have a user interface: LCD, switches Embedded Systems

SAM Delete Function and Timing void Delete_Message(unsigned mes_num) { … LCD(“Are you sure?”); // 10 ms get_debounced_switch(&k, 5); // 400 ms min, 5 s max if (k == CANCEL_KEY) { LCD(“Cancelled”); // 10 ms } else if (k == TIMEOUT) { LCD(“Timed Out”); // 10 ms } else { LCD(“Erasing”); // 10 ms Flash_to_Buffer(DIR_PAGE); // 250 us Read_Buffer(dir); // 100 us … // find offsets … // erase dir. entry Write_to_Buffer(dir); // 6 us Buffer_to_Flash(DIR_PAGE); // 20 ms Flash_to_Buffer(data_page); … // overwrite msg: 50 us Buffer_to_Flash(data_page); // 20 ms LCD(“Done”); } Embedded Systems

Cooperative RR Scheduler? LCD(“Are You Sure?”) get_debounced _switch(&k, 5); switch on k LCD(“Cancelled”) LCD(“Timed Out”) LCD(“Erasing”) Flash_to_Buffer(DIR_PAGE) Read_Buffer(dir) … // Find offsets … // erase dir. entry Write_to_Buffer(dir); Buffer_to_Flash(DIR_PAGE) Flash_to_Buffer(data_page) … // Overwrite msg in buffer Buffer_to_Flash(data_page); Since task must Run To Completion… The delete function could take up to five seconds to run, halting all other tasks (but interrupts run) Other software needs to keep running, so break this into pieces. Run one piece at a time. How to split? Each piece ends where processor waits for user (e.g. debounced switch) or other devices (Flash, LCD). How to control execution of pieces? Use a task per piece, use calls to Reschedule_Task and Disable_Task as needed Need 13 different tasks (12 shown here) Use a state machine within one task Embedded Systems

State Machine in One Task switch(cur_state) 1: LCD(“Are You Sure?”); cur_state = 2; 3: if (debounce_done) { if (k == CANCEL_KEY) { LCD(“Cancelled”); cur_state = 99; } else if (k == TIMEOUT) { LCD(“Timed Out”); } else { LCD(“Erasing”); cur_state = 4; } 4: if (LCD_Done) { Flash_to_Buffer( DIR_PAGE); cur_state = 5; } 5: if (Flash_done) { Read_Buffer( dir); cur_state = 6; } 2: if (LCD_Done) { get_debounced _switch(&k, 5); cur_state = 3; } return Embedded Systems

Daydreaming Some functions are causing trouble for us – they use slow devices which make the processor wait LCD: controller chip on LCD is slow DataFlash: it takes time to program Flash EEPROM Switch debouncing: physical characteristics of switch, time-outs Wouldn’t it be great if we could … Make those slow functions yield the processor to other tasks? Not have the processor start running that code again until the device is ready? Maybe even have the processor interrupt less-important tasks? Avoid breaking up one task into many tasks, or a state machine? Open ourselves up to a whole new species of bugs, bugs which are very hard to duplicate and track down? Embedded Systems

Preemptive Scheduling Kernel What we need is a kernel Shares the processor among multiple concurrently running tasks/threads/processes Can forcibly switch the processor from thread A to B and resume B later (preemption) Can resume threads when their data is ready Can simplify inter-thread communication by providing mechanisms The heart of any operating system Terminology: “Kernel Mode” PCs and workstations don’t expose all of the machine to the user’s program Only code in kernel or supervisor mode have full access Some high-end embedded processors have a restricted mode (e.g. ARM, MIPS) Embedded Systems

Operating Systems (for PCs and Workstations) Two perspectives Extended Machine – top-down view (using abstractions) File System: make a magnetic platter, read/write head, spindle motor and head servo look like a hierarchical collection of directories containing files and other directories Virtual Memory: make a disk and 512 MB of RAM look like 4 GB of RAM Resource Manager – bottom-up view Share access to resources Keep them from interfering Common PC/Workstation operating system features Process management – share the processor Process synchronization and communication Memory management File management Protection Time management I/O device access For embedded systems, we care mostly about preemptive thread management – sharing the processor Embedded Systems

What Execution State Information Exists? A program, process or thread in execution which has state information… Current instruction – identified with program counter Call stack – identified with stack pointer Arguments, local variables, return addresses, dynamic links Other CPU state Register values (anything which will be shared and could be affected by the other processes) – general purpose registers, stack pointer, etc. Status flags (zero, carry, interrupts enabled, carry bit, etc.) Other information as well Open files, memory management info, process number, scheduling information Ignore for now Memory 0x0000 global data CPU R0 R1 R2 R3 heap A0 A1 FLG USP PC FB ISP SB stack instructions 0xFFFF Embedded Systems

Processes vs. Threads Process – No information is visible to other processes (nothing is shared) Thread – Shares address space and code with other threads (also called lightweight process) One big side effect: context switching time varies Switching among processes requires swapping large amounts of information Switching among threads requires swapping much less information (PC, stack pointer and other registers, CPU state) and is much faster For this discussion, concepts apply equally to threads and processes Embedded Systems

Maintaining State for Multiple Threads Store this thread-related information in a task/thread control block (TCB) process control block = PCB Shuffling information between CPU and multiple TCBs lets us share processor Consider case of switching from thread A to thread B Assume we have a call stack for each thread Assume we can share global variables among the two threads Standard for threads For M16C architecture, SB register is same for both threads Memory 0x0000 global data CPU R0 R1 R2 R3 heap A0 A1 FLG USP PC FB ISP SB B Stack A Stack instructions Thread A Thread B 0xFFFF Embedded Systems

Step 1. Copy CPU State into TCB A CPU is initially executing task A, so save this information in TCB A Memory 0x0000 global data CPU R0 R1 R2 R3 heap A0 A1 FLG USP PC FB ISP SB TCB A R0 R1 R2 R3 B Stack A0 A1 FLG USP PC FB ISP SB A Stack Thread A instructions Thread B 0xFFFF Embedded Systems

Step 2. Reload Old CPU State from TCB B Reloading a previously saved state configures the CPU to execute task B from where it left off This context switching is performed by the dispatcher code Dispatcher is typically written in assembly language to gain access to registers not visible to C programmer Memory 0x0000 global data CPU R0 R1 R2 R3 heap A0 A1 FLG USP PC FB ISP SB TCB A R0 R1 R2 R3 B Stack A0 A1 FLG USP PC FB ISP SB A Stack instructions Thread A TCB B R0 R1 R2 R3 Thread B A0 A1 FLG USP PC FB ISP SB 0xFFFF Embedded Systems

Thread States New Ready Waiting Running Terminated Now that we can share the CPU, let’s do it! Define five possible states for a thread to be in New – just created, but not running yet Running – instructions are being executed (only one thread can be running at a time!) Waiting/Blocking – thread is waiting for an event to occur Ready – process is not waiting but not running yet (is a candidate for running) Terminated – process will run no more New Ready What the task needs happens This is highest priority ready task This isn’t highest priority ready task Waiting Task needs something to happen Running Terminated Embedded Systems

Thread Queues New New New Ready Ready Ready Ready Ready Waiting Create a queue for each state (except running) Now we can store thread control blocks in the appropriate queues Kernel moves tasks among queues/processor registers as needed Ready Ready Ready Ready Ready What the task needs happens This is highest priority ready task Waiting Waiting Waiting Waiting Waiting Waiting This isn’t highest priority ready task Task needs something to happen Running Terminated Terminated Terminated Embedded Systems

Example Dispatcher Code Use interrupt to trigger a context switch Timer tick Break instruction Recall the interrupt sequence of activities Clear request bit of the active interrupt Save FLG in temporary register in CPU Clear flags in FLG: I (interrupt enable), D (debug flag), and U (stack pointer select) Push temporary register (holding old FLG) onto stack Save PC (20 bits) on stack typedef struct { int sr0, sr1, sr2, sr3; int sa0, sa1; int sfb, ssp; int spc_lm; char sflg_l; char spch_flgh; } TCB_T; new SP PC Low new SP+1 PC Middle new SP+2 FLG Low new SP+3 old SP FLG High PC High Embedded Systems

Example Dispatcher Code to Save Context push.w A0 ; save A0 mov.w cur_TCB, A0 ; load pointer to cur_TCB mov.w R0,sr0[A0] ; save R0 mov.w R1,sr1[A0] ; save R1 mov.w R2,sr2[A0] ; save R2 mov.w R3,sr3[A0] ; save R3 pop.w R0 ; get old value of A0 mov.w R0,sa0[A0] ; save it mov.w A1,sa1[A0] ; save A1 mov.w FB,sfb[A0] ; save frame base register pop.w spc_lm[A0] ; get lower word of old PC from stack pop.b sflg_l[A0] ; get lower byte of flag from stack pop.b spch_flgh[A0] ; get upper nibbles of old PC and flag register mov.w ISP, ssp[A0] ; save stack pointer, which now has no extra information on it ; now scheduler can decide what thread to run next Embedded Systems

Restore Context mov.w new_TCB, A0 ; load pointer to new_TCB mov.w sr0[A0], R0 ; restore R0 mov.w sr1[A0], R1 ; R1 mov.w sr2[A0], R2 ; R2 mov.w sr3[A0], R3 ; R3 mov.w sa1[A0], A1 ; A1 mov.w sfb[A0], FB ; FB mov.w ssp[A0], ISP ; SP push.b spch_flgh[A0] ; high nibbles of FLG and PC push.b sflg_l[A0] ; low byte of FLG push.w spc_lm[A0] ; low and middle bytes of PC mov.w sa0[A0], A0 ; finally restore A0 reit ; return from interrupt. This will reload PC and FLG from the stack Embedded Systems

Thread State Control Use OS scheduler to keep track of threads and their states For each state, OS keeps a queue of TCBs for all processes in that state Moves TCBs between queues as thread state changes OS’s scheduler chooses among Ready threads for execution based on priority Scheduling Rules Only the thread itself can decide it should be waiting (blocked) A waiting thread never gets the CPU. It must be signaled by an ISR or another thread. Only the scheduler moves tasks between ready and running What changes the state of a thread? The OS receives a timer tick which forces it to decide what to run next The thread voluntarily yields control The thread requests information which isn’t ready yet Embedded Systems

Overview of Data Structures for Scheduler TCB B Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Running points to TCB for currently running process. TCB has old information which will be updated on next task switch Running New Null Pointer TCB A Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB C Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB E Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB G Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Ready TCB F Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB D Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB H Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Wait Add Next, Prev pointers in each TCB to make it part of a doubly linked list Keep track of all TCBs Create a pointer for each queue: Ready, Wait, New Create a pointer for the currently running task’s TCB Embedded Systems

Example: Context Switch Thread A is running, and scheduler decides to run thread C instead. For example, thread A is still able to run, but has lower priority than thread C. Start by copying CPU state into TCB A TCB B Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Running CPU R0 R1 R2 R3 New Null Pointer A0 A1 FLG USP PC FB ISP SB TCB A Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB C Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB E Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB G Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Ready TCB F Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB D Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB H Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Wait Embedded Systems

Example: Context Switch Insert TCB A into ready queue by modifying appropriate pointers TCB B Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG CPU Running R0 R1 R2 R3 A0 A1 FLG USP New Null Pointer PC FB ISP SB TCB C Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB E Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB G Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB A Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Ready TCB F Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB D Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB H Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Wait Embedded Systems

Example: Context Switch Remove thread C from the ready queue and mark it as the thread to run next TCB B Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG CPU Running R0 R1 R2 R3 A0 A1 FLG USP New Null Pointer PC FB ISP SB TCB E Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB G Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB A Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB C Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Ready TCB F Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB D Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB H Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Wait Embedded Systems

Example: Context Switch Copy thread C’s state information back into the CPU and resume execution TCB B Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG CPU Running R0 R1 R2 R3 A0 A1 FLG USP New Null Pointer PC FB ISP SB TCB E Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB G Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB A Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB C Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Ready TCB F Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB D Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG TCB H Next Prev R0 R1 R2 R3 A0 A1 FB PC USP ISP SB FLG Wait Embedded Systems

uC/OS-II Real-time kernel Pronounced “microC OS two” Portable, scalable, preemptive RTOS Ported to over 90 processors Pronounced “microC OS two” Written by Jean J. Labrosse of Micrium, http://ucos-ii.com Implementation is different from material just presented for performance and feature reasons CPU state is stored on thread’s own stack, not TCB TCB keeps track of boundaries of stack space TCB also tracks events and messages and time delays Embedded Systems

TCB for uC/OS-II typedef struct os_tcb { OS_STK *OSTCBStkPtr; /* Pointer to current top of stack */ void *OSTCBExtPtr; /* Pointer to user definable data for TCB extension */ OS_STK *OSTCBStkBottom; /* Pointer to bottom of stack – last valid address */ INT32U OSTCBStkSize; /* Size of task stack (in bytes) */ INT16U OSTCBOpt; /* Task options as passed by OSTaskCreateExt() */ INT16U OSTCBId; /* Task ID (0..65535) */ struct os_tcb *OSTCBNext; /* Pointer to next TCB in the TCB list */ struct os_tcb *OSTCBPrev; /* Pointer to previous TCB in list */ OS_EVENT *OSTCBEventPtr; /* Pointer to event control block */ void *OSTCBMsg; /* Message received from OSMboxPost() or OSQPost() */ INT16U OSTCBDly; /* Nbr ticks to delay task or, timeout waiting for event */ INT8U OSTCBStat; /* Task status */ INT8U OSTCBPrio; /* Task priority (0 == highest, 63 == lowest) */ BOOLEAN OSTCBDelReq; /* Indicates whether a task needs to delete itself */ } OS_TCB; Embedded Systems

Data Structures for uC/OS-II OSTCBCur - Pointer to TCB of currently running task OSTCBHighRdy - Pointer to highest priority TCB ready to run OSTCBList - Pointer to doubly linked list of TCBs OSTCBPrioTbl[OS_LOWEST_ PRIO + 1] - Table of pointers to created TCBs, ordered by priority OSReadyTbl - Encoded table of tasks ready to run OSPrioCur – Current task priority OSPrioHighRdy – Priority of highest ready task OSTCBFreeList - List of free OS_TCBs, use for creating new tasks TCB P=8 TCB P=6 TCB P=5 TCB P=1 TCB P=3 3 5 TCB TCB TCB TCB TCB Embedded Systems

Dispatcher for uC/OS-II _OSCtxSw: PUSHM R0,R1,R2,R3,A0,A1,SB,FB MOV.W _OSTCBCur, A0 ;OSTCBCur->OSTCBStkPtr = Stack pointer STC ISP, [A0] ;Call user definable OSTaskSwHook() JSR _OSTaskSwHook ;OSTCBCur = OSTCBHighRdy MOV.W _OSTCBHighRdy, _OSTCBCur ;OSPrioCur = OSPrioHighRdy MOV.W _OSPrioHighRdy, _OSPrioCur ;Stack Pointer = OSTCBHighRdy->OSTCBStkPtr MOV.W _OSTCBHighRdy, A0 LDC [A0], ISP ;Restore all processor registers from the new task's stack POPM R0,R1,R2,R3,A0,A1,SB,FB REIT Embedded Systems

Preemptive vs. Non-Preemptive Non-preemptive kernel/cooperative multitasking Each task must explicitly give up control of CPU E.g. return from task code, call yield function Asynchronous events are handled by ISRs ISR always returns to interrupted task Can use non-reentrant code (covered later) Task level response time can be slower as slowest task must complete Generally don’t need semaphores Preemptive kernel At each scheduling point, the highest priority task ready to run is given CPU control If a higher priority task becomes ready, the currently running task is suspended and moved to the ready queue Maximum response time is less than in non-preemptive system Non-reentrant code should not be used Shared data typically needs semaphores Embedded Systems