Real-Time Programming

Similar documents
Tasks. Task Implementation and management

RTOS Real T i Time me Operating System System Concepts Part 2

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Introduction to Real-Time Operating Systems

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Concurrency: Deadlock and Starvation

CS370 Operating Systems

Module 1. Introduction:

Introduction to Embedded Systems. Lab Logistics


Dealing with Issues for Interprocess Communication

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Preemptive Scheduling

Main Points of the Computer Organization and System Software Module

Interprocess Communication By: Kaushik Vaghani

Exam TI2720-C/TI2725-C Embedded Software

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Process Coordination and Shared Data

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Commercial Real-time Operating Systems An Introduction. Swaminathan Sivasubramanian Dependable Computing & Networking Laboratory


Chapter 6: Process Synchronization

Lesson 6: Process Synchronization

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Virtual Machine Design

Lecture notes Lectures 1 through 5 (up through lecture 5 slide 63) Book Chapters 1-4

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

The components in the middle are core and the components on the outside are optional.

Concurrency: Deadlock and Starvation. Chapter 6

Systemy RT i embedded Wykład 11 Systemy RTOS

Zilog Real-Time Kernel

UNIT:2. Process Management

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

Multitasking / Multithreading system Supports multiple tasks

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

Real-time Support in Operating Systems

Efficiency and memory footprint of Xilkernel for the Microblaze soft processor

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

CSE 153 Design of Operating Systems

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Back to RTOS. CSE466 Autumn 00-1

In examining performance Interested in several things Exact times if computable Bounded times if exact not computable Can be measured

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

CHAPTER 6: PROCESS SYNCHRONIZATION

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

ZiLOG Real-Time Kernel Version 1.2.0

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Chapter 6: Process Synchronization

Real Time Operating System: Inter-Process Communication (IPC)

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Midterm Exam. October 20th, Thursday NSC

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

GLOSSARY. VisualDSP++ Kernel (VDK) User s Guide B-1

CS-537: Midterm Exam (Spring 2001)

REAL TIME OPERATING SYSTEM PROGRAMMING-I: VxWorks

Techno India Batanagar Department of Computer Science & Engineering. Model Questions. Multiple Choice Questions:

Introduction to OS Synchronization MOS 2.3

REAL TIME OPERATING SYSTEMS: A COMPLETE OVERVIEW

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Embedded System Curriculum

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Embedded Operating Systems

CS370 Operating Systems

NuttX Realtime Programming

The Kernel Abstraction

Timers 1 / 46. Jiffies. Potent and Evil Magic

OPERATING SYSTEM CONCEPTS UNDERSTAND!!! IMPLEMENT!!! ANALYZE!!!

Module 6: Process Synchronization

CSE 120. Fall Lecture 8: Scheduling and Deadlock. Keith Marzullo

Process- Concept &Process Scheduling OPERATING SYSTEMS

Please do not handin a.doc file, a.zip file, a.tar file, or anything else

Chapter 6: Process Synchronization

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

CS 153 Design of Operating Systems Winter 2016

Chapter 6: Process Synchronization

REAL-TIME MULTITASKING KERNEL FOR IBM-BASED MICROCOMPUTERS

CSI3131 Final Exam Review

Microkernel/OS and Real-Time Scheduling

Process Management And Synchronization

Interrupts Peter Rounce - room 6.18

SMD149 - Operating Systems

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

CS370 Operating Systems

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

RT3 - FreeRTOS Real Time Programming

Sample Questions. Amir H. Payberah. Amirkabir University of Technology (Tehran Polytechnic)

Transcription:

Real-Time Programming Week 7: Real-Time Operating Systems Instructors Tony Montiel & Ken Arnold rtp@hte.com 4/1/2003 Co Montiel 1 Objectives o Introduction to RTOS o Event Driven Systems o Synchronization and Communication o Task Scheduling o Common RTOS Problems 4/1/2003 Co Montiel 2

Intro to RTOS o Operating System o Share/Allocate Resources: CPU Time Memory I/O o Real Time OS o Event Driven Tasks 4/1/2003 Co Montiel 3 Event Driven Systems o Processing Required When Events Occur o Asynchronous With Respect to Program o System Must Respond to Events o Multiple Events Can Occur Simultaneously o RTOS Attempts to Allocate CPU Time 4/1/2003 Co Montiel 4

