Upload
ophelia-gibbs
View
213
Download
0
Tags:
Embed Size (px)
Citation preview
Real-Time SystemsReal-Time SystemsLecture 3Lecture 3
Teachers: Olle BowalliusPhone: 790 44 42Email: [email protected]
Anders VästbergPhone: 790 44 55Email: [email protected]
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
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
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
Concurrent ProgrammingConcurrent Programming
Shared memory multiprocessor
Distributed memory machine
[Andrews, 2000]
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]
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.
Process StatesProcess States
Running
Blocked Ready
Process blocks for input
Scheduler pick another process
Input becomesavailable
Scheduler picks this process
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.
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
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.
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
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
ThreadsThreads
Three processes with one thread eachOne process with three threads
ThreadsThreads
Items shared by all threads in a processItems private to each thread
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
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.
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) */}
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) */ }}