Announcement Class this Wednesday (11/4) will be canceled due to travel ECE 455/555 Embedded System Design 1 Recap from last two classes Hardware support CMOS, Clock gating, Supply shutdown, Dynamic voltage scaling Dynamic power management Adjusts power mode to adapt to workload variations Baseline: greedy policy • Break-even time TBE Energy saving Predictive techniques Power manager Advanced Configuration and Power Interface (ACPI) Holistic approach ECE 455/555 Embedded System Design 2 ECE 455/555 Embedded System Design Operating Systems - I Wei Gao Fall 2015 Basic Functions OS controls resources: who gets the CPU; when I/O takes place; how much memory is allocated. Application programs run on top of OS services Challenge: manage multiple, concurrent tasks. ECE 455/555 Embedded System Design 4 Example: Sensor Motes Sensing Sampling (multiple) sensors in different rates Communication Send data Receive data Computation Data processing and aggregation Routing ECE 455/555 Embedded System Design 5 Life without Processes Code turns into a mess: Spaghetti code do all tasks in a single piece of code (with a loop) no separation of concern Control structure gets very complex Hard to guarantee different timing requirements Cannot handle varying execution time or rate Cannot handle dynamic task arrivals time A B C A C ECE 455/555 Embedded System Design A_code(); … B_code(); … if (C) C_code(); … A_code(); … switch (x) { case C: C(); case D: D(); ... 6 Co-Routines Methodology Idea to realize controlled concurrency Separate tasks into different code segments (co-routines) Co-routines voluntarily give up control to other co-routines. Pattern of control transfers and context-switch are embedded in the code. Co-routine 1 ADR r14,co2a co1a … ADR r13,co1b MOV r15,r14 co1b … ADR r13,co1c MOV r15,r14 co1c ... Co-routine 2 co2a … ADR r14,co2b MOV r15,r13 co2b … ADR r14,co2c MOV r15,r13 co2c … ECE 455/555 Embedded System Design 7 Process A process is a unique execution of a program. Several copies of a program may run simultaneously or at different times. A process has its own context: Data in registers, PC, status, memory. Stored in activation record OS manages processes. TinyOS: a task is similar to a process ECE 455/555 Embedded System Design 8 Processes and CPUs Activation record process context. Context switch: current CPU context goes out new CPU context goes in process 1 context process 2 context ... PC registers CPU memory ECE 455/555 Embedded System Design 9 Cooperative Multitasking Improvement to co-routines: hides context switching mechanism; still relies on processes to voluntarily give up CPU. Each process allows a context switch at cswitch() call. Separate scheduler chooses which process runs next. if (x > 2) sub1(y); else sub2(y, 2); cswitch(); proca(a, b, c); Process 1 Student A save_state(current); p = choose_process(); load_and_go(p); Scheduler TA ECE 455/555 Embedded System Design proc_data(r, s, t); cswitch(); If (val1 == 3) abc(val2); rst(val3); Process 2 Student B 10 Problems with Cooperative Multitasking Programming errors can keep other processes out: process never gives up CPU; process waits too long to switch, missing input. Process2() { x = global1; /* global1 is an input to the process */ while (x < 500) x = aproc(global2); /* subroutine does its work */ cswitch(); } ECE 455/555 Embedded System Design 11 Preemptive Multitasking No more voluntary release of CPU Operating System (OS) is now in charge Timer Most powerful form of multitasking: interrupt OS controls when context switches; OS determines what process runs next. Use periodic timer interrupts to call OS to switch contexts interrupt P1 OS CPU interrupt P1 OS P2 Flow of control with preemption ECE 455/555 Embedded System Design time 12 Process Scheduling Who gets CPU? Usually priority-based • Priority can be based on deadline, requested time, or… When context switch occurs? Low priority tasks are running while high priority tasks are waiting We have two classes to introduce different scheduling algorithms Key issue of real-time operating systems ECE 455/555 Embedded System Design 13 TinyOS: Tasks vs. Interrupts Tasks do intensive computations Unpreemptable FIFO scheduling Bounded number of pending tasks Events handle interrupts Interrupts trigger lowest level events Events can signal events, call commands, or post tasks Two priorities Interrupt POST events Tasks Tasks Preempt FIFO commands commands Interrupts Hardware ECE 455/555 Embedded System Design Time 14 POSIX IEEE standards designed to provide application portability between Unix variants. IEEE 1003.1 defines a Unix-like OS interface. IEEE 1003.2 defines the shell and utilities IEEE 1003.4 defines real-time extensions. Supported by many operating systems Variants of UNIX: AIX, HP-UX, Solaris, Linux Many commercial RTOS, e.g., VxWorks Windows provides similar services ECE 455/555 Embedded System Design 15 Processes in POSIX Create a process with fork: parent process keeps executing the old program; child process executes a new program. Process A Process A Process B ECE 455/555 Embedded System Design 16 Create a New Process using fork() A process can create a child process by making a copy of itself Parent process is returned with the child process ID Child process gets a return value of 0 child_id = fork(); if (child_id == 0) { /* child operations */ } else { /* parent operations */ } ECE 455/555 Embedded System Design 17 Overlay a Process using execv() Child process usually runs different code Use execv() to overlay the code of a process Parent uses wait() to wait for child process to finish and then release its memory childid = fork(); if (childid == 0) { execv(“mychild”,childargs); exit(0); } else { parent_stuff(); wait(&csstatus); exit(0); } ECE 455/555 Embedded System Design 18 Process States A process can be in one of three states: executing on the CPU; ready to run; waiting for data. executing gets CPU Scheduler preempted needs data gets data and CPU gets data ready waiting needs data ECE 455/555 Embedded System Design 19 Process Scheduling: Embedded vs. General-Purpose Workstations try to avoid starving processes of CPU access. Fairness = access to CPU. Embedded systems must meet deadlines. Low-priority processes may not run for a long time. ECE 455/555 Embedded System Design 20 Priority-Driven Scheduling Every process has a priority. CPU goes to the highest-priority ready process Variants Fixed vs. dynamic priority Preemptive vs. non-preemptive ECE 455/555 Embedded System Design 21 Scheduling Example Each process has a fixed priority (1 highest); P1: priority 1; P2: priority 2; P3: priority 3. P3 released P1 released P2 released P2 0 P1 10 20 P2 30 P3 40 ECE 455/555 Embedded System Design 50 time 60 22 The Scheduling Problem Can we meet all deadlines? Must be able to meet deadlines in all cases. How much CPU horsepower do we need to meet our deadlines? Timing violations: What happens if a process doesn’t finish by its deadline? Hard deadline: system fails if missed. Soft deadline: user may notice, but system doesn’t necessarily fail. ECE 455/555 Embedded System Design 23 Example: Space Shuttle Software Error Space Shuttle’s first launch was delayed by a software timing error: Primary control system PASS and backup system BFS. BFS failed to synchronize with PASS. A change to one routine added unnoticed delay that threw off start time calculation. 1 in 67 chance of timing problem. ECE 455/555 Embedded System Design 24 Summary OS: manage multiple, concurrent tasks Process Co-routines methodology Multitasking with context switch Processes in TinyOS and POSIX fork(), execv() Process states and management Process scheduling Priority-driven scheduling Space shuttle software error Inter-process communication Shared memory Message passing ECE 455/555 Embedded System Design 25 Reading Textbook: 6.1-6.3 Recommended reading: Operating System Concepts, A. Silberschatz, B. Galvin and G. Gagne, 8th edition, 2008. (the famous “dinosaur” book) ECE 455/555 Embedded System Design 26