280 likes | 553 Views
Ch. 6-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
E N D
Ch. 6-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 • communication systems
Tasks: spark control crankshaft sensing fuel/air mixture oxygen sensor Kalman filter state machine Example: engine control engine controller
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
ADR r14,co2a co1a … ADR r13,co1b MOV r15,r14 co1b … ADR r13,co1c MOV r15,r14 co1c ... co2a … ADR r14,co2b MOV r15,r13 co2b … ADR r14,co2c MOV r15,r13 co2c … Co-routines Co-routine 1 Co-routine 2
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.
Processes • A process is anunique 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.
Activation record: copy of process state. Context switch: current CPU context goes out; new CPU context goes in. Processes and CPUs P1 data, code PC P2 data, code registers ... CPU P1 act. record P2 act. record memory
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.
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
fork() • A process makes a copy of itself as a child. (Both parent and child runs the same code.) • Call to fork() • The parent is returned the PID of child. • The child gets 0 childid = fork(); if (childid == 0) { /* child operations */ } else { /* parent operations */ }
execv() • Overlays child code: childid = fork(); if (childid == 0) { execv(“mychild”, childargs); perror(“execv”); exit(1); } The file with child code. If no error, never return.
Extensions childid = fork(); if (childid == 0) { /* must be the child */ execv(“mychild”, childargs); perror(“execv”); exit(1); } else { /* is the parent */ parent_stuff(); wait(&cstatus); /* return child’s status and make sure that the child’s resources are freed */ exit(0); }
Context switching • The mechanism for moving the CPU from one process to another. • Who controls when the context is switched? • How is the context switched?
Co-operative multitasking • Give up the CPU to another voluntarily. • 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.
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.
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?
Example: context switching in cooperative multitasking if (x>2) sub1(y); else sub2(y,z); cswitch(); proca(a,b,c); Process 1 proc_data(r,s,t); cswitch(); if (val1==3) abs(val2); rst(val3); Process 2 3 1 2 save_state(current); p=choose_process(); load_and_go(p); Scheduler
Process Control Block in cooperative multitasking PC Process control block (r13) CPSR r0 r1 r2 r3 r13 r14
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 Set the S bit in STM R13 is not updated without “!” Pipeline…
Buggy Cooperative Multitasking • Case 1 void process1() { if (input1 == 0) { subA(); else { subB(); switch(); } } • Case 2 void process2() { x = global1; while (x < 500) x = aproc(global2); switch(); }
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
Flow of control with preemption interrupt interrupt P1 OS P1 OS P2 time
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.
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.
Processes and UML • A process is an active object, that hasindependent thread of control. • active class? processClass1 myAttributes myOperations() start resume Signals
UML signals • Signal: object that is passed between processes for active communication: acomm: datasignal
Designing with active objects • Can mix normal and active objects: p1: processClass1 a: rawMsg w: wrapperClass ahat: fullMsg master: masterClass