Sei sulla pagina 1di 32

Maths is not everything

Embedded Systems
6 - Operating Systems

Processes Context Switching Multitasking


RMR2012

UML and Processes

Maths is not everything

Operating Systems Processes

RMR2012

Why we need it?


The alternative is to use sequential programming techniques The programmer must construct the system so that it involves the cyclic execution of a program sequence to handle the various concurrent activities This complicates the programmer's already difcult task and involves him/her in considerations of structures which are irrelevant to the control of the activities in hand The resulting programs will be more obscure and inelegant It makes decomposition of the problem more complex Parallel execution of the program on more than one processor will be much more difcult to achieve
Maths is not everything

The placement of code to deal with faults is more problematic

RMR2012

Why multiple processes?

Processes help us manage timing complexity:


multiple rates
multimedia automotive

asynchronous input
Maths is not everything

2008 Wayne Wolf

user interfaces communication systems

RMR2012

Example: engine control

Tasks:
spark control crankshaft sensing fuel/air mixture oxygen sensor Kalman lter
Maths is not everything

engine controller

RMR2012

2008 Wayne Wolf

Life without processes

Code turns into a mess:


interruptions of one task for another spaghetti code

time

A B C A

Maths is not everything

RMR2012

2008 Wayne Wolf

A_code(); B_code(); if (C) C_code(); A_code(); switch (x) { case C: C(); case D: D(); ...

Process Representation

Coroutines Fork and Join Cobegin Explicit Process Declaration


Maths is not everything

RMR2012

Co-routines

Co-routine 1 ADR r14,co2a


co1a ADR r13,co1b MOV r15,r14 co1b ADR r13,co1c
Maths is not everything

Co-routine 2 co2a ADR r13,co2b MOV r15,r13 co2b ADR r13,co2c MOV r15,r13 co2c

2008 Wayne Wolf

MOV r15,r14 co1c ...

RMR2012

Co-routine methodology

Like subroutine, but caller determines the return address. Co-routines voluntarily give up control to other co-routines.
2008 Wayne Wolf

Maths is not everything

Pattern of control transfers is embedded in the code.

RMR2012

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.
Maths is not everything

RMR2012

2008 Wayne Wolf

The operating system manages processes.

Processes and Threads


All operating systems provide processes Processes execute in their own virtual machine (VM) to avoid interference from other processes Recent OSs provide mechanisms for creating threads within the same virtual machine; threads are sometimes provided transparently to the OS Threads have unrestricted access to their VM The programmer and the language must provide the protection from interference
Maths is not everything

Long debate over whether language should define concurrency or leave it up to the O.S.
Ada and Java provide concurrency C, C++ do not

RMR2012

11

Concurrent Programming Constructs

Allow
The expression of concurrent execution through the notion of process Process synchronization Inter-process communication.

Processes may be:


independent
Maths is not everything

cooperating competing

RMR2012

12

Processes and CPUs

Activation record: copy of process state. Context switch:


current CPU context goes out; new CPU context goes in.
2008 Wayne Wolf

process 1 process 2

PC

registers

...
memory

CPU

Maths is not everything

RMR2012

Processes in POSIX

Create a process with fork:


parent process keeps executing old program; child process executes new program.
process a
Maths is not everything

process a

process b

RMR2012

2008 Wayne Wolf

fork()

The fork process creates child:


childid = fork(); if (childid == 0) { /* child operations */
2008 Wayne Wolf

} else { /* parent operations */ }

Maths is not everything

RMR2012

execv()

Overlays child code:


childid = fork(); if (childid == 0) { execv(mychild,childargs); perror(execv);
Maths is not everything

le with child code

2008 Wayne Wolf

exit(1); }

RMR2012

Cobegin

The cobegin (or parbegin or par) is a structured way of denoting the concurrent execution of a collection of statements:
cobegin S1; S2; S3; . . Sn coend
Maths is not everything

S1, S2 etc, execute concurrently The statement terminates when S1, S2 etc have terminated

RMR2012

17

Explicit Process Declaration The structure of a program can be made clearer if routines state whether they will be executed concurrently Note that this does not say when they will execute
task body Process is begin . . .
Maths is not everything

end;

RMR2012

Languages that support explicit process declaration may have explicit or implicit process/task creation

18

Maths is not everything

Operating Systems Context Switching

RMR2012

Context switching

Who controls when the context is switched? How is the context switched?

Maths is not everything

RMR2012

2008 Wayne Wolf

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.


Maths is not everything

RMR2012

2008 Wayne Wolf

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.

Maths is not everything

RMR2012

2008 Wayne Wolf

Context switching

Must copy all registers to activation record, keeping proper return value for PC. Must copy new activation record into CPU state.
Maths is not everything

RMR2012

2008 Wayne Wolf

How does the program that copies the context keep its own context?

Context switching in ARM

Save old process:


Save all-reg ! PD

Start new process:


ADR r0,NEXTPROC get new PD LDR r13,[r0] LDMDB r13,{r0,r14} set status MSR SPSR,r0 LDMIA r13,{r0-r14}^ get new context MOVS pc,r14
set new PC

STMIA r13,{r0-r14}^ MRS r0,SPSR


Save SPR, PC ! PD get status

STMDB r13,{r0,r15}

Maths is not everything

RMR2012

2008 Wayne Wolf

Maths is not everything

Operating Systems Multitasking

RMR2012

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: CPU timer


interrupt

Maths is not everything

RMR2012

2008 Wayne Wolf

Flow of control with preemption

interrupt

interrupt

P1

OS

P1

OS

P2

time
Maths is not everything

RMR2012

2008 Wayne Wolf

Preemptive context switching

Timer interrupt gives control to OS, which saves interrupted processs state in an activation record. OS chooses next process to run.
2008 Wayne Wolf

Maths is not everything

OS installs desired activation record as current CPU state.

RMR2012

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 periods process at the start of the current process.
2008 Wayne Wolf

Maths is not everything

RMR2012

Processes and UML

A process is an active class---independent thread of control.


processClass1 myAttributes myOperations() Signals
Maths is not everything

RMR2012

2008 Wayne Wolf

start resume

UML signals

Signal: object that is passed between processes for active communication:


acomm: datasignal

Maths is not everything

RMR2012

2008 Wayne Wolf

Designing with active objects

Can mix normal and active objects:


p1: processClass1
a: rawMsg

w: wrapperClass
2008 Wayne Wolf

ahat: fullMsg

Maths is not everything

master: masterClass

RMR2012

Potrebbero piacerti anche