Event Driven Systems o The hardware and software together take action as a direct result of the occurrence of an event. o When an event occurs: Other processing is suspended The event is processed Other processing is resumed o Usually these are also Real Time systems 4/1/2003 Co Montiel 5 Polling Events o The simplest, but least efficient method of determining if an event has occurred o Processor waits in a loop for the event o Often used in simple I/O handlers: Wait: Input buffer full? No: jump to Wait Yes: read input data buffer and continue Processor does nothing else... 4/1/2003 Co Montiel 6

Embedded RTOS Concepts o Resource Allocation: CPU Time Round Robin vs. Priority, Preemptive vs. Non-preemptive Memory I/O o Inter-Task Communication o Tasks Scheduling, States o Hiding Hardware Details 4/1/2003 Co Montiel 7 Tasks States, Scheduling o Context Preservation & Restoration Issues: Co-processors, Limited Stack Size... o Task Control Data Structures o Limited Embedded Resources: R/W Volatile Memory Non-volatile Memory Register Banks Timers, Counters, Peripherals 4/1/2003 Co Montiel 8

Differences: Embedded OS o Harvard Architecture is Common o Code in ROM, Data in RAM o Limited Memory Resource Allocation o File System is Limited or Non-Existent o Different Storage Media: Magnetic Media is Rare Solid State Media Common Flash Memory is NOT Like Disk 4/1/2003 Co Montiel 9 RTOS Alternatives o >70% of Embedded Developers Don t Use an RTOS Why? o Disadvantages of Commercial RTOS: Cost, Complexity, Efficiency Issues Customization Req d, Latency, Overhead o Embedded Environment Differences Memory Sizes, Types Cost, Volume, Risks 4/1/2003 Co Montiel 10

RTOS Task State Diagram 4/1/2003 Co Montiel 11 uc/os State Diagram 4/1/2003 Co Montiel 12

Synchronization & Communication o Synchronizing Tasks Semaphores Waiting for Event or Time o Inter-Task Communication Mailbox (Pointer) Circular Queue or Buffer 4/1/2003 Co Montiel 13 What is IPC? o IPC: Inter-Process Communications o Also works for Inter-Task Communications. o A safe method for passing data, messages, and/or events between tasks. o Multiple Processor IPC is a special case. 4/1/2003 Co Montiel 14

Why IPC? o Embedded systems are event driven. o IPC supports this model by allowing tasks to block on the IPC method. o Tasks can be independently developed. Higher reliability by reducing dependencies. 4/1/2003 Co Montiel 15 Why not IPC? o 1 task. o Very simple architecture. o Performance requirements. 4/1/2003 Co Montiel 16

Methods of IPC o Queues o Mailboxes o Signals/Events/Semaphores o Shared Memory 4/1/2003 Co Montiel 17 Queues o Typically implemented as a ring buffer. o Message is copied into and out of the queue buffer. o Disadvantages: Moving the data twice. May consume more memory than other methods. o Advantages: Data is protected. 4/1/2003 Co Montiel 18

Queues o Implementation Dependant Features: Writer may block if queue is full. Reader may block if queue is empty. There can be multiple readers/writers. Messages may be fixed or variable length. FIFO, maybe with priority. 4/1/2003 Co Montiel 19 Queues o Sample APIs: CreateQueue( QControlBlock, Name, StartAddress, Size, <Fixed/Var>, <MsgLen> ); WriteQueue( QControlBlock, Msg, Size, Suspend ); ReadQueue( QControlBlock, Msg, Size, ActualSize, Suspend ); 4/1/2003 Co Montiel 20

Mailboxes o Essentially a fixed size queue with element size equal to size of pointer. o Disadvantages: Data is not protected. Data must remain in scope. Not clear when data has been consumed. o Advantages: Faster than queues. More efficient than queues. If data fits in place of pointer, then this is a queue. 4/1/2003 Co Montiel 21 Mailboxes o Implementation Dependant Features: Subset of queues features. o Sample APIs: CreateMBox( MBoxControl, Name ); WriteMBox( MBoxControl, Msg, Suspend ); ReadMBox( MBoxControl, Msg, Suspend ); 4/1/2003 Co Montiel 22

