Real-Time Operating Systems

Real-Time Operating Systems

Real-Time Operating Systems

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. Real-Time Operating Systems Read Chapter 6 (David E. Simon, An Embedded Software Primer) Read Chapter 1/ Chapter 2 (Labrosse)

2. What, Why, How? • What does real time mean? • What is an OS? • Why would I need an OS? • Why would I need a real time OS? • How do we use an OS on embedded systems? Embedded Systems Development Software Architectures S.Biaz

3. Is This a Real Time System? • Consider the following case: • A human operator must regulate the temperature of a system with a +/- 5 degrees F • The temperature adjusts the current intensity to regulate the temperature. • The oven has a huge inertia: its temperature varies by at most 2 degrees per hour • The union imposes 2 coffee breaks a day • The coffee break was negotiated to last one hour! Embedded Systems Development Software Architectures S.Biaz

4. Is This a Real Time System? (2) • Consider the following case: • Consider a system that can achieve 10 Mflops • We want to use this machine for 24-hour weather prediction. • The atmosphere is respectively divided in longitude and latitude in areas of 200 miles X 200 miles. Measures are made at different altitudes • It takes 100 billion floating operations to solve the Navier-Stockes equations. Embedded Systems Development Software Architectures S.Biaz

5. Is This a Real Time System? (3) • Consider the following case: • Consider the problem on previous slide. • The resolution of 200 miles is too coarse • Suppose we double the resolution in 4 dimensions. • Volume of data increases 16 fold, so does execution time Embedded Systems Development Software Architectures S.Biaz

6. What Is an O.S? • A piece of software • It provides tools to manage (for embedded systems) • Processes, (or tasks) • Memory space Embedded Systems Development Software Architectures S.Biaz

7. What Is an Operating System? • What? It is a program (software) that acts as an intermediary between a user of a computer and the computer hardware. • Why? Make the use of a computer CONVENIENT and EFFICIENT. • How? • We have one semester to answer this question. Embedded Systems Development Software Architectures S.Biaz

8. What Is an Operating System?For an Embedded System • Provides software tools for a convenient and prioritized control of tasks. • Provides tools for task (process) synchronization. • Provides a simple memory management system Embedded Systems Development Software Architectures S.Biaz

9. Convenient and Efficient?(General Purpose) • Convenient : • Simplicity of use • Messy or complicated details are hidden • User is unaware that he is using an OS • Efficient • Resources are optimally used Embedded Systems Development Software Architectures S.Biaz

10. Abstract View of A System(General Purpose) Embedded Systems Development Software Architectures S.Biaz

11. Layered View of a Computer System • A computer system consists of • hardware • system programs (Blurry frontier) • application programs Services Requests Embedded Systems Development Software Architectures S.Biaz

12. Abstract View of A System(Embedded System) Application OS Hardware Embedded Systems Development Software Architectures S.Biaz

13. Operating System Definitions(General Purpose) • Resource allocator – manages and allocates resources • Control program – controls the execution of user programs and operations of I/O devices • Kernel – the one program running at all times (all else being application programs) Embedded Systems Development Software Architectures S.Biaz

14. Multiprogramming CPU • Objective: Better control of tasks Embedded Systems Development Software Architectures S.Biaz

15. Dual-Mode Operation • Sharing system resources requires operating system to ensure that an incorrect program or poorly behaving human cannot cause other programs to execute incorrectly • OS must provide hardware support to differentiate between at least two modes of operations 1.User mode – execution done on behalf of a user 2. Monitor mode (also kernel mode or system mode) – execution done on behalf of operating system Embedded Systems Development Software Architectures S.Biaz

16. Dual-Mode Operation (Cont.) • Mode bit added to computer hardware to indicate the current mode: monitor (0) or user (1) • When an interrupt or fault occurs hardware switches to monitor mode Interrupt/fault kernel user set user mode Privileged instructions can be issued only in kernel mode Embedded Systems Development Software Architectures S.Biaz

