Rtos
Rtos
1 2
Why OS?
    Today’s topic: OS Support                                             Two, or three is fine, but more than five would be difficult
                                                                           without help
    for Real Time Programming                                              you need to take care
                                                                               memory areas
                                                                               Program counters
                                                                               Scheduling, run one instruction each?
                                                                               ....
                                                                               Communication/synchronization
                                                                               Device drivers
                                                                          OS is nothing but a program offering the functions needed in all
                                                                           applications e.g. the start of Enea’s OSE
3 4
An example nano-kernel:
a single task cyclic executive                                         Overall Stucture of Computer Systems
                                                                                                     OS kernel
                                                                                                       Hardware
                                                                   5                                                                          6
                                                                                                                                                  1
Basic functions of OS                               Process, Thread and Task
   Process mangement                                  A process is a program in execution ...
   Memory management                                  Starting a new process is a heavy job for OS: memory has to be
                                                        allocated, and lots of data structures and code must be copied.
   Interrupt handling                                       memory pages (in virtual memory and in physical RAM) for code,
   Exception handling                                       data, stack, heap, and for file and other descriptors; registers in
                                                             the CPU; queues for scheduling; signals and IPC; etc.
   Process Synchronization (IPC)                      A thread is a “lightweight” process, in the sense that different
   Process schedulling                                 threads share the same address space.
                                                            They share global and “static” variables, file descriptors, signal
   Disk management                                          bookkeeping, code area, and heap, but they have own thread
                                                             status, program counter, registers, and stack.
                                                            Shorter creation and context switch times, and faster IPC.
                                                                 to save the state of the currently running task (registers, stack pointer,
                                                                  PC, etc.), and to restore that of the new task.
                                                       Tasks are mostly threads
7 8
11 12
                                                                                                                                                    2
Task Classification by deadlines (i.e. timing constraints)             RT task characterization
    Hard RT task must be computed within the given deadline                     value                                value
     (otherwise system failure), e.g ABS control
         The course will mainly deal with Hard RT tasks                                      Hard deadline                             soft deadline
                                                                                 value                               value
    On-time RT tasks (e.g alarm, robotics)
13 14
Task Classification by release rate (1) Task Classification by release rate (2)
    Periodic tasks: arriving at fixed frequency, can be                   Non-Periodic or aperiodic tasks = all tasks that are
     characterized by 3 parameters (C,D,T) where                            not periodic, also known as Event-driven, their
       C = computing time                                                  activations are generated by interrupts
       D = deadline
       T = period (e.g. 20ms, or 50HZ)
                                                                           Sporadic tasks = aperiodic tasks with minimum
      Often D=T, but it can be D<T or D>T
                                                                            interarrival time Tmin (often with hard deadline)
                                                                                worst case = periodic tasks with period Tmin
     Also called Time-driven tasks, their activations are
     generated by timers
15 16
    Ready
    Running
    Waiting/blocked/suspended ...
                                                                                                          preemption
    Idling                                                                Activate
                                                                                         Ready                                       Runnig
                                                                                                                                                        Terminate
                                                                                                          Dispatch
    Terminated
                                                                                               signal                         wait
Blocked
17 18
                                                                                                                                                                    3
Task states (Ada, delay)                                                              Task states (Ada95)
                                         preemption                                                                        preemption
    Activate                                                          Terminate   created        Activate                                                       Terminate
                      Ready              Dispatch
                                                             Runnig                                         Ready          Dispatch
                                                                                                                                                 Runnig
Blocked Blocked
19 20
     Id
                                                                                             Task mangement
21 22
    Task termination: remove the TCB                                                            Creating an RT task, it has to get the memory without delay: this is
    Change Priority: modify the TCB                                                              difficult because memory has to be allocated and a lot of data
                                                                                                  structures, code seqment must be copied/initialized
    ...
    State-inquiry: read the TCB                                                                 The memory blocks for RT tasks must be locked in main memoery
                                                                                                  to avoid access latencies due to swapping
23 24
                                                                                                                                                                              4
Basic functions of RT OS                                                                    Interrupt Handling
   Task mangement                                                                             Types of interrupts
                                                                                                    Asynchronous (or hardware interrupt) by hardware event (timer, network
                                                                                                     card …) the interrupt handler as a separated task in a different context.
   Interrupt handling                                                                              Synchronous (or software interrupt, or a trap) by software instruction (swi
                                                                                                     in ARM, int in Intel 80x86), a divide by zero, a memory segmentation fault,
   Memory management                                                                                etc. The interrupt handler runs in the context of the interrupting task
   Exception handling                                                                         Challenges in RTOS
                                                                                                    Interrupt latency
   Task synchronization                                                                                 The time between the arrival of interrupt and the start of corresponding ISR.
                                                                                                          Modern processors with multiple levels of caches and instruction pipelines that
   Task scheduling                                                                                   
                                                                                                          need to be reset before ISR can start might result in longer latency.
   Time management                                                                                 Interrupt enable/disable
                                                                                                         The capability to enable or disable (“mask”) interrupt individually.
                                                                                                    Interrupt priority
                                                                                                         to block a new interrupt if an ISR of a higher-priority interrupt is still running.
                                                                                                         the ISR of a lower-priority interrupt is preempted by a higher-priority interrupt.
                                                                                                         The priority problems in task scheduling also show up in interrupt handling.
