19
Real-Time Systems Real-Time Systems Lecture 3 Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: [email protected] Anders Västberg Phone: 790 44 55 Email: [email protected]

Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: [email protected] Anders Västberg Phone: 790 44 55 Email: [email protected]

Embed Size (px)

Citation preview

Page 1: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Real-Time SystemsReal-Time SystemsLecture 3Lecture 3

Teachers: Olle BowalliusPhone: 790 44 42Email: [email protected]

Anders VästbergPhone: 790 44 55Email: [email protected]

Page 2: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Concurrent ProgrammingConcurrent Programming

Sequential Program– Sequence of actions that produce a result– Is called a process, task or thread

Concurrent Program– Two or more processes that work together– Need synchronisation and communication

Page 3: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Concurrent ProgrammingConcurrent Programming

Constructs needed:– Notion of processes to express concurrent

execution– Process synchronization– Communication between processes

Processes communicate via shared memory and/or by message passing

Page 4: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Concurrent ProgrammingConcurrent Programming

The actual implementation (i.e. execution) of a collection of processes usually takes one of three forms.

Single processor– processes multiplex their executions on a single

processor Multiprocessor

– processes multiplex their executions on a multiprocessor system where there is access to shared memory

Multicomputer (Distributed System)– processes multiplex their executions on several

processors which do not share memory

Page 5: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Concurrent ProgrammingConcurrent Programming

Shared memory multiprocessor

Distributed memory machine

[Andrews, 2000]

Page 6: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Multiprogramming on a single Multiprogramming on a single ProcessorProcessor

a) Multiprogramming of four programsb) Conceptual model of four independent, sequential processesc) Only one program is active at once

[Tanenbaum 2001]

Page 7: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

ProcessProcess

A process is an executing a sequential program

Each process has its own virtual CPUThe context of each process.

– Each record is called a Process Control Block or a Task Control Block

– Contents: Contents in registers, pc, psw, sp, priority, id, other states.

Page 8: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Process StatesProcess States

Running

Blocked Ready

Process blocks for input

Scheduler pick another process

Input becomesavailable

Scheduler picks this process

Page 9: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

ProcessesProcesses

Processes can be – Independent

Not synchronized or communicating

– Cooperating Synchronized and communicating

– Competing Peripheral devices, memory, and processor power Must communicate to fairly share resources.

Page 10: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

ProcesserProcesser Create processes:

– Initialize the system– A process creates a child process– The user creates a new process– Run a batch job

The process is either initialized by passing parameters to it or by explicitly communicating to with the process after its created

Process termination– Completion– Suicide– Abortion by other process– Untrapped error condition– Never– When no longer needed

Page 11: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Hierarchies of ProcessesHierarchies of Processes

A process can create a child processThe child process can create new processesParent / Child

– Parent responsible for the creation of Child process

Guardian / Dependent– The guardian process can not terminate until all

dependent processes have terminated.

Page 12: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

A process treeA process tree– A creates two child processes, B and CA creates two child processes, B and C– B creates three child processes, D, E, and FB creates three child processes, D, E, and F

Hierarchies of ProcessesHierarchies of Processes

Page 13: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Processes and ThreadsProcesses 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 not

Page 14: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

ThreadsThreads

Three processes with one thread eachOne process with three threads

Page 15: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

ThreadsThreads

Items shared by all threads in a processItems private to each thread

Page 16: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Variations in the Process Variations in the Process ModelModel 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

Page 17: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

Support for Concurrent Support for Concurrent Programming in OSProgramming in OS

Two main categories:• Pre-emptive multitasking

The OS controls which process is executing.

• Co-operative multitaskingThe current process decides if it shall stop executing.

Page 18: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

MicroC/OS-IIMicroC/OS-II

int main(int argc, char *argv[]){ INT8U err;

OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel" */

OSTaskCreate(AppStartTask, (void *) 0, (OS_STK*) &AppStartTaskStk[TASK_STK_SIZE-1], TASK_START_PRIO); OSStart(); /* Start multitasking (i.e. give control to uC/OS-II) */}

Page 19: Real-Time Systems Lecture 3 Teachers: Olle Bowallius Phone: 790 44 42 Email: olleb@isk.kth.se Anders Västberg Phone: 790 44 55 Email: vastberg@kth.se

MicroC/OS-IIMicroC/OS-II

void AppStartTask (void *p_arg){ p_arg = p_arg; while (TRUE) /* Task body, always written as an infinite loop. */ { OS_Printf("Delay 1 second and print\n"); OSTimeDlyHMSM(0, 0, 1, 0); /* OSTimeDly(1000) */ }}