Embedded System Lab. II Embedded System Lab. II 2 RTOS Hard Real-Time vs Soft Real-Time RTOS Real-Time, Real-Time RTOS General purpose system OS H/W RTOS H/W task Hard Real-Time Real-Time System, Hard Real-Time System Ex), Soft Real-Time Soft Real-Time System Ex) cellular phone, router Embedded System Lab. II 3 Embedded System Lab. II 4
RTOS - Multitasking RTOS - Task Multitasking Embedded system task (), Multitasking Ex) ADSL Router PPP(point-to-point) Task IP(Internet Protocol) Task UDP(User Datagram Protocol) Task TCP(Transmission Control Protocol) Task RIP(Routing Information Protocol) Task ATM(Asynchronous Transfer Mode) Task Task priority Task priority task priority task Preemptive kernel Task stack Task ( ) Task stack, task, argument, return stack stack Embedded System Lab. II 5 Embedded System Lab. II 6 RTOS Task Task states Diagram Task status DORMANT Memory Task RTOS READY RUNNING Task READY task priority WATING READY terminate WATING get something wait something(events) start context switch interrupt DORMANT READY RUNNING ISR terminate context switch return Embedded System Lab. II 7 Embedded System Lab. II 8
Task state High priority task A serial Low priority task B LED ON/OFF 2 task Task A task B priority RUNNING task B READY. Task A serial WATING Scheduler (Context Switch) task B RUNNING. task B LED ON/OFF. Serial ISR. ISR Scheduler. Scheduler (Context Switch) ISR WATING task A RUNNING task B READY. Multiple Task Diagram 1 2 3 stack stack stack TCB TCB TCB Priority Status Priority Status Priority Status Stack Base Address register Stack Base Address register Stack Base Address register register.. MEMORY Embedded System Lab. II 9 Embedded System Lab. II 10 Scheduler (Dispatcher) Context Switch (Task Switch) Scheduler READY task task priority task priority-based scheduling High priority Ready list A 100 B 80 C 50 D 30 E 5 Scheduler A win!! Task RUNNING. Task (). Task. Context Switch Scheduler RUNNING task RUNNING task Context task Context TCB task Context Switch. Task Context. Context Task. Priority based Scheduler Embedded System Lab. II 11 Embedded System Lab. II 12
Context switch Context switch Diagram task1 Scheduler READY task2 Context switch (, ). task1 Context task1 TCB task2 TCB Context. task2 Scheduler task1 RUNNING Context Switch (, ) task2 (Context) task2 TCB task1 TCB Context 1 2 3 stack stack stack TCB TCB TCB Priority Status Priority Status Priority Status Stack Base Address register Stack Base Address register Stack Base Address register.. context push registers Image into the each TCB MEMORY context switch pop registers image from the TCB into registers Embedded System Lab. II 13 register Embedded System Lab. II 14 Non-preemptive Kernel Non-preemptive Kernel task kernel task task cooperative multitasking Real-time system priority task priority task Ex) Windows 3.1 Low-Priority Task A ISR ISR makes the high-priority task ready Time High-Priority Task B Low-Priority task relinquishes the Embedded System Lab. II 15 Embedded System Lab. II 16
Non-Preemptive Kernel Preemptive Kernel Low priority taska interrupt ISR Scheduler task priority task READY ISR ISR Low priority taska taska system call kernel taskb ( serial ) task kernel task task ( task priority ) priority task Deterministic RTOS Ex) Windows 95/98/NT, UNIX Embedded System Lab. II 17 Embedded System Lab. II 18 Preemptive Kernel Preemptive Kernel Low-Priority Task A Time ISR High-Priority task relinquishes the ISR makes the high-priority task ready High-Priority Task B Low priority taska ISR Scheduler task priority task READY ISR ISR taska high priority taskb taskb ( serial ) taskb kernel system call kernel taska taska Embedded System Lab. II 19 Embedded System Lab. II 20
Critical Section (Region) Mutual Exclusion task, Context Switch task Solution Mutual Exclusion Progress Bounded Waiting Semaphore task task Mutual Exclusion Disable interrupts; Access(read/write) the shared resource; Enable interrupts; ( ) enable Embedded System Lab. II 21 Embedded System Lab. II 22 Mutual Exclusion (2) Semaphore Scheduling Semaphore Disable scheduling; Access(read/write) the shared resource; Enable scheduling; 1960 Edgser Dijkstra RTOS key Scheduling ISR Mutual Exclusion task, priority task deterministic Binary Semaphore 1 Acquire Semaphore Semaphore SEMAPHORE Shared Resource Shared Resource access time Acquire Semaphore n Binary Semaphore Embedded System Lab. II 23 Embedded System Lab. II 24
Semaphore (2) Task Synchronization Counting Semaphore 1 n Semaphore priority based FIFO based Acquire Semaphore Acquire Semaphore 5 SEMAPHOREs 5 4 3 2 1 Shared Resources int N = 0; void taska(void) /* task A */ int i; for (i = 1; i <= 2000; i++) N++; void taskb(void) /* taskb */ int i; for (i = 1; i <= 2000; i++) printf( N is %d\n, N); int N = 0; /* semaphore X count = 0 */ /* semaphore Y count = 1 */ void taska(void) /* taska */ int i; for (i = 1; i <= 2000; i++) Take semaphorex; /* attempt to get semaphore X */ N++; Give semaphorey; /* release semaphore Y */ void taskb(void) /* taskb */ int i; for (i = 1; i <= 2000; i++) Take semaphorey; /* attempt to get semaphore Y*/ printf( N is %d\n, N); Give semaphorex; /* release semaphore X */ Embedded System Lab. II 25 Embedded System Lab. II 26 Reentrancy Priority Inversion & Priority Inheritance Non-reentrant function example int Temp; void swap(int *x, int *y) Temp = *x; *x = *y; *y = Temp; Priority Inversion priority task priority task HIGH Task 1 Task 1 Temp = 1 Low priority task A for( ; ; ) for( ; ) x = 1; x = 1; y = 2; y = 2; swap(&x, &y); swap(&x, &y); Temp = *x; Temp = *x; *x = *y; *x = *y; *y = Temp; *y = Temp; sleep(1); sleep(1); ISR OS (context switch) OS (context switch) Temp = 3 Original Temp value(=1) is altered to 3!!! Temp = 3 High priority task B for( ; ; ) for( ; ) x = 3; x = 3; y = 4; y = 4; swap(&x, &y); swap(&x, &y); Temp = *x; Temp = *x; *x = *y; *x = *y; *y = Temp; *y = Temp; sleep(1); sleep(1); priority Task 3 LOW : take semaphore : give semaphore : give semaphore Task 2 Task 3 Task 3 time : context switch(preemption) : priority inheritance/release : waited(blocked) Priority Inversion Embedded System Lab. II 27 Non-reentrant function Embedded System Lab. II 28
Priority Inversion & Priority Inheritance (2) Task Communication (Inter) Priority Inversion priority task WAITING, task task priority task priority HIGH LOW priority Task 3 : take semaphore : give semaphore : give semaphore Task 1 Task 3 time Task 1 : context switch(preemption) : priority inheritance/release : waited(blocked) Task 2 Priority Inheritance global variable Linked list, Circular queue.. Mutual Exclusion message passing Message Mailbox Message Queue ISR ISR send send Mailbox receive Message Message Mailbox Queue receive Message Message Queue Embedded System Lab. II 29 Embedded System Lab. II 30 Interrupts Interrupts (2) mechanism asynchronous events Non-preemptive kernel ISR ISR task Preemptive kernel ISR, Scheduling Interrupt Latency : Maximum amount of time interrupts are disabled + Time to start executing the first instruction in the ISR Interrupt Response : Interrupt Latency + Time to save the s context + Execution time of the kernel ISR entry function(preemptive kernel only) Interrupt Recovery : Time to determine if a higher priority task is ready(preemptive kernel only) + Time to restore the s context of the highest priority task + Time to execute the return from interrupt instruction Embedded System Lab. II 31 Embedded System Lab. II 32
Interrupts (3) non-preemptive kernel Interrupts (4) preemptive kernel Interrupt Request time Interrupt Request A s Context saved time Kernel s ISR Exit function Interrupt Recovery A s Context restored Kernel s ISR Entry function User ISR Code s Context restored Interrupt Latency Interrupt Response User ISR Code Interrupt Recovery s Context restored Interrupt latency, response, and recovery (non-preemptive kernel) Interrupt Latency Interrupt Response Kernel s ISR Exit function Interrupt Response s Context restored B Interrupt latency, response, and recovery (preemptive kernel) Embedded System Lab. II 33 Embedded System Lab. II 34 RTOS RTOS -Task RTOS kernel interface VxWorks psos Nucleus VRTX uc/os II Task ID Task ID Task Unique Key VxWorks, psos, Nucleus TCB pointer Task Id OS TCB, pointer VRTX, uc/os II Virtual Task ID, TCB Table 0-255(VRTX), 0-63(uC/OS II) Task ID Task Task VxWorks, psos, Nucleus, VRTX Configuration Task uc/os II 64 Task, 4 4 Task OS Task 56 Embedded System Lab. II 35 Embedded System Lab. II 36
RTOS -Task RTOS -Task Task VxWorks, psos, Nucleus 10, 4, 8 Task Task unique ID VRTX, uc/os II Task Priority() VxWorks, VRTX, Nucleus 0 255 priority ( 0 ) psos 0 255 priority ( 0 ) 0 240-255 OS uc/os II Priority Task ID (0-63) Task Nucleus, uc/os II Scheduling psos Scheduling VxWorks, VRTX Scheduling Scheduling 2 Argument Passing() Task data argument VxWorks 10 parameter VRTX char *paddr unsigned long psize parameter block Nucleus argc argv psos 4 integer argument uc/os II 3 parameter Embedded System Lab. II 37 Embedded System Lab. II 38 RTOS Semaphore/Mutex RTOS Semaphore/Mutex Mutex Nucleus uc/os II 2.04 version psos psos 3 VxWorks Binary Semaphore Priority or FIFO Semaphore pending Priority, FIFO uc/os II Priority VxWorks, psos, Nucleus, VRTX Priority FIFO Create interface Name psos, Nucleus VxWorks, VRTX, uc/os II Name Semaphore ID Timout No Wait Semaphore Timout Timout,, No Timeout 3 No Timeout Semaphore pend return VxWorks, Nucleus NOWAIT pending interface timeout (FOREVER,NOWAIT,Timeout) psos WAIT, NOWAIT wait parameter, WAIT 0 Timeout interface VRTX, uc/os II NOWAIT accept() interface Embedded System Lab. II 39 Embedded System Lab. II 40
RTOS Semaphore/Mutex RTOS Queue Semaphore VxWorks Take/Give psos P/V VRTX, uc/os II Pend/Post Nucleus Obtain/Release Mutex VxWorks Take/Give VRTX Lock/Unlock Variable-length Fixed-length Variable-length : queue message Fixed-length : queue message VxWorks Variable-length psos, Nucleus Variable-length Fixed-length VRTX, uc/os II Fixed-length Priority or FIFO Queue pending uc/os II Priority VxWorks, psos, Nucleus, VRTX Priority FIFO Create interface Embedded System Lab. II 41 Embedded System Lab. II 42 RTOS Queue RTOS Queue Name psos, Nucleus VxWorks, VRTX, uc/os II Name Queue ID Send/Post Timout Queue Full Send/Post error return, send/post VxWorks, Nucleus - psos, VRTX, uc/os II error return Timout No Wait Queue Timout Timout,, No Timeout 3 No Timeout Queue message pend return VxWorks, Nucleus NOWAIT pending interface timeout (FOREVER,NOWAIT,Timeout) psos WAIT, NOWAIT wait parameter, WAIT 0 Timeout interface VRTX, uc/os II NOWAIT accept() interface Broadcast Queue pend Task psos, VRTX, Nucleus VxWorks, uc/os II Embedded System Lab. II 43 Embedded System Lab. II 44
RTOS Queue RTOS (psos) Queue VxWorks, psos, Nucleus send/receive VRTX, uc/os II post/pend Integrated Systems WindRiver ( psos+ ) RTOS (prism+) Kernel Software component Kernel, application, royalty application Embedded System Lab. II 45 Embedded System Lab. II 46 RTOS (VxWorks) Multi Thread OS (1) WindRiver Chip Device Driver psos 200 -> RTOS (Tornado:) Kernel, application, royalty OSE Enea OSE Systems, RTOS VRTX Mentor Graphics RTOS. Nucleus Plus Accelerated Technology, RTOS RTOS Full Source Code, Royalty PDA 50, 1, 2 SuperTask US Software, RTOS. Nucleus Source Code Open, No Royalty Embedded System Lab. II 47 Embedded System Lab. II 48
Multi Thread OS (2) Multi Process OS (3) MicroC/OS (uc/os) RTOS Jean J. Labrosse RTOS, Source Code, Royalty Upgrade Upgrade uc/os-ii, QNX QNX Software Systems, UNIX, Real-Time Platform Package OS-9 Microware, RTOS LynxOS LinuxWorks, Embedded Linux RTOS UNIX OS, Real-Time Application RTLinux Finite State Machine Labs, Embedded Linux Embedded System Lab. II 49 Embedded System Lab. II 50