Real-Time Operating Systems Lecture for the Embedded Systems Course CSD, University of Crete (May 23, 2014) ManolisMarazakis (maraz@ics.forth.gr) Institute of Computer Science (ICS) Foundation for Research and Technology Hellas (FORTH)
Real-Time OS =? Real-Time Control External Events Synchronous, Asynchronous, Independent Speed Fast response, Low overheads Deterministic A late answer is a wrong answer Multi-Tasking Sequence: One task controlling all events in an infinite loop Multi-tasking: Task Waiting on Event, Task becomes Ready upon Event Priority Scheduling Preemptive Scheduling on Task Priority No delay on Context Switch to the next Tick Equal Priority tasks won t preempt each other Scheduling due to synchronous or asynchronous event Round-Robin Scheduling All tasks in the same priority share the CPU Context Switch in each (predefined) time slice period 2 Real-Time Operating Systems
Need for Synchronization? Reentrancy & SharedResources Co-ordination between tasks Asynchronous events Interrupts & Exceptions Interrupts allow devices to notify the CPU on events Interrupt Levels Exceptions Unplanned event generated by the CPU (e.g. divide-by-0) Exceptions will generate an internal interrupt ISR (Interrupt Service Routines) Not a task (has no task control block ) Timers Synchronous events 3 Real-Time Operating Systems
RTOS vs General-Purpose OS General-purpose OS: Key criteria: Task throughput, fairness average case optimizations Real-Time OS: Deterministic timing behavior worst-case guarantees Processing must be done within a time constraint or the system will fail. All delays in the system will be bounded; from the retrieval of stored data to the time RTOS finishes the request. 4 Real-Time Operating Systems
Can Linux provide real-time guarantees? (NO) (usually) Non-preemptive kernel A system call may sometimes take a long time to complete Coarse timer resolution Tasks can be released with millisecond precision (1/10/100) Virtual memory Unpredictable delay Variable task priority Task priority varies with time, to achieve fairness Batching and possible re-ordering of storage & network operations (for efficient use of H/W) High-priority task may queue behind a low-priority task that has issued a system call High-priority tasks may have to wait for lower priority tasks to release resources 5 Real-Time Operating Systems
Can Linux provide real-time guarantees? (YES) Provides RT-POSIX interface Fixed priority real-time scheduling classes SCHED_FIFO SCHED_RR Timesharing in SCHED_OTHER Preemptive Linux kernel Concerns: Real-time? Footprint? 6 Real-Time Operating Systems
Outline RTOS theory LynxOS QNX Wind River VxWorks RTLinux TimeSysLinux POSIX 1003.1b standard Asynchronous I/O Semaphores Message queues Queued signals Scheduling Clocks &timers Memory Management 7 Real-Time Operating Systems
Three Models of Kernel Kernel =? Process Management Memory Management I/O System Management Three models of kernel : Monolithic Layered Microkernel (client-server) 8 Real-Time Operating Systems
Monolithic Kernel In a monolithic-modularized OS, the functionality is integrated into a single executable file that is made up of modules, separate pieces of code reflecting various OS functionality. 9 Real-Time Operating Systems
Layered Kernel In the layered design, the OS is divided into hierarchical layers (0...N), where upper layers are dependent on the functionality provided by the lower layers (via APIs). Like the monolithic design, layered OSesare a single large file that includes device driversand middleware 10 Real-Time Operating Systems
Microkernel (Client-server) OS An OS that is stripped down to minimal functionality, commonly only process and memory management subunits is called a client-server OS, or a microkernel. The remaining functionality typical of other kernel algorithms is abstracted out of the kernel, while device drivers, for instance, are usually abstracted out of a microkernel entirely 11 Real-Time Operating Systems
Non-preemptiveScheduling Tasks are given control of the master CPU until they have finished execution, regardless of the length of time or the importance of the other tasks that are waiting. First-Come-First-Serve (FCFS)/ Run-To-Completion The response time of a FCFS algorithm is typically slower than other algorithms Shortest Process Next (SPN)/Run-To-Completion SPN algorithm has faster response times for shorter processes. However, then the longer processes are penalized by having to wait until all the shorter processes in the queue have run. In this scenario, starvation can occur to longer processes if the ready queue is continually filled with shorter processes. The overhead is higher than that of FCFS, since the calculation and storing of run times for the processes in the ready queue must occur. Co-operative: the tasks themselves run until they tell the OS when they can be context switched (i.e., for I/O, etc.). This algorithm can be implemented with the FCFS or SPN algorithms, rather than the run-to-completion scenario, but starvation could still occur with SPN if shorter processes were designed not to cooperate, for example. 12 Real-Time Operating Systems
Preemptive Scheduling Round Robin/FIFO (First In, First Out) Scheduling Each process in the FIFO queue is allocated an equal time slice (the duration each process has to run). An interrupt is generated at the end of each of these intervals to start the pre-emption process. Priority (Preemptive) Scheduling Every process is assigned a priority The processes with the highest priority always preempt lower priority processes when they want to run Problems: Process starvation, Priority inversion, how to determine the priorities of various processes, i.e. same priority vs how often the process(es) are run 13 Real-Time Operating Systems
Preemptive Scheduling (cont.) Rate Monotonic Scheduling (RMS) scheme, in which tasks are assigned a priority based upon how often they execute within the system. EDF (Earliest Deadline First)/Clock Driven Scheduling The EDF/Clock Driven algorithm schedules priorities to processes according to three parameters: frequency (number of times process is run) deadline (when processes execution needs to be completed) duration (time it takes to execute the process) 14 Real-Time Operating Systems
Preemptive Scheduling & RTOS Tasks with real-time requirements have to be allowed to preempt other tasks Preemptive scheduling must be one of the algorithms implemented within RTOS schedulers RTOS schedulers also make use of their own array of timers, ultimately based upon the system clock, to manage and meet their hard deadlines. Whether an RTOS or a non real-time OS in terms of scheduling, all will vary in their implemented scheduling schemes. E.g: vxworks(wind River) : priority-based + round-robin Jbed(Esmertec) : EDF Linux (Timesys) : priority-based 15 Real-Time Operating Systems
LynxOS(1/2) Microkernel design Small kernel footprint : ~28 KB in size The microkernel provides essential services in scheduling, interrupt dispatching and synchronization The other services are provided by kernel lightweight service modules Kernel Plug-Ins (KPIs) New KPIs can be added to the microkernel to support I/O, file systems, TCP/IP, streams and sockets Can function as a multipurpose UNIX OS LynxOSoffers memory protection through hardware MMUs Applications make I/O requests to I/O system through system calls Kernel directs I/O request to the device driver 16 Real-Time Operating Systems
LynxOS(2/2) Each device driver has an interrupt handler and kernel thread The interrupt handler carries the 1 st step of interrupt handling If it does not complete the processing, it sets an asynchronous trap to the kernel Later, when kernel can respond to the software interrupt, it schedules an instance of the kernel thread to complete the interrupt processing 17 Real-Time Operating Systems
QNX Neutrino (1/2) Microkernel design kernel provides essential threads and realtime services Microkernel footprint: ~12 KB Other services are considered as resource managers and can be added or removed at run-time SMP RTOS requires high end, networked SMP machines with GBs of physical memory Message passing: Messages are the basic means of inter-process communication among all threads Message-based priority tracking Messages are delivered at the priority order and the service provider executes at the priority of the highest priority clients waiting for service So, if the highest priority task wants to do read some data from file, the file system resource manager will execute at this task s priority 18 Real-Time Operating Systems
QNX Neutrino (2/2) When a service provider thread wants to provide service, then it creates a channel (for exchanging messages) with its service identifier for identification To get a service from a provider, the client thread attaches it to the provider s channel Within the client, this connection is directly mapped to the file descriptor (so RFS can be sent directly to the file descriptor) QNX messages are blocking (unlike POSIX standards) 19 Real-Time Operating Systems
VxWorks(1/4) Microkernel Monolithic Kernel (CoreOS+ Wind microkernel) Provides interfaces specified by RT-POSIX standards in addition to its own APIs Shared-memory objects: shared binary and counting semaphores Standard MMU (as in modern OS) Provides basic virtual-to-physical memory mapping Allows to add new mappings and make portions of memory non cacheable When memory boards are added dynamically, to increase the address space for inter-process communication The data is made non cacheable, to ensure cache consistency 20 Real-Time Operating Systems
VxWorks(2/4) Reduced Context Switch time Saves only those register windows that are actually in use (e.g. applicable on SPARC) When a task s context is restored, only the relevant register window is restored To improve response time, it saves the register windows in a register cache useful for recurring tasks 21 Real-Time Operating Systems
VxWorks(3/4) Real-Time Embedded Application Graphics Multiprocessing Internet Java Support POSIX Library File System WindNet Networking Core OS: Wind Microkernel 22 22 Real-Time Operating Systems
VxWorks(4/4) 23 Real-Time Operating Systems
RTLinux(1/3) Patch-set for Linux kernel + RT API Runs Linux kernel as lowest-priority task (pre-emptible) User Processes Device drivers System libraries Linux kernel I/O Hardware Interrupts Hardware 24 Real-Time Operating Systems 24
RTLinux(2/3) Linux is executed in the background User Processes Real Time Tasks Device drivers System libraries Linux kernel Direct h/w access RT-Scheduler RTLinux Plug-in I/O I/O Software Interrupts Hardware Interrupts Hardware 25 Real-Time Operating Systems 25
RTLinux(3/3) Development flow for RT-application program: 1. Develop RT-application program : Linux kernel module 2. Load RT-Core / RT-Scheduler/ RT-FIFOs transit to RTLinux 3. Load RT-application module Interrupt control H/W Real-Time Kernel Linux Linux Processes Real-Time FIFOs Real-Time Tasks 26 Real-Time Operating Systems
Linux/RK A Kernel that provides to applications Timely, Guaranteed, and Enforced access to System Resources Allows Applications to specify only their Resource Demands User-Level leaving the Kernel to satisfy Demands using hidden management schemes Linux Process Kernel Resource Kernel Kernel Loadable Kernel Module Linux Process Hardware Linux Kernel Linux Process 27 Real-Time Operating Systems Reservation Parameters T : Period (1/f) C : Execution time within period D : Deadline within period
Hard Reservation 28 Real-Time Operating Systems
Firm Reservation 29 Real-Time Operating Systems
Soft Reservation 30 Real-Time Operating Systems
Commercialized Linux/RK: TimeSys Linux Resource kerneland QoSSupport guaranteed, timelyand enforced access to CPU cycles and network bandwidth SMP support with QoSReservations Fully preemptive kernel Fixed-priority scheduling (POSIX-compliant) High-resolution timer and clock support (microsecond resolution) Periodic processes Message queues Priority inheritanceand priority ceiling protocol emulation support to avoid unbounded priority inversion TimeSysLinux/GPL Basic TimeSysLinux kernel Full preemption at the kernel level, prioritized interrupt handlers, unlimited priorities,... TimeSysLinux/Real-time Support priority inheritance and a POSIXbased high-resolution timer API TimeSysLinux/CPU Support CPU reservation, which gives a thread, process, or process group exclusive use of the CPU. TimeSysLinux/Net Support network bandwidth reservation to guarantee that your thread or process will get the bandwidth it requires, regardless of network activity in other processes TimeSysLinux GPL: Downloadable from sourceforge.net/projects/timesysgpl 31 Real-Time Operating Systems
Sources Victor Yodaiken, The RTLinuxManifesto, In Proceedings of the 5 th Linux Expo, 1999 Victor Yodaiken, CortDougan, Michael Barabanov, RTLinux/RTCoredual kernel real-time operating system, FSM Labs White Paper, 2004 [ available from http://vyodaiken.com/papers-and-talks/] QNX Neutrino RTOS System Architecture [ available from qnx.com/download/ ] http://www.lynx.com/products/real-time-operatingsystems/lynxos-rtos/ http://www.vxworks.com/collateral Raj Rajkumar, Kanaka Juvva, AnastasioMolanoand ShuiOikawa, Resource Kernels: A Resource-Centric Approach to Real-Time Systems, In Proceedings of the SPIE/ACM Conference on Multimedia Computing and Networking, January 1998. 32 Real-Time Operating Systems