Signals/Events/Semaphores o Used as an IPC mechanism, semaphores: Allow for synchronization between tasks. Allow ISR and task to communicate. HEY! Data is ready! o Disadvantages: Minimal amount of data (1 bit). Overhead slower than shared variable. o Advantages: Task can block on semaphore. Controlled IPC mechanism (vs. shared variable). 4/1/2003 Co Montiel 23 Signals/Events/Semaphores o Implementation Dependant Features: Event groups: collection of semaphores which can be logically combined to form truth table : Resume task when Ev1 && Ev3 &&!Ev4. 4/1/2003 Co Montiel 24

Semaphores o MUST have Indivisible Test and Set One atomic operation Test & Set o May Also Be Multi-Valued Useful for Multiple Shared Resources -63 to +32767 for uc/os o Mutually Exclusive Access to a Shared Resource: a.k.a. Mutex 4/1/2003 Co Montiel 25 Semaphore vs. Mutex o Mutex: Binary Semaphore Two states: available or busy o Semaphore: Binary (Mutex) or > 2 states (counting): counting semaphore -- increment or decrement example: sharing "n" resources or devices sempaphore = number of available resources 4/1/2003 Co Montiel 26

Example: getsemaphore() bit getsemaphore( uint8 *semaphore ) { bit result = 0; // Assume failure. bit oldea = EA; // Save current state. EA = 0; // Disable interrupts. if ( *semaphore > 0 ) // Any resources avail? { *semaphore = *semaphore - 1; // Grab one. result = 1; // Good to go. } EA = oldea; // Restore interrupts. // NOTE: Could give up processor if we didn t // get the semaphore. return result; } 4/1/2003 Co Montiel 27 Example in Assembly Acquire_Semaphore: ; Acquire semaphore ; Returns with A=1 if success, ; A=0 if failure to acquire semaphore. mov a,#1 ; Assume we got it. jbc sema4_bit, got_sema ; Atomic Test & Clr. clr a ; Failed to acquire. got_sema: ret Release_Sempahore: ; Release semaphore. setb sema4_bit ; Restore flag. ret 4/1/2003 Co Montiel 28

Shared Memory o Can always use shared memory for all the previously mentioned IPC mechanisms. o Disadvantages: Not thread-safe, unless hardware lock available. No controls to prevent misunderstandings. o Advantages: Fast. No RTOS overhead. Ring Buffer can be thread safe with one producer and one consumer. 4/1/2003 Co Montiel 29 Task Scheduling o Scheduler Allocates CPU Time o Determines Which Task to Run Next Context Switch uc/os uses: Priority Based, number 0-63 High Priority (0) Supercedes Low Priority (63) High Priority Tasks can Interrupt Low Ones o Overhead: Time Wasted, Not Spent on Task 4/1/2003 Co Montiel 30

Schedulers o There are basically two types of scheduling schemes in use today: Priority Round-robin o This is not to be confused with preemption (a.k.a time-slicing) and non-preemption (or cooperative) multi-tasking. o Schedulers can implement multiple combinations of the above methods. 4/1/2003 Co Montiel 31 uc/os Priorities o 64 Priorities o One Unique priority for each task o Lower number = higher priority o Example priorities used by sample firmware for a TCP/IP stack and server applications: MAIN_PRIO 50 HTTP 45 PPP 44 TCP 40 IP 39 Ethernet Send 38 4/1/2003 Co Montiel 32

Task Priorities and Blocking o Task priorities must be chosen carefully o If you create a high priority task that never blocks, lower priority tasks will never run o For example, task A has a priority of 50 and constantly polls an peripheral status register. Task B is assigned a priority of 51 and is used for a serial command interface. In this case, Task B will never run. 4/1/2003 Co Montiel 33 Kernels o The Kernel is responsible for task management and communication between tasks. o Kernels can be Preemptive or Nonpreemptive o The Scheduler is a part of the Kernel. 4/1/2003 Co Montiel 34

Round-Robin Scheduling o Basically, tasks run in a pre-defined order. o For example: Say you have 3 tasks (T1, T2, T3). Their execution order is T1, T2, then T3. Whenever T1 releases the CPU (a different story), if T2 is ready to run it will be allowed to run. After T2 is done, T3 will run (assuming it is ready). After T3 is done, T1 will get to run again. 4/1/2003 Co Montiel 35 Priority Scheduling o Each task is given a priority. o Higher priority tasks run before lower priority tasks. o For example: Say T1 has highest priority and T3 lowest. T2 is running. T1 and T3 are ready at the same time. T1 will be allowed to run. 4/1/2003 Co Montiel 36

Non-Preemptive Kernel o Each task must specifically do something to give up control of the CPU o Also called cooperative-multitasking o ISR s interrupt tasks, but do not immediately determine which task will run upon completion o Simpler to implement than pre-emptive. o Critical section easier to design away. 4/1/2003 Co Montiel 37 Non-Preemptive Example From uc/os-ii page 43 4/1/2003 Co Montiel 38

Preemptive Kernel o Used for Real-Time applications o Highest priority task always runs o A task switch will occur when: A task makes a higher priority task ready to run. An ISR completes o More complicated to implement o Critical section issue more pronounced. 4/1/2003 Co Montiel 39 Preemptive Example From uc/os-ii page 44 4/1/2003 Co Montiel 40

Re-entrant Code o Code that can be interrupted, and called again one or more times, before completing execution. Must use only local resources (i.e.variables) Cannot modify global resources Compiler libraries are often non re-entrant Most OS functions are non re-entrant Code that uses dedicated hardware is not 4/1/2003 Co Montiel 41 Common RTOS Problems o Task Stack Size o Stack Overflow o Scheduling Issues o Preemptive Task Priority o Getting Stuck... 4/1/2003 Co Montiel 42

Deadlock o Circular dependency between tasks and resources o Task1 takes MutexA, waits on MutexB o Task2 takes MutexB, waits on MutexA o DEADLOCK: Waiting for an event which will never occur. o Only way to end is reboot. 4/1/2003 Co Montiel 43 Deadlock Example Task A Resource1 Task B Resource2 = Has Acquired = Is Requesting 4/1/2003 Co Montiel 44

Determinism o A system is guaranteed to behave in a predictable way. o Priority inversion can make the system unpredictable: A low priority tasks behaves like a high priority task. 4/1/2003 Co Montiel 45 Priority Inversion o Higher priority task blocks, waiting for a lower priority task. o OK thus the reason for mutexes. o 3 rd task comes along and can run. o This affects determinism. o Very bad for hard real-time systems. 4/1/2003 Co Montiel 46

Priority Inversion Example High Priority Med Priority Low Priority t0 t1 t2 t3 t4 t5 t6 t7 = task blocked = priority inversion 4/1/2003 Co Montiel 47 Priority Inheritance task + PI High Priority Med Priority Low Priority t0 t1 t2 t3 t4 t5 t6 = task blocked = Priority inheritance active 4/1/2003 Co Montiel 48

Additional Reading o http://www.ganssle.com/tem/tem14.pdf This is the discussion of the Mars Pathfinder mission. Very interesting reading. 4/1/2003 Co Montiel 49 A Critical View o Commercial RTOS: any co-operative? o May not be due to technical reasons: Co-operative schedulers easier to make. Co-operative schedulers are portable. Pre-emptive RTOS much larger and more complex (uc/os: ~3000 vs. Pont: ~300) This may make them unsuitable for inhouse development. 4/1/2003 Co Montiel 50

RTOS Strategic Issues o License Fees, Development Costs o Learning Curve is Non-Trivial o Long Term RTOS Vendor Stability o Lack of Visibility and Control o Availability of Source Code o Upgrades, Support, Obsolescence o Development of Internal RTOS 4/1/2003 Co Montiel 51 Summary o OS Concepts o Resource Sharing / Allocation o Use only the elements needed. o For simplest situations, shared memory may be best approach. o Variety of IPC mechanisms exist: Queues, Mailboxes, Signals/Events/Semaphores 4/1/2003 Co Montiel 52