25 26
27 28
29 30
                                                                                                                                                                                                     5
Exception handling                                                                                       Watch-dog
   Exceptions e.g missing deadline, running out of                                                         A task, that runs (with high priority) in parallel with all others
    memory, timeouts, deadlocks                                                                             If some condition becomes true, it should react ...
        Error at system level, e.g. deadlock                                                                    Loop
        Error at task level, e.g. timeout                                                                        begin
   Standard techniques:                                                                                           ....
        System calls with error code                                                                             end
        Watch dog                                                                                               until condition
        Fault-tolerance (later)                                                                            The condition can be an external event, or some flags
   However, difficult to know all senarios                                                                 Normally it is a timeout
        Missing one possible case may result in disaster
        This is one reason why we need Modelling and Verification
31 32
   Watch-dog (to monitor whether the application task is alive)                                            Task mangement
    Loop                                                                                                    Interrupt handling
      if flag==1 then
                                                                                                            Memory management
         {
                                                                                                            Exception handling
          next :=system_time;
          flag :=0                                                                                          Task synchronization
          }
                                                                                                            Time management
     else if system_time> next+20s then WARNING;
                                                                                                            CPU scheduling
    sleep(100ms)
    end loop
   Application-task
        flag:=1 ... ... computing something ... ... flag:=1 ..... flag:=1 ....
33 34
               while only the task that has taken a mutex is allowed to release it.
                                                                                                                           all tasks always take locks in the same order.
        Spinlock: lock mechanism for multi-processor systems,                                                         
               Before a task gets the write lock, all read locks have to be released.
        Barrier: to synchronize a lot of tasks,
               they should wait until all of them have reached a certain “barrier.”
35 36
                                                                                                                                                                                               6
IPC: Data exchanging                                                      Semaphore, Dijkstra 60s
37 38
39 40
41 42
                                                                                                                                          7
Exercise/Questions                                                         Disadvantages (problems) with semaphores
43 44
   Assume 3 tasks: A, B, C with priorities Ap<Bp<Cp                            Task A with low priority holds S that task C with highest
   Assume semaphore: S shared by A and C                                        priority is waiting.
   The following may happen:                                                   Tast A can not be forced to give up S, but A can be
        A gets S by P(S)                                                        preempted by B because B has higher priority and can run
        C wants S by P(S) and blocked                                           without S
        B is released and preempts A
        Now B can run for a long long period .....
                                                                           So the problem is that ’A can be preempted by B’
        A is blocked by B, and C is blocked by A
        So C is blocked by B
   The above senario is called ’priority inversion’                            Solution 1: no preemption (an easy fix) within CS sections
   It can be much worse if there are more tasks with priorities in             Solution 2: high A’s priority when it gets a semaphore shared
    between Bp and Cp, that may block C as B does!                               with a task with higher priority! So that A can run until it
                                                                                 release S and then gets back its own priority
45 46
47 48
                                                                                                                                                      8
Task states                                                                                                     Scheduling algorithms
49 50
Preemption
51 52
     static priority
         A task is given a priority at the time it is created, and it keeps this
                                                                                                                   Task mangement
          priority during the whole lifetime.                                                                      Interrupt handling
         The scheduler is very simple, because it looks at all wait queues at
          each priority level, and starts the task with the highest priority to                                    Memory management
          run.
    dynamic priority                                                                                              Exception handling
         The scheduler becomes more complex because it has to calculate                                           Task synchronization
          task’s priority on-line, based on dynamically changing parameters.
         Earliest-deadline-first (EDF) --- A task with a closer deadline gets a                                   Task scheduling
          higher scheduling priority.
         Rate-monotonic scheduling
               A task gets a higher priority if it has to run more frequently.                                    Time management
               This is a common approach in case that all tasks are periodic. So, a
                task that has to run every n milliseconds gets a higher priority than a
                task that runs every m milliseconds when n<m.
53 54
                                                                                                                                                                                                   9
    Time mangement                                                                      Time interrupt routine
    A high resolution hardware timer is programmed to                                     Save the context of the task in execution
     interrupt the processor at fixed rate – Time interrupt                                    Increment the system time by 1, if current time > system
    Each time interrupt is called a system tick (time                                          lifetime, generate a timing error
     resolution):                                                                              Update timers (reduce each counter by 1)
                                                                                                    A queue of timers
                                                                                               Activation of periodic tasks in idling state
         Normally, the tick can vary in microseconds (depend on hardware)
         The tick may (not necessarily) be selected by the user                               Schedule again - call the scheduler
         All time parameters for tasks should be the multiple of the tick                     Other functions e.g.
         Note: the tick may be chosen according to the given task parameters                       (Remove all tasks terminated -- deallocate data structures e.g TCBs)
         System time = 32 bits                                                                     (Check if any deadline misses for hard tasks, monitoring)
                    One tick = 1ms: your system can run 50 days
                
                   One tick = 20ms: your system can run 1000 days = 2.5 years             load context for the first task in ready queue
                   One tick = 50ms: your system can run 2500 days= 7 years
55 56
57 58
59 60
                                                                                                                                                                                     10
RT Linux                                                        Scheduling
                                                                   Linux contains a dynamic scheduler
                                                                   RT-Linux allows different schedulers
                                                                       EDF (Earliest Deadline First)
                                                                        Rate-monotonic scheduler
                                                                        Fixed-prioritiy scheduler
61 62
63 64
11