IN2305-II Embedded Programming Lecture 6: RTOS Introduction.

Slides:



Advertisements
Similar presentations
Chapter 7 - Resource Access Protocols (Critical Sections) Protocols: No Preemptions During Critical Sections Once a job enters a critical section, it cannot.
Advertisements

Real-Time Library: RTX
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
IN2305-II Embedded Programming Lecture 7: More OS Services.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 Concurrent and Distributed Systems Introduction 8 lectures on concurrency control in centralised systems - interaction of components in main memory -
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
IN2305-II Embedded Programming Lecture 5: Survey of SW Architectures.
Monitors: An Operating System Structuring Concept
Outline Introduction to MQX Initializing and starting MQX
ELN5622 Embedded Systems Class 8 Spring, 2003 Aaron Itskovich
Introduction to Embedded Systems
1 Previous lecture review n Out of basic scheduling techniques none is a clear winner: u FCFS - simple but unfair u RR - more overhead than FCFS may not.
SYNCHRONIZATION Module-4. scheduling Scheduling is an operating system mechanism that arbitrate CPU resources between running tasks. Different scheduling.
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.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Real Time Operating Systems
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Overview Task State Diagram Task Priority Idle Hook AND Co-Routines
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Embedded Systems OS. Reference Materials The Concise Handbook of Real- Time Systems TimeSys Corporation.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
ECGR-6185 µC/OS II Nayana Rao University of North Carolina at Charlotte.
Real Time Operating Systems Michael Thomas Date: Rev. 1.00Date.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
ECE291 Computer Engineering II Lecture 15 Dr. Zbigniew Kalbarczyk University of Illinois at Urbana- Champaign.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS) 6.0 Introduction A more complex software architecture is needed to handle multiple tasks, coordination,
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
4.1 Introduction to Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
CHAPTER 7 CONCURRENT SOFTWARE Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
CE Operating Systems Lecture 8 Process Scheduling continued and an introduction to process synchronisation.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
2-1 Chapter 2 Real-Time Systems Concepts. 2-2 Foreground/Background Systems BackgroundForeground ISR Time Code execution application consists of an infinite.
Outlines  Introduction  Kernel Structure  Porting.
Topics Covered What is Real Time Operating System (RTOS)
CS703 – Advanced Operating Systems
Background on the need for Synchronization
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS/RTK)
An Embedded Software Primer
Process Synchronization and Communication
Chapter 6: CPU Scheduling
Semaphores -Gajendra Singh.
An Embedded Software Primer
COMS Prelim 1 Review Session
Fast Communication and User Level Parallelism
Computer Science & Engineering Electrical Engineering
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CS703 – Advanced Operating Systems
Sarah Diesburg Operating Systems CS 3430
Presentation transcript:

IN2305-II Embedded Programming Lecture 6: RTOS Introduction

in2305-II: L62 RTOS: Primary Motivation Task switching with priority preemption Additional services (semaphores, timers, queues,..) Better code! Having interrupt facilities, one doesn’t always need to throw a full-fledged RTOS at a problem However, in vast majority of the cases the code becomes (1) cleaner, (2) much more readable by another programmer, (3) less buggy, (4) more efficient The price: negligible run-time overhead and small footprint

in2305-II: L63 Task Switching Task switching = switching from current context (PC, stack, registers) to another context Context = thread identity, hence aka multithreading Need two constructs: initialize a context switch to a context Often used: setjmp / longjmp We use X32 init_stack / context_switch

in2305-II: L64 Simple Example (X32) void**thread_main; **thread_a; void*stack_a[1024]; intmain(void) { thread_a = init_stack(stack_a, task_a); printf(“now in thread_main\n”); context_switch(thread_a,&thread_main); printf(“back in main_thread\n”); } voidtask_a(void) { print(“now in thread_a\n“); context_switch(thread_main,&thread_a); }

