Coroutine Flow Control

Coroutine A

Coroutine B

Coroutine C

1

resume B

2

3

5

resume A

6

6

7

resume B

8

resume C

4

9

resume A

10

11

resume c

12

12

13

resume B

14

15

Note

No return statement — only a resume statement

The value of the data local to the coroutine persit between successive calls

The execution of a coroutine is supended as control leaves it, only to carry on where it left off when it resumed

Do coroutines express true parallelism?

Fork and Join

The fork specifies that a designated routine should start executing concurrently with the invoker

Join allows the invoker to wait for the completion of the invoked routine

function F return is ...;

procedure P;

...

C:= fork F;

...

J:= join C;

...

end P;

After the fork, P and F will be executing concurrently. At the point of the join, P will wait until the F has finished (if it has not already done so)

Fork and join notation can be found in Mesa and UNIX/POSIX

UNIX Fork Example

How many processes created?

For (i=0; i != 10; i++)

pid[i] = fork();

}

wait ...

An Example

(libsdl)

int thread_1(void * parmblock)

{ while (1) ; }

int thread_2(void * parmblock)

{ while (1) ; }

void main(void)

{

SDL_init(...);

SDL_CreateThread(thread1,NULL);

SDL_CreateThread(thread2,NULL);

printf(“...”); /* hvem glemte at chekke returkoder ... */

}

Processer med info -

Ønske om at et stykke kode kan genbruges til flere parallelle opgaver (eks. En serial driver til com1 og com2,...)

I bogen er der fine ADA eksempler. Her er lidt C.

Char s1[]=com1”, s2[]=”com2”;

int rs232thread(void *p)

{

switch ( *((int *)p)) {

case 1: /* com 1 */

case 2: /* com 2 */

default: /* hvorforkommer vi her ? */

}

}

...

int i=1;

SDL_CreateThread(rs232thread,&i);

i = 2;

SDL_CreateThread(rs232thread,&i);

...

Concurrent Execution in POSIX

Provides two mechanisms: fork and pthreads.

fork creates a new process

pthreads are an extension to POSIX to allow threads to be created

All threads have attributes (e.g. stack size)

To manipulate these you use attribute objects

Threads are created using an appropriate attribute object

typedef ... pthread_t; /* details not defined */

typedef ... pthread_attr_t;

int pthread_attr_init(pthread_attr_t *attr);

int pthread_attr_destroy(pthread_attr_t *attr);

int pthread_attr_setstacksize(..);

int pthread_attr_getstacksize(..);

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine)(void *), void *arg);

/* create thread and call the start_routine with the argument */

int pthread_join(pthread_t thread, void **value_ptr);

int pthread_exit(void *value_ptr);

/* terminate the calling thread and make the pointer value_ptr

available to any joining thread */

pthread_t pthread_self(void);

All functions returns 0 if successful,

otherwise an error number

Typical C POSIX interface

T

S

P

A Simple Embedded System

Overall objective is to keep the temperature and pressure of some chemical process within well-defined limits

Switch

ADC

ADC

DAC

Screen

Heater

Thermocouples

Pressure

Transducer

Pump/Valve

Disadvantages

Both tasks send data to the screen, but the screen is a resource that can only sensibly be accessed by one process at a time

A third entity is required. This has transposed the problem from that of concurrent access to a non-concurrent resource to one of resource control

It is necessary for controller tasks to pass data to the screen resource

The screen must ensure mutual exclusion

The whole approach requires a run-time support system

OS versus Language Concurrency

Should concurrency be in a language or in the OS?

Arguments for concurrency in the languages:

It leads to more readable and maintainable programs

There are many different types of OSs; the language approach makes the program more portable

An embedded computer may not have any resident OS

Arguments against concurrency in a language:

It is easier to compose programs from different languages if they all use the same OS model

It may be difficult to implement a language's model of concurrency efficiently on top of an OS’s model

OS standards are beginning to emerge

The Ada/Java philosophy is that the advantages outweigh the disadvantages

Summary of Concurrent Programming

The application domains of most real-time systems are inherently parallel

The inclusion of the notion of process within a real-time programming language makes an enormous difference to the expressive power and ease of use of the language

Without concurrency the software must be constructed as a single control loop

The structure of this loop cannot retain the logical distinction between systems components. It is particularly difficult to give process-oriented timing and reliability requirements without the notion of a process being visible in the code

Summary Continued

The use of a concurrent programming language is not without its costs. In particular, it becomes necessary to use a run-time support system to manage the execution of the system processes

The behaviour of a process is best described in terms of states

non-existing

created

initialized

executable

waiting dependent termination

waiting child initialization

terminated

Variations in the Process Model

structure

static, dynamic

level

top level processes only (flat)

multilevel (nested)

initialization

with or without parameter passing

granularity

fine or coarse grain

termination

natural, suicide

abortion, untrapped error

never, when no longer needed

representation

coroutines, fork/join, cobegin, explicit process declarations

Ada, Java and C/POSIX

Ada and Java provide a dynamic model with support for nested tasks and a range of termination options.

POSIX allows dynamic threads to be created with a flat structure; threads must explicitly terminate or be killed.

Characteristics of RTS

andy

2000-10-11T16:20:45