17. Process/Task Concept • An operating system executes a variety of programs: • Batch system – jobs • Time-shared systems – user programs or tasks • Similar terms job, process, task (ES) almost interchangeably Embedded Systems Development Software Architectures S.Biaz

18. Process/Task Concept (Cont.) • Process – a program in execution; process execution must progress in sequential fashion • A process includes: • program counter • stack • data section Embedded Systems Development Software Architectures S.Biaz

19. Example of ProcessesThe Process Model CPU PC • Multiprogramming of four programs • Conceptual model of 4 independent, sequential processes • Only one program active at any instant Embedded Systems Development Software Architectures S.Biaz

20. Multitasking Embedded Systems Development Software Architectures S.Biaz

21. Key Difference on Embedded Systems • General Purpose OS: processes may be initiated by: • Different users • Different application • Embedded system: tasks are part of a unique application Embedded Systems Development Software Architectures S.Biaz

22. Process/Task State • Bottom line: keep track of which process/task runs (has the CPU) • As a process executes, it changes state (in some states, process/task does need CPU) • new: The process is being created • running: Instructions are being executed • waiting: The process is waiting for some event to occur • ready: The process is waiting to be assigned to a process • terminated: The process has finished execution Embedded Systems Development Software Architectures S.Biaz

23. Diagram of Process State Embedded Systems Development Software Architectures S.Biaz

24. Diagram of Task State Embedded Systems Development Software Architectures S.Biaz

25. Process Control Block (PCB) Information associated with each process • Process state • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information Embedded Systems Development Software Architectures S.Biaz

26. Process Control Block (PCB) Embedded Systems Development Software Architectures S.Biaz

27. CPU Switch From Process to Process:Context Switch Embedded Systems Development Software Architectures S.Biaz

28. Ready Queue And Various I/O Device Queues Embedded Systems Development Software Architectures S.Biaz

29. CPU Scheduler • What? This is a prime OS function that decides which process/task will run • Why? The application is made of multiple processes/tasks with possibly different time constraints. CPU scheduler implements this prioritization • How? On embedded systems, priorities assigned by the programmer are the main criteria Embedded Systems Development Software Architectures S.Biaz

30. Scheduler • Non preemptive (kernel): the process/task relinquishes VOLUNTARILY the CPU. • Preemptive (kernel): OS preempts the CPU from a task and takes control of the CPU after a scheduled event or an interrupt . Embedded Systems Development Software Architectures S.Biaz

