Hawassa University, Institute of Technology School of Electrical and
Computer Engineering
Embedded Systems (ECEg-
5702) Lecture 5: Real‐time Operating systems
By Demisew T. 6/30/21 1 Outline of the Lecture
Introduction
Defining a Task and Real Time
RTOS
Key Characteristics of an RTOS
Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 2 Introduction Simple software applications are typically designed to run sequentially, one instruction at a time, in a pre-determined chain of instructions. However, this scheme is inappropriate for real-time embedded applications, which generally handle multiple inputs and outputs within tight time constraints. Real-time embedded software applications must be designed for concurrency. Concurrent design requires developers to decompose an application into small, schedulable, and sequential program units. When done correctly, concurrent design allows system multitasking to meet performance and timing requirements for a real-time system. Most RTOS kernels provide task objects and task management services to facilitate designing concurrency within an application.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 3 Defining a TASK and Real Time
A task is a program strand of section which has
a clear and distinct purpose and outcome. Multi-tasking simply describes a situation where there are many tasks which need to be performed, ideally simultaneously. For example, a temperature monitoring system is made up of three tasks that normally repeat after a short delay, namely: Task 1 Reads the temperature Task 2 Formats the temperature Task 3 Displays the temperature Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 4 Defining a TASK and Real Time
In reality the tasks are not all of equal
importance. Different tasks have different priorities. High priority task should have the right to execute before a low priority task. Each task also has a deadline. Generally, a task with a tight deadline requires a high priority.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 5 Defining a TASK and Real Time
What is “Real time”?
A system operating in real time must be able to provide the correct results at the required time deadlines. Notice that this definition carries no implication that working in real time implies high speed, although this can often help. It simply states that what is needed must be ready at the time it is needed.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 6 RTOS A real-time operating system (RTOS) is a piece of code (usually called the kernel) that controls task allocation when the microcontroller is operating in a multi-tasking environment. RTOS decides, for instance, which task to run next, how to coordinate the task priorities, and how to pass data and messages among tasks. There are several commercially available RTOS for PIC microcontrollers. Salvo which can be used from a Hi-Tech PIC C compiler. CCS built-in RTOS. Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 7 RTOS RTOS are built around a multi-tasking kernel which controls the allocation of time slices to tasks. A time slice is the period of time a given task has for execution before it is stopped and replaced by another task. This process, also known as context switching, repeats continuously. When context switching occurs, the executing task is stopped, the processor registers are saved in memory, the processor registers of the next available task are loaded into the CPU, and the new task begins execution. An RTOS also provides task-to-task message passing, synchronization of tasks, and allocation of shared resources to tasks.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 8 RTOS
The basic parts of an RTOS are:
Scheduler
RTOS services
Synchronization and messaging tools
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 9 RTOS The scheduler: A scheduler is at the heart of every RTOS, as it provides the algorithms to select the tasks for execution. Three of the more common scheduling algorithms are: • Cooperative scheduling • Round-robin scheduling • Preemptive scheduling
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 10 RTOS Cooperative scheduling is perhaps the simplest scheduling algorithm available. Each task runs until it is complete and gives up the CPU voluntarily. Cooperative scheduling cannot satisfy real-time system needs, since it cannot support the prioritization of tasks according to importance. Also, a single task may use the CPU too long, leaving too little time for other tasks. And the scheduler has no control of the various tasks’ execution time.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 11 RTOS Round-robin scheduling: each task is assigned an equal share of CPU time (see Figure below). A counter tracks the time slice for each task. When one task’s time slice completes, the counter is cleared and the task is placed at the end of the cycle. Newly added tasks are placed at the end of the cycle with their counters cleared to 0. This, like cooperative scheduling, is not very useful in a real-time system, since very often some tasks take only a few milliseconds while others require hundreds of milliseconds or more.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 12 RTOS Preemptive scheduling is considered a real-time scheduling algorithm. It is priority based, and each task is given a priority (see Figure on the next slide). The task with the highest priority gets the CPU time. Real-time systems generally support priority levels ranging from 0 to 255, where 0 is the highest priority and 255 is the lowest.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 13 RTOS Preemptive scheduling In some real-time systems where more than one task can be at the same priority level, preemptive scheduling is mixed with round-robin scheduling. In such cases, tasks at higher priority levels run before lower priority ones, and tasks at the same priority level run by round-robin scheduling. If a task is preempted by a higher priority task, its run time counter is saved and then restored when it regains control of the CPU. In some systems a strict real-time priority class is defined where tasks above this class may run to completion (or run until a resource is not available) even if there are other tasks at the same priority level.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 14 RTOS In a real-time system a task can be in any one of the following states: Ready to run Running Blocked When a task is first created, it is usually ready to run and is entered in the task list. From this state, subject to the scheduling algorithm, the task can become a running task. According to the conditions of preemptive scheduling, the task will run if it is the highest priority task in the system and is not waiting for a resource. A running task becomes a blocked task if it needs a resource that is not available. For example, a task may need data from an A/D converter and is blocked until it is available. Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 15 RTOS Once the resource can be accessed, the blocked task becomes a running task if it is the highest priority task in the system, otherwise it moves to the ready state. Only a running task can be blocked. A ready task cannot be blocked. When a task moves from one state to another, the processor saves the running task’s context in memory, loads the new task’s context from memory, and then executes the new instructions as required.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 16 RTOS
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 17 RTOS The kernel usually provides an interface to manipulate task operations. Typical task operations are: Creating a task Deleting a task Changing the priority of a task Changing the state of a task
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 18 RTOS RTOS Services: RTOS services are utilities provided by the kernel that help developers create real-time tasks efficiently. For example, a task can use time services to obtain the current date and time. Some of these services are: • Interrupt handling services • Time services • Device management services • Memory management services • Input-output services
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 19 RTOS Synchronization and Messaging Tools: Synchronization and messaging tools are kernel constructs that help developers create real-time applications. Some of these services are: • Semaphores • Event flags • Mailboxes • Pipes • Message queues Semaphores are used to synchronize access to shared resources, such as common data areas. Event flags are used to synchronize the intertask activities. Mailboxes, pipes, and message queues are used to send messages among tasks. Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 20 Key Characteristics of an RTOS An application’s requirements define the requirements of its underlying RTOS. Some of the more common attributes are reliability, predictability, performance, compactness, and scalability. the RTOS attribute an application needs depends on the type of application being built.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 21 Key Characteristics of an RTOS Reliability: Depending on the application, the system might need to operate for long periods without human intervention. Different degrees of reliability may be required. For example, a digital solar-powered calculator might reset itself if it does not get enough light, yet the calculator might still be considered acceptable. On the other hand, a telecom switch cannot reset during operation without incurring high associated costs for down time. Although different degrees of reliability might be acceptable, in general, a reliable system is one that is available (continues to provide service) and does not fail. Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 22 Key Characteristics of an RTOS Predictability: The RTOS needs to be predictable to a certain degree. The term deterministic describes RTOSes with predictable behavior, in which the completion of operating system calls occurs within known timeframes. In a good deterministic RTOS, the variance of the response times for each type of system call is very small.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 23 Key Characteristics of an RTOS Performance: This requirement dictates that an embedded system must perform fast enough to fulfill its timing requirements. Typically, the more deadlines to be met—and the shorter the time between them—the faster the system’s CPU must be. Although underlying hardware can dictate a system’s processing power, its software can also contribute to system performance. Throughput also measures the overall performance of a system, with hardware and software combined. One definition of throughput is the rate at which a system can generate output based on the inputs coming in. Sometimes developers measure RTOS performance on a call-by-call basis. Benchmarks are written by producing timestamps when a system call starts and when it completes. Although this step can be helpful in the analysis stages of design, true performance testing is achieved only when the system performance is measured as a whole. Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 24 Key Characteristics of an RTOS Compactness: Application design constraints and cost constraints help determine how compact an embedded system can be. For example, a cell phone clearly must be small, portable, and low cost. These design requirements limit system memory, which in turn limits the size of the application and operating system. In such embedded systems, where hardware real estate is limited due to size and costs, the RTOS clearly must be small and efficient. In these cases, the RTOS memory footprint can be an important factor. To meet total system requirements, designers must understand both the static and dynamic memory consumption of the RTOS and the application that will run on it.
Hawassa University, Institute of Technology School
of Electrical and Computer Engineering 6/30/21 25 Key Characteristics of an RTOS Scalability: Because RTOSes can be used in a wide variety of embedded systems, they must be able to scale up or down to meet application- specific requirements. Depending on how much functionality is required, an RTOS should be capable of adding or deleting modular components, including file systems and protocol stacks. If an RTOS does not scale up well, development teams might have to buy or build the missing pieces. Suppose that a development team wants to use an RTOS for the design of a cellular phone project and a base station project. If an RTOS scales well, the same RTOS can be used in both projects, instead of two different RTOSes, which saves considerable time and money. Hawassa University, Institute of Technology School of Electrical and Computer Engineering 6/30/21 26