Processes and operating systems z Motivation for processes

  • Slides: 24
Download presentation
Processes and operating systems z. Motivation for processes. z. The process abstraction. z. Context

Processes and operating systems z. Motivation for processes. z. The process abstraction. z. Context switching. z. Multitasking. z. Processes and UML. © 2000 Morgan Kaufman Overheads for Computers as Components

Why multiple processes? z. Processes help us manage timing complexity: ymultiple rates xmultimedia xautomotive

Why multiple processes? z. Processes help us manage timing complexity: ymultiple rates xmultimedia xautomotive yasynchronous input xuser interfaces xcommunication systems © 2000 Morgan Kaufman Overheads for Computers as Components

Example: engine control z Tasks: yspark control ycrankshaft sensing yfuel/air mixture yoxygen sensor y.

Example: engine control z Tasks: yspark control ycrankshaft sensing yfuel/air mixture yoxygen sensor y. Kalman filter ystate machine © 2000 Morgan Kaufman Overheads for Computers as Components engine controller

Life without processes z Code turns into a mess: yinterruptions of one task for

Life without processes z Code turns into a mess: yinterruptions of one task for another yspaghetti code time A B C A C © 2000 Morgan Kaufman Overheads for Computers as Components A_code(); … B_code(); … if (C) C_code(); … A_code(); … switch (x) { case C: C(); case D: D(); . . .

Co-routines Co-routine 1 ADR r 14, co 2 a co 1 a … ADR

Co-routines Co-routine 1 ADR r 14, co 2 a co 1 a … ADR r 13, co 1 b MOV r 15, r 14 co 1 b … ADR r 13, co 1 c MOV r 15, r 14 co 1 c. . . © 2000 Morgan Kaufman Co-routine 2 co 2 a … ADR r 13, co 2 b MOV r 15, r 13 co 2 b … ADR r 13, co 2 c MOV r 15, r 13 co 2 c … Overheads for Computers as Components

Co-routine methodology z. Like subroutine, but caller determines the return address. z. Co-routines voluntarily

Co-routine methodology z. Like subroutine, but caller determines the return address. z. Co-routines voluntarily give up control to other co-routines. z. Pattern of control transfers is embedded in the code. © 2000 Morgan Kaufman Overheads for Computers as Components

Processes z. A process is a unique execution of a program. y. Several copies

Processes z. A process is a unique execution of a program. y. Several copies of a program may run simultaneously or at different times. z. A process has its own state: yregisters; ymemory. z. The operating system manages processes. © 2000 Morgan Kaufman Overheads for Computers as Components

Processes and CPUs z Activation record: copy of process state. z Context switch: ycurrent

Processes and CPUs z Activation record: copy of process state. z Context switch: ycurrent CPU context goes out; ynew CPU context goes in. process 1 process 2. . . memory © 2000 Morgan Kaufman Overheads for Computers as Components PC registers CPU

Terms z. Thread = lightweight process: a process that shares memory space with other

Terms z. Thread = lightweight process: a process that shares memory space with other processes. z. Reentrancy: ability of a program to be executed several times with the same results. © 2000 Morgan Kaufman Overheads for Computers as Components

Processes in POSIX z Create a process with fork: yparent process keeps executing old

Processes in POSIX z Create a process with fork: yparent process keeps executing old program; ychild process executes new program. © 2000 Morgan Kaufman process a Overheads for Computers as Components process b

fork() z. The fork process creates child: childid = fork(); if (childid == 0)

fork() z. The fork process creates child: childid = fork(); if (childid == 0) { /* child operations */ } else { /* parent operations */ } © 2000 Morgan Kaufman Overheads for Computers as Components

execv() z. Overlays child code: childid = fork(); if (childid == 0) { execv(“mychild”,

execv() z. Overlays child code: childid = fork(); if (childid == 0) { execv(“mychild”, childargs); perror(“execv”); exit(1); } © 2000 Morgan Kaufman file with child code Overheads for Computers as Components

Context switching z. Who controls when the context is switched? z. How is the

Context switching z. Who controls when the context is switched? z. How is the context switched? © 2000 Morgan Kaufman Overheads for Computers as Components

Co-operative multitasking z. Improvement on co-routines: yhides context switching mechanism; ystill relies on processes

Co-operative multitasking z. Improvement on co-routines: yhides context switching mechanism; ystill relies on processes to give up CPU. z. Each process allows a context switch at cswitch() call. z. Separate scheduler chooses which process runs next. © 2000 Morgan Kaufman Overheads for Computers as Components

Problems with co-operative multitasking z. Programming errors can keep other processes out: yprocess never

Problems with co-operative multitasking z. Programming errors can keep other processes out: yprocess never gives up CPU; yprocess waits too long to switch, missing input. © 2000 Morgan Kaufman Overheads for Computers as Components

Context switching z. Must copy all registers to activation record, keeping proper return value

Context switching z. Must copy all registers to activation record, keeping proper return value for PC. z. Must copy new activation record into CPU state. z. How does the program that copies the context keep its own context? © 2000 Morgan Kaufman Overheads for Computers as Components

Context switching in ARM z Save old process: z Start new process: STMIA r

Context switching in ARM z Save old process: z Start new process: STMIA r 13, {r 0 -r 14}^ MRS r 0, SPSR STMDB r 13, {r 0, r 15} ADR r 0, NEXTPROC LDR r 13, [r 0] LDMDB r 13, {r 0, r 14} MSR SPSR, r 0 LDMIA r 13, {r 0 -r 14}^ MOVS pc, r 14 © 2000 Morgan Kaufman Overheads for Computers as Components

Preemptive multitasking z. Most powerful form of multitasking: y. OS controls when contexts switches;

Preemptive multitasking z. Most powerful form of multitasking: y. OS controls when contexts switches; y. OS determines what process runs next. z. Use timer to call OS, switch contexts: CPU © 2000 Morgan Kaufman timer interrupt Overheads for Computers as Components

Flow of control with preemption interrupt P 1 OS P 2 time © 2000

Flow of control with preemption interrupt P 1 OS P 2 time © 2000 Morgan Kaufman Overheads for Computers as Components

Preemptive context switching z. Timer interrupt gives control to OS, which saves interrupted process’s

Preemptive context switching z. Timer interrupt gives control to OS, which saves interrupted process’s state in an activation record. z. OS chooses next process to run. z. OS installs desired activation record as current CPU state. © 2000 Morgan Kaufman Overheads for Computers as Components

Why not use interrupts? z. We could change the interrupt vector at every period,

Why not use interrupts? z. We could change the interrupt vector at every period, but: ywe would need management code anyway; ywe would have to know the next period’s process at the start of the current process. © 2000 Morgan Kaufman Overheads for Computers as Components

Processes and UML z. A process is an active class---independent thread of control. process.

Processes and UML z. A process is an active class---independent thread of control. process. Class 1 my. Attributes my. Operations() Signals start resume © 2000 Morgan Kaufman Overheads for Computers as Components

UML signals z. Signal: object that is passed between processes for active communication: acomm:

UML signals z. Signal: object that is passed between processes for active communication: acomm: datasignal © 2000 Morgan Kaufman Overheads for Computers as Components

Designing with active objects z. Can mix normal and active objects: p 1: process.

Designing with active objects z. Can mix normal and active objects: p 1: process. Class 1 a: raw. Msg w: wrapper. Class ahat: full. Msg master: master. Class © 2000 Morgan Kaufman Overheads for Computers as Components