1.69k likes | 2.19k Views
CH6-1. Processes and operating systems. Motivation for processes. The process abstraction. Context switching. Multitasking. Processes and UML. Why multiple processes?. Processes help us manage timing complexity: multiple rates multimedia automotive asynchronous input user interfaces
E N D
CH6-1 Processes and operating systems • Motivation for processes. • The process abstraction. • Context switching. • Multitasking. • Processes and UML. Overheads for Computers as Components
Why multiple processes? • Processes help us manage timing complexity: • multiple rates • multimedia • automotive • asynchronous input • user interfaces • communication systems Overheads for Computers as Components
Tasks: spark control crankshaft sensing fuel/air mixture oxygen sensor Kalman filter state machine Example: engine control engine controller Overheads for Computers as Components
Code turns into a mess: interruptions of one task for another spaghetti code Life without processes A_code(); … B_code(); … if (C) C_code(); … A_code(); … switch (x) { case C: C(); case D: D(); ... A time B C A C Overheads for Computers as Components
ADR r14,co2a co1a … ADR r13,co1b MOV r15,r14 co1b … ADR r13,co1c MOV r15,r14 co1c ... co2a … ADR r13,co2b MOV r15,r13 co2b … ADR r13,co2c MOV r15,r13 co2c … Co-routines Co-routine 1 Co-routine 2 Overheads for Computers as Components
Co-routine methodology • Like subroutine, but caller determines the return address. • Co-routines voluntarily give up control to other co-routines. • Pattern of control transfers is embedded in the code. Overheads for Computers as Components
Cyclic scheduler • Run processes in order. • Limited ability to handle multi-rate systems. P1 P4 P2 P3 Overheads for Computers as Components
Timed cyclic scheduler • Start process execution based on timer. P1 P4 P2 P3 Overheads for Computers as Components
Use asynchronous interrupts or timer to run event handlers. Requires one timer per rate. Ties execution to hardware priorities. Interrupt-driven scheduler low-priority interrupt interrupt interrupt P1 P2 P3 t Overheads for Computers as Components
Processes • 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 state: • registers; • memory. • The operating system manages processes. Overheads for Computers as Components
Activation record: copy of process state. Context switch: current CPU context goes out; new CPU context goes in. Processes and CPUs process 1 PC registers process 2 CPU ... memory Overheads for Computers as Components
Terms • Thread = lightweight process: a process that shares memory space with other processes. • Reentrancy: ability of a program to be executed several times with the same results. Overheads for Computers as Components
Create a process with fork: parent process keeps executing old program; child process executes new program. Processes in POSIX process a process a process b Overheads for Computers as Components
fork() • The fork process creates child: childid = fork(); if (childid == 0) { /* child operations */ } else { /* parent operations */ } Overheads for Computers as Components
execv() • Overlays child code: childid = fork(); if (childid == 0) { execv(“mychild”,childargs); perror(“execv”); exit(1); } file with child code Overheads for Computers as Components
Context switching • Who controls when the context is switched? • How is the context switched? Overheads for Computers as Components
Co-operative multitasking • Improvement on co-routines: • hides context switching mechanism; • still relies on processes to give up CPU. • Each process allows a context switch at cswitch() call. • Separate scheduler chooses which process runs next. Overheads for Computers as Components
Problems with co-operative multitasking • Programming errors can keep other processes out: • process never gives up CPU; • process waits too long to switch, missing input. Overheads for Computers as Components
Context switching • Must copy all registers to activation record, keeping proper return value for PC. • Must copy new activation record into CPU state. • How does the program that copies the context keep its own context? Overheads for Computers as Components
Save old process: STMIA r13,{r0-r14}^ MRS r0,SPSR STMDB r13,{r0,r15} Start new process: ADR r0,NEXTPROC LDR r13,[r0] LDMDB r13,{r0,r14} MSR SPSR,r0 LDMIA r13,{r0-r14}^ MOVS pc,r14 Context switching in ARM Overheads for Computers as Components
Preemptive multitasking • Most powerful form of multitasking: • OS controls when contexts switches; • OS determines what process runs next. • Use timer to call OS, switch contexts: interrupt CPU timer Overheads for Computers as Components
Flow of control with preemption interrupt interrupt P1 OS P1 OS P2 time Overheads for Computers as Components
Preemptive context switching • Timer interrupt gives control to OS, which saves interrupted process’s state in an activation record. • OS chooses next process to run. • OS installs desired activation record as current CPU state. Overheads for Computers as Components
Why not use interrupts? • We could change the interrupt vector at every period, but: • we would need management code anyway; • we would have to know the next period’s process at the start of the current process. Overheads for Computers as Components
Processes and UML • A process is an active class---independent thread of control. processClass1 myAttributes myOperations() start resume Signals Overheads for Computers as Components
UML signals • Signal: object that is passed between processes for active communication: acomm: datasignal Overheads for Computers as Components
Designing with active objects • Can mix normal and active objects: p1: processClass1 a: rawMsg w: wrapperClass ahat: fullMsg master: masterClass Overheads for Computers as Components
CH6-2 Outline • Review. • Varieties of scheduling. • Static scheduling. • Feasibility. • Scheduling algorithms. Overheads for Computers as Components
Reactive systems • Respond to external events. • Engine controller. • Seat belt monitor. • Requires real-time response. • System architecture. • Program implementation. • May require a chain reaction among multiple processors. Overheads for Computers as Components
Real-time systems • Perform a computation to conform to external timing constraints. • Deadline frequency: • Periodic. • Aperiodic. • Deadline type: • Hard: failure to meet deadline causes system failure. • Soft: failure to meet deadline causes degraded response. • Firm: late response is useless but some late responses can be tolerated. Overheads for Computers as Components
Why scheduling? • The CPU is often shared among several processes. • Cost. • Energy/power. • Physical constraints. • Someone must be responsible for giving the CPU to processes. • Co-operation between processes. • RTOS. Overheads for Computers as Components
Why CPU scheduling is not like hardware scheduling • Large variation in run times. • Asynchronous system architecture—no global clock to control processes. • Larger data-dependent variations in run time, coupled with greater desire to take advantage of slack times. • Processes may have variable start times. Overheads for Computers as Components
Processes • 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 state: • registers; • memory. • The operating system manages processes. Overheads for Computers as Components
Timing requirements on processes • Period: interval between process activations. • Rate: reciprocal of period. • Initiation time: time at which process becomes ready. • Deadline: time at which process must finish. Overheads for Computers as Components
Process characteristics • Process execution time Ti. • Execution time in absence of preemption. • Possible time units: seconds, clock cycles. • Worst-case, best-case execution time may be useful in some cases. • Sources of variation: • Data dependencies. • Memory system. • CPU pipeline. Overheads for Computers as Components
A process can be in one of three states: executing on the CPU; ready to run; waiting for data. State of a process executing gets data and CPU gets CPU preempted needs data gets data ready waiting needs data Overheads for Computers as Components
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? Overheads for Computers as Components
Scheduling metrics • CPU utilization: • Fraction of the CPU that is doing useful work. • Often calculated assuming no scheduling overhead. • Utilization: • U = [ St1 ≤ t ≤ t2 T(t) ] / [t2 – t1] Overheads for Computers as Components
Scheduling feasibility • Resource constraints make schedulability analysis NP-hard. • Must show that the deadlines are met for all timings of resource requests. P1 P2 I/O device Overheads for Computers as Components
Simple processor feasibility • Assume: • No resource conflicts. • Constant process execution times. • Require: • T ≥ Si Ti • Can’t use more than 100% of the CPU. T1 T2 T3 T Overheads for Computers as Components
Hyperperiod • Hyperperiod: least common multiple (LCM) of the task periods. • Must look at the hyperperiod schedule to find all task interactions. • Hyperperiod can be very long if task periods are not chosen carefully. Overheads for Computers as Components
Hyperperiod example • Long hyperperiod: • P1 7 ms. • P2 11 ms. • P3 15 ms. • LCM = 1155 ms. • Shorter hyperperiod: • P1 8 ms. • P2 12 ms. • P3 16 ms. • LCM = 96 ms. Overheads for Computers as Components
Simple processor feasibility example • P1 period 1 ms, CPU time 0.1 ms. • P2 period 1 ms, CPU time 0.2 ms. • P3 period 5 ms, CPU time 0.3 ms. Overheads for Computers as Components
P Cyclostatic/TDMA • Schedule in time slots. • Same process activation irrespective of workload. • Time slots may be equal size or unequal. T1 T2 T3 T1 T2 T3 P Overheads for Computers as Components
PLCM TDMA assumptions • Schedule based on least common multiple (LCM) of the process periods. • Trivial scheduler -> very small scheduling overhead. P1 P1 P1 P2 P2 Overheads for Computers as Components
TDMA schedulability • Always same CPU utilization (assuming constant process execution times). • Can’t handle unexpected loads. • Must schedule a time slot for aperiodic events. Overheads for Computers as Components
TDMA schedulability example • TDMA period = 10 ms. • P1 CPU time 1 ms. • P2 CPU time 3 ms. • P3 CPU time 2 ms. • P4 CPU time 2 ms. Overheads for Computers as Components
P Round-robin • Schedule process only if ready. • Always test processes in the same order. • Variations: • Constant system period. • Start round-robin again after finishing a round. T1 T2 T3 T2 T3 P Overheads for Computers as Components
Round-robin assumptions • Schedule based on least common multiple (LCM) of the process periods. • Best done with equal time slots for processes. • Simple scheduler -> low scheduling overhead. • Can be implemented in hardware. Overheads for Computers as Components
Round-robin schedulability • Can bound maximum CPU load. • May leave unused CPU cycles. • Can be adapted to handle unexpected load. • Use time slots at end of period. Overheads for Computers as Components