in2305-II: L65 Time Slicing Example (1) void**thread_main; **thread_a; void*stack_a[1024]; intthread_id; voidisr_timer(void) { if (thread_id == 0) { thread_id = 1; context_switch(thread_a,&thread_main); } else { thread_id = 0; context_switch(thread_main,&thread_a); }

in2305-II: L66 Time Slicing Example (2) intmain(void) { thread_a = init_stack(stack_a, task_a); thread_id = 0; // now in main !! set timer to interrupt every 5 ms while (TRUE) print(now in thread_main\n); } voidtask_a(void) { while (TRUE) print(“now in thread_a\n“); }

in2305-II: L67 X32: Demo Demo.. (x32_projects.tgz: slicing.c)

in2305-II: L68 Task Switching in RTOS In an RTOS task switching is performed by the RTOS RTOS scheduler decides which task to run (continue) Scheduling based on the state of all tasks: blocked running ready event occurred waiting for event highest prio ready preempted by higher prio

in2305-II: L69 UTMS Example voidvButtonTask(void) // high priority { while (TRUE) { !! block until button push event !! quickly respond to user } voidvLevelsTask(void) // low priority { while (TRUE) { !! read float levels in tank !! do a looooong calculation }

in2305-II: L610 RTOS interference The block construct in vButtonTask is a call to the RTOS to deschedule the task until event has occurred This implies that another thread must eventually post this event, i.e., notifies the RTOS that the event has occurred, again by calling the RTOS Once the RTOS is notified it will unblock vButtonTask (i.e., move it’s state from blocked to ready) Thus, two RTOS functions are needed: OS_Pend(event)// block, go execute some other task OS_Post(event)// unblock the blocked task (eventually)

in2305-II: L611 RTOS Implementation Example (1) voidOS_Pend(int event) { !! old_id = current task_id !! task_state[old_id] = BLOCKED !! event_task[event] = old_id; !! figure out which task to run -> new_id !! context_switch(task[new_id],&(task[old_id])) !! return // to task[old_id], once !! rescheduled to run }

in2305-II: L612 RTOS Implementation Example (2) voidOS_Post(int event) { !! old_id = event_task[event]; !! task_state[old_id] = READY !! figure out which task to run -> new_id !! (old_id may have higher priority) !! if not other task then return !! else context switch: !! current_id = current task_id !! context_switch(task[new_id], &(task[current_id])) !! return // to task[current_id] once !! rescheduled to run }

in2305-II: L613 UTMS Example Revisited voidisr_buttons(void) // ISR: be quick! only post { if (peripherals[BUTTONS] & 0x01) // button 0 OS_Post(event); // signal event } voidvButtonTask(void) // task: do the slow printing { while (TRUE) { OS_Pend(event); // wait for event printf(“current float levels: \n”); !! list them }

in2305-II: L614 UTMS Context Switching vLevelsTask button IRQ button ISR vButtonTask OS_PendOS_Post RTOS OS_Pend highest priority task starts first context switch

in2305-II: L615 Advantages RTOS Efficiency: no polling for button state Note that this could not be done by vButtonTask because of priorities Clean code: alternative would be polling by vLevelsTask which would lead to awful (RR) code Note: an interrupt solution would be efficient, but the slow processing (e.g., printing) within vButtonTask should NOT be done by an ISR! (destroying interrupt latency, see earlier lecture)

in2305-II: L616 Shared Data Problem Each task (thread) has its own context: PC, stack, regs (SP, other..) The rest is shared between all tasks Shared data allows inter-task communication Tasks can be preempted by another task (just like preemption by an ISR): shared data problem!

in2305-II: L617 UTMS Example voidvButtonTask(void) // high priority { while (TRUE) { !! block until button push event !! print tankdata[i].lTimeUpdated !! print tankdata[i].lTankLevel } voidvLevelsTask(void) // low priority { while (TRUE) { !! read + calculate tankdata[i].lTimeUpdated = !! time tankdata[i].lTankLevel = !! calc result } OS_Post

in2305-II: L618 Reentrancy Each task (thread) has its own context: PC, stack, regs (SP, other..) The context also includes local C variables as they are stored on the stack So code (functions) that use local vars can be called by multiple threads without risk of messing up other threads’ copies of the local vars (cf. function recursion) If not local, a shared data problem may occur, causing the function to be not reentrant

in2305-II: L619 Example Reentrancy Problem voidtask1(void) voidtask2(void){..; vAdd(9);..;..; vAdd(11);..;} voidvAdd(int cInc) // NOT reentrant! { cErrors = cErrors + cInc; } task1 task2 cErrors + cInc cErrors = atomic context switch NOT atomic

in2305-II: L620 Solutions Just use local vars: no shared data problem, reentrancy guaranteed, no need for atomicity But local vars don’t get you very far, at some point need to share data: make critical sections atomic In the case of interrupts we used {EN|DIS}ABLE_INT Now we need to stop RTOS to preempt: OS_XXX() Classic OS service: semaphores P() / V(), pend() / post(), take() / release(),.. cf. OS_Pend() / OS_Post() we saw earlier

in2305-II: L621 Example Reentrancy Solution voidtask1(void) voidtask2(void){..; vAdd(9);..;..; vAdd(11);..;} voidvAdd(int cInc) // Reentrant! { OS_Pend(s); cErrors = cErrors + cInc; OS_Post(s); } task1 task2 cErrors + cInc cErrors = pend post new context switch post atomic

in2305-II: L622 Semaphores: Versatile & Efficient Useful for protecting critical sections (e.g., in vAdd() ) Also useful for signaling between code (cf. UTMS!) s = Init(0); Pend(s); Post(s); s = Init(1); Pend(s); Post(s); c r i t i c a l s e c t i o n MECS ME = Mutual Exclusion CS = Condition Synchronization

in2305-II: L623 Semaphores: Pitfalls … Classical: Deadlock Less trivial: Priority Inversion pend(A) pend(B) pend(A) s = Init(0); deadlock task A task B task C pend post if B weren’t there

in2305-II: L624 Protecting Shared Data Disabling/Enabling Interrupts: fast (single instruction) only method when data shared between task and ISR [1] drastic: affects response time + affects OS context switching! Taking/Releasing Semaphores: less fast (OS function) doesn’t work for ISRs [1] more transparent to IRQ and task response times [1] Taking semaphores in IRS is NOT allowed (discussed later)

in2305-II: L625 Our RTOS: uC/OS Comes with book (CDROM) Extremely simple CDROM includes DOS port, and demo exes Non-preemptive threading model (no time slicing, so no task preempts another task unless that task blocks (semaphore, delay,..) All tasks must have different priority Needs 1 (timer) interrupt to implement time delays X32 port Context switching with X32 context_switch() Uses TIMER1 IRQ for time delays See in2305 Lab Manual and X32 site for documentation

in2305-II: L626 X32: Demo Demo.. (CDROM DOS demos) (x32_projects.tgz: ucos_hello.c, ucos_reentrancy.c)