31. Example:(http://www.netrino.com/Publications/Glossary/RMA.html)Example:(http://www.netrino.com/Publications/Glossary/RMA.html) • P1 = 50ms, C1= 25ms (CPU uti. = 50%) • P2 = 100ms, C2= 40ms (CPU uti. = 40%) • Looks like total CPU utilization is less than 100%: can these tasks be scheduled real time? (not missing their deadlines, next cycle start) • How to schedule these and not miss deadlines? Embedded Systems Development Software Architectures S.Biaz

32. Example (cont’d):(http://www.netrino.com/Publications/Glossary/RMA.html) • Case 1: Priority(Task1) > Priority(Task2) • Case 2: Priority(Task2) > Priority(Task1) • P1 = 50ms, C1= 25ms (CPU uti. = 50%) • P2 = 100ms, C2= 40ms (CPU uti. = 40%) Embedded Systems Development Software Architectures S.Biaz

33. Question: • If I assign priorities such that most frequent tasks get highest priorities, does my scheduling GUARANTEE meeting the deadlines? Embedded Systems Development Software Architectures S.Biaz

34. Answer: Rate Monotonic Scheduling (RMS) • Given • m periodic events • event i occurs within period Pi and requires at most Ci seconds • Tasks are independent (do not have to synchronize) • Tasks are prioritized based on highest rate. Highest priority goes first • Preemptive scheduling is used • Then the load can only be handled if and only if m C å £ i m(2(1/m) -1) P = i 1 i Embedded Systems Development Software Architectures S.Biaz

35. RMS: Example • Let a system with 3 tasks T1, T2, and T3: • T1 requires 3 ms every 10ms • T2 requires 5 ms every 15 ms • T3 requires 1 ms every 5 ms • Can these tasks be scheduled in real time? (Deadline is next time cycle) Embedded Systems Development Software Architectures S.Biaz

36. If a Task Can Be Interrupted.. • We get problems: • Shared data problem • Non reentrant functions Embedded Systems Development Software Architectures S.Biaz

37. Reentrant Function • A function F is reentrant if it can be invoked by multiple processes/tasks that can execute F CORRECTLY and concurrently. • In other words, F is correctly used by multiple processes/tasks at the same time. Embedded Systems Development Software Architectures S.Biaz

38. Reentrant Function (2) • In general, if a function modifies only local variables (usually stored on stack), it is reentrant. • Example: non reentrant function int temp; /* global variable */ void swap(int *x, int *y){ temp = *x; *x = *y; *y = temp; } Embedded Systems Development Software Architectures S.Biaz

39. Dealing With Non Reentrant F. • Modify functions to make them reentrant • If not possible, treat them as critical regions (sections) and enforce mutual exclusion Embedded Systems Development Software Architectures S.Biaz

40. Enforcing Mutual Exclusion • Disable/Enable interrupts: • OS_ENTER_CRITICAL() • OS_EXIT_CRITICAL() • Using Test-and-Set function • Disable/Enable scheduling: • OSSchedLock() • OSSchedUnlock() • Using semaphores Embedded Systems Development Software Architectures S.Biaz

41. Hardware Support (1) • Implementation of Test-And-Set Instruction • Test-and Set must be an atomic operation (either completely executed or not executed at all) Boolean Test-and-Set(int *lock){ int register; register = *lock; lock = 1; return(register = = 0); } • Test-and-Set atomically: • Sets lock to 1 • Tests whether the lock was open (0) Embedded Systems Development Software Architectures S.Biaz

42. Mutual Exclusion Using Test-And-Set while (1){ While (!Test-And-Set(&Lock)); Critical Region Lock = 0; Remainder } /* end while (1) */ System variables: Lock = 0; while (1){ While (!Test-And-Set(&Lock)); Critical Region Lock = 0; Remainder } /* end while (1) */ Mutual Exclusion ? Yes Bounded Waiting ? Yes Progress ? Yes Embedded Systems Development Software Architectures S.Biaz

43. Semaphore (Cont’d) • Down function: (Wait) if (S <= 0) sleep(); S = S – 1; • Up function: (Signal) S = S + 1; wake up some process Mutexes are binary semaphores Embedded Systems Development Software Architectures S.Biaz

44. Mutual Exclusion Using Semaphores mC System variables: Lock = 0; while (1){ while (1){ OSSemPend(SharedDataSem,0,&err); OSSemPend(SharedDataSem,0,&err); Critical Region Critical Region OSSemPost(SharedDataSem); OSSemPost(SharedDataSem); Remainder Remainder } /* end while (1) */ } /* end while (1) */ Mutual Exclusion ? Yes Bounded Waiting ? Yes Progress ? Yes Embedded Systems Development Software Architectures S.Biaz

45. Semaphores for Scheduling:unilateral rendezvous • Consider a task that waits for some event that can be generated by another task or some interrupt. • Solution 1: use a flag variable • Solution 2: use a semaphore Embedded Systems Development Software Architectures S.Biaz

46. Semaphores for Scheduling: unilateral rendezvous (2) Embedded Systems Development Software Architectures S.Biaz

47. Semaphores Scheduling:Startup Barrier • Consider two tasks that must start some work at the same time. Embedded Systems Development Software Architectures S.Biaz