Embedded Systems Introduction to Real-Time Operating Systems C.-Z. Yang Sept.-Dec. 2001.

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels
Advertisements

EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Week 12 Real time kernel Ref: Extracts from:
Real-Time Kernels and Operating Systems Basic Issue - Purchase commercial “off-the- shelf” system or custom build one Basic Functions –Task scheduling.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Computer Systems/Operating Systems - Class 8
CSE Fall Introduction - 1 What is an Embedded Systems  Its not a desktop system  Fixed or semi-fixed functionality (not user programmable)
OS Spring’03 Introduction Operating Systems Spring 2003.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Embedded Systems Software Architectures C.-Z. Yang Sept.-Dec
Embedded Systems An Overview to Embedded Software C.-Z. Yang Sept.-Dec
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
Embedded Systems Basic Design Using a Real- Time Operating System C.-Z. Yang Sept.-Dec
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Embedded Systems Interrupts C.-Z. Yang Sept.-Dec
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
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
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSE 451: Operating Systems Section 5 Midterm review.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Processor Architecture
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.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
Department of Computer Science and Software Engineering
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS) 6.0 Introduction A more complex software architecture is needed to handle multiple tasks, coordination,
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 3.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
1.  System Characteristics  Features of Real-Time Systems  Implementing Real-Time Operating Systems  Real-Time CPU Scheduling  An Example: VxWorks5.x.
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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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.
Tutorial 2: Homework 1 and Project 1
Introduction to Operating Systems Concepts
Computer System Structures
Process Management Process Concept Why only the global variables?
CS 6560: Operating Systems Design
Topics Covered What is Real Time Operating System (RTOS)
Background on the need for Synchronization
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS/RTK)
Mechanism: Limited Direct Execution
Introduction to the Linux Kernel
An Embedded Software Primer
Threads, SMP, and Microkernels
Lecture 4- Threads, SMP, and Microkernels
Threads and Concurrency
CPU scheduling decisions may take place when a process:
Threads Chapter 4.
Concurrency, Processes and Threads
CS333 Intro to Operating Systems
Concurrency, Processes and Threads
Embedded System Development Lecture 12 4/4/2007
Presentation transcript:

