s emb t11-processes

32
RMR©2012 Maths is not everything Embedded Systems 6 - Operating Systems Processes Context Switching Multitasking UML and Processes

Upload: joao-moreira

Post on 14-Jun-2015

101 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: S emb t11-processes

RMR©2012

Maths is not everything

Embedded Systems6 - Operating Systems

ProcessesContext Switching

MultitaskingUML and Processes

Page 2: S emb t11-processes

RMR©2012

Maths is not everything

Operating Systems

Processes

Page 3: S emb t11-processes

RMR©2012

Maths is not everything

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 difficult 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 difficult to achieve

The placement of code to deal with faults is more problematic

3

Page 4: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Why multiple processes?

Processes help us manage t iming complexity:

multiple rates

multimediaautomotive

asynchronous input

user interfacescommunication systems

Page 5: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Example: engine control

Tasks:spark control

crankshaft sensing

fuel/air mixture

oxygen sensor

Kalman filter

enginecontroller

Page 6: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Life without processes

Code turns into a mess:

interruptions of one task for another

spaghetti code

time

A

BCAC

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

Page 7: S emb t11-processes

RMR©2012

Maths is not everything

Process Representation

CoroutinesFork and JoinCobeginExplicit Process Declaration

7

Page 8: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Co-routines

ADR r14,co2aco1a … ADR r13,co1b MOV r15,r14co1b … ADR r13,co1c MOV r15,r14co1c ...

co2a … ADR r13,co2b MOV r15,r13co2b … ADR r13,co2c MOV r15,r13co2c …

Co-routine 1 Co-routine 2

Page 9: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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.

Page 10: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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.

Page 11: S emb t11-processes

RMR©2012

Maths is not everything

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

Long debate over whether language should define concurrency or leave it up to the O.S.

Ada and Java provide concurrency

C, C++ do not11

Page 12: S emb t11-processes

RMR©2012

Maths is not everything

Concurrent Programming Constructs

AllowThe expression of concurrent execution through the notion of process

Process synchronization

Inter-process communication.

Processes may be: independent

cooperating

competing

12

Page 13: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Processes and CPUs

Activation record: c o p y o f p r o c e s s state.Context switch:

current CPU context goes out;

new CPU context goes in. CPU

PC

registers

process 1

process 2

...

memory

Page 14: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Processes in POSIX

Create a process with fork:

parent process keeps executing old program;

child process executes new program.

process a

process a process b

Page 15: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

fork()

The fork process creates child:

childid = fork();if (childid == 0) { /* child operations */} else { /* parent operations */}

Page 16: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

execv()

Overlays child code:childid = fork();if (childid == 0) { execv(“mychild”,childargs); perror(“execv”); exit(1);}

file with child code

Page 17: S emb t11-processes

RMR©2012

Maths is not everything

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; . . Sncoend

S1, S2 etc, execute concurrentlyThe statement terminates when S1, S2 etc have terminated17

Page 18: S emb t11-processes

RMR©2012

Maths is not everything

Explicit Process Declaration

The structure of a program can be made clearer if routines state whether they will be executed concurrentlyNote that this does not say when they will execute

task body Process is

begin

. . .

end;

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

18

Page 19: S emb t11-processes

RMR©2012

Maths is not everything

Operating Systems

Context Switching

Page 20: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Context switching

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

Page 21: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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.

Page 22: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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.

Page 23: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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?

Page 24: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Context switching in ARM

Save old process:

STMIA r13,{r0-r14}^

MRS r0,SPSR

STMDB r13,{r0,r15}

Start new process:

ADR r0,NEXTPROCLDR r13,[r0]LDMDB r13,{r0,r14}MSR SPSR,r0LDMIA r13,{r0-r14}^MOVS pc,r14

Save all-reg ⟶ PD

get status

Save SPR, PC ⟶ PD

get new PD

get new context

set status

set new PC

Page 25: S emb t11-processes

RMR©2012

Maths is not everything

Operating Systems

Multitasking

Page 26: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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:

CPUti

merinterrupt

Page 27: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Flow of control with preemption

time

P1 OS P1 OS P2

interrupt interrupt

Page 28: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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.

Page 29: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

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.

Page 30: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Processes and UML

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

processClass1

myOperations()

startresume

myAttributes

Signals

Page 31: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

UML signals

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

acomm: datasignal

Page 32: S emb t11-processes

RMR©2012

Maths is not everything

© 2

008

Way

ne W

olf

Designing with active objects

Can mix normal and active objects:

p1: processClass1

master: masterClass

w: wrapperClass

a: rawMsg

ahat: fullMsg