Embedded Systems Introduction to Real-Time Operating Systems C.-Z. Yang Sept.-Dec. 2001

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems2 OS Architectures A traditional UNIX system architecture

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems3 OS Architectures A monolithic OS kernel

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems4 OS Architectures A real-time kernel implementation

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems5 OS Architectures A micro-kernel RTOS

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems6 Case Study: Real Time or Real Linux? A Realistic Alternative Paul N. Leroux Technology Analyst QNX Software Systems Ltd.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems7 Using Linux as a Real-time OS For the embedded systems designer, Linux poses a dilemma. –Linux has a rich legacy of source code, and industry- standard POSIX APIs. –The standard Linux kernel can't deliver the "hard" real- time capabilities. Several innate problems –Process scheduling –Preemptible kernel

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems8 Process Scheduling Preemptive priority –Rather than use a preemptive priority-based scheduler, as an RTOS would, Linux implements a "fairness" policy so that every process gets a reasonable opportunity to execute. –In fact, the OS will sometimes interrupt a high-priority process to give a lower-priority process a share of CPU time.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems9 Preemption The standard Linux kernel isn't preemptible. –A high-priority process can never preempt a kernel call, but must instead wait for the entire call to complete - even if the call was invoked by the lowest-priority process in the system.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems10 Real Time Implemented Outside of Linux Running Linux as a task on top of a realtime kernel –Any tasks that require deterministic scheduling also run in this preemptible kernel, but at a higher priority than Linux.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems11 The Dual-kernel model System Operations –The realtime kernel always gets first dibs on hardware interrupts. –Otherwise, the kernel will pass the interrupt to Linux for processing. Advantages –All this work is invisible to applications running in the Linux environment - except, of course, for the CPU cycles lost to the realtime kernel and its tasks. –System development is very flexible.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems12 Shortcomings of The Dual-kernel model Duplicated coding efforts –Tasks running in the realtime kernel can't make full use of existing Linux system services. Fragile execution environment –Tasks running in the realtime kernel don't benefit from the robust MMU-protected environment. Limited portability –With the dual-kernel approach, realtime tasks aren't Linux processes at all, but threads and signal handlers written to a small subset of the POSIX API or, in some cases, a non- standard API.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems13 Shortcomings of The Dual-kernel model No determinism for existing Linux applications and drivers –Because Linux processes don't run in the realtime kernel, they don't gain any deterministic behavior. Limited design options –The APIs supported by the realtime kernel provide only a subset of the services provided by standard POSIX and Linux APIs.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems14 "Pure" Realtime Linux? It allows developers to use a standard Linux kernel and programming model. It helps address the low-latency requirements of reactive, event-driven systems. However, –such low-latency patches don't address the complexity of most realtime environments. –For example, modifications of the Linux driver and virtual file system (VFS) are needed.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems15 Difficulties in a Pure Realtime Linux In any case, it's unclear which realtime modifications, will eventually be integrated into the standard Linux kernel. After all, most Linux-based systems rely on the kernel's current approach, which sacrifices predictability to achieve higher overall throughput.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems16 The Best of Both Worlds The RTOS should be able to –allow Linux developers to keep their existing tools, source code, and programming model –maintain the key benefits, such as easier troubleshooting and OS customization, of Linux's open source model –address the shortcomings of realtime Linux extensions

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems17 Several Problems How well can an RTOS support the Linux programming model? –The answer lies, to a great degree, in the POSIX APIs adopted by Linux. –As a result, an RTOS can support the same POSIX APIs as Linux without adopting Linux's non-deterministic kernel. A need for a new lingua franca among embedded Linux developers.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems18 QNX model A realtime, microkernel architecture

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems19 Embedded Linux: a new definition As a set of APIs, along with a test suite to measure conformance. As a result, –the RTOS inherently supports embedded Linux applications, while simultaneously providing all the benefits of a true RTOS designed from the ground up for embedded systems.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems20 More Benefits A tougher runtime model –This model provides an environment for realtime applications that's inherently more robust than Linux - and certainly much tougher than the unprotected realtime kernels used in the dual-kernel approach. A unified environment –The realtime and non-realtime environments are one and the same.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems21 More Benefits Less duplicated effort –All drivers run in user space, so they can be developed using standard source-level tools and techniques.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems22 Background

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems23 An Embedded RTOS At boot-up time, your application usually gets control first, and it then starts the RTOS. Many RTOSs do not protect themselves as carefully from your application as do desktop OS. –For many embedded systems, it may not matter if the application takes the RTOS down with it: the whole system will have to be rebooted anyway.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems24 An Embedded RTOS To save memory RTOSs typically include just the services that you need for your embedded system and no more. Most RTOSs allow you to configure them extensively before you link them to the application.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems25 Several Purchase Options Run faster Use less memory Have a better API Have better debugging tools Support more processors Have more already-debugged network drivers

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems26 Tasks and Task States

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems27 Tasks The basic building block Three states

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems28 The Scheduler It keeps track of the state of each task and decides which one task should go into the running state. The schedulers in most RTOSs are entirely simpleminded about which task should get the processor: –They look at priorities you assign to the tasks. The scheduler assume that you knew what you were doing when you set the task priorities.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems29 State Transitions A task will only block because it decides for itself that it has run out of things to do. While a task is blocked, it never gets the microprocessor. The shuffling of tasks between the ready and running states is entirely the work of the scheduler.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems30 Some Common Questions How does the scheduler know when a task has become blocked or unblocked? –Through a collection of RTOS function calls. What happens if all the tasks are blocked? –The scheduler will spin in some tight loop somewhere inside of the RTOS, waiting for something to happen.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems31 Some Common Questions What if two tasks with the same priority are ready? –It depend upon which RTOS you use. –At least one system solves this problem by making it illegal to have two tasks with the same priority. –Some other RTOSs will time-slice between two such tasks. –Some will run one of them until it blocks and then run the other.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems32 Some Common Questions If one task is running and another, higher- priority task unblocks, does the task that is running get stopped and moved to the state right away? –A preemptive RTOS will stop a lower-priority task as soon. –A non-preemptive RTOS will only take the microprocessor away from the lower-priority task when that task blocks.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems33 A Simple Example The classic situation This task will be unblocked as soon as the user pushes a button.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems34 A Simple Example The computational task

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems35 System Operations Transitions

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems36 Initialization Code Assigning the priorities

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems37 Features of Using an RTOS Two tasks can be written independently of one another, and the system will still respond well. The RTOS will make the response good whenever the user presses a button by turning the microprocessor over to the task that responds to the buttons immediately.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems38 Tasks and Data

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems39 Context Each task has its own private context. –the register values, –a program counter, –a stack. All other data is shared among all of the tasks in the system. –Global –static –initialized –...

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems40 An Example A common data area

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems41 Sharing Data Two main functions vRespondToButtonvCalculateTankLevels

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems42 Shared-data problems Another example –Task2 interrupts Task1

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems43 A clearer examination The assembly code

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems44 A clearer examination The flow

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems45 Reentrancy Reentrant functions are –functions that can be called by more than one task and that will always work correctly. –Even if the RTOS switches from one task to another in the middle of executing the function.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems46 Three Rules A reentrant function may not use variables in a nonatomic way unless they are stored on the stack of the task that called the function or are otherwise the private variables of that task. A reentrant function may not call any other functions that are not themselves reentrant. A reentrant function may not use the hardware in a nonatomic way.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems47 A Review of C Variables Where will the variables in the following code be stored?

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems48 Applying the Reentrant Rules Is this example reentrant?

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems49 Gray Areas of Reentrancy A reentrant function may not use non-stack variables in a non-atomic way. Is incrementing cErrors atomic?

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems50 A closer look A 8051 codeA 80x86 code

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems51 Semaphores and Shared Data

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems52 A Railroad Story The scenario

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems53 RTOS Semaphors take/release TakeSemaphore() and ReleaseSemaphore() A binary semaphore –Only one task can have the semaphore at a time.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems54 A Typical Use The tank application

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems55 The Sequence of Events If the user presses a button while the levels task is still modifying the data and still has the semaphore, –The RTOS will switch to the “button task,” just as before, moving the levels task to the ready state. –When the button task tries to get the semaphore by calling TakeSemaphore it will block because the levels task already has the semaphore. –The RTOS will then look around for another task to run. –When the levels task releases the semaphore by calling ReleaseSemophore, the button task will no longer be blocked.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems56 Execution Flow The flow

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems57 Execution Flow The flow

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems58 The Nuclear Reactor System Semaphore-related functions –OSSemPost(): release the semaphore –OSSemPend(): take the semaphore –OSSemCreate(): initialize the semaphore Related data structures –OS_EVENT: the data representing the semaphore –WAIT_FOREVER: indicates that the task making the call is willing to wait forever Other functions –OSTimeDly(): block functions

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems59 The Code The data structures

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems60 The Code The main function

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems61 The Code Two tasks A potential bug!

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems62 Reentrancy and Semaphores Now adding a semaphore to the previous code Atomic now!

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems63 Multiple Semaphores Some RTOS allows you to have as many semaphores as you like. The advantages –In a system with only one semaphore, if the lower-priority task takes the semaphore to change data, the higher- priority task might block waiting for the semaphore. How does the RTOS know which semaphore protects which data? –It doesn’t. –You must decide what shared data each of your semaphores protects!

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems64 Semaphores as a Signaling Device Another common use of semaphores is as a simple way to communicate –from one task to another or –from an interrupt routine to a task. For example, –printing task –formatting task

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems65 The code The data structures

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems66 The functions Functions

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems67 The functions Functions

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems68 Semaphore Problems Do semaphores represent the solutions to all of our shared-data problems? –No! Semaphores should be also carefully used.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems69 Semaphore Problems Forgetting to take the semaphore Forgetting to release the semaphore Taking the wrong semaphore Holding a semaphore for too long Causing a deadly embrace

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems70 An Example Priority inversion

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems71 Another Example Deadly embrace –Two tasks may all block.

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems72 Semaphore Variants Counting semaphores Resource semaphores Mutex semaphores

元智大學資訊工程系 Systems - Introduction to Real-Time Operating Systems73 Ways to Protect Shared Data Disabling interrupts Using semaphores Disabling task switch