Programmering i C++ Föreläsning 1
Download
Report
Transcript Programmering i C++ Föreläsning 1
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]
Concurrent 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 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 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 Programming
Shared memory multiprocessor
Distributed memory machine
[Andrews, 2000]
Multiprogramming on a single
Processor
a) Multiprogramming of four programs
b) Conceptual model of four independent, sequential processes
c) Only one program is active at once
[Tanenbaum 2001]
Process
A process is an executing a sequential
program
Each process has its own virtual CPU
The 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 States
Running
Process blocks
for input
Scheduler pick
another process
Scheduler picks
this process
Blocked
Ready
Input becomes
available
Processes
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.
Processer
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 Processes
A process can create a child process
The child process can create new processes
Parent / Child
– Parent responsible for the creation of Child
process
Guardian / Dependent
– The guardian process can not terminate until all
dependent processes have terminated.
Hierarchies of Processes
A process tree
– A creates two child processes, B and C
– B creates three child processes, D, E, and F
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 not
Threads
Three processes with one thread each
One process with three threads
Threads
Items shared by all threads in a process
Items private to each thread
Variations in the Process
structure
Model
– 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
Programming in OS
Two main categories:
• Pre-emptive multitasking
The OS controls which process is executing.
•
Co-operative multitasking
The current process decides if it shall stop executing.
MicroC/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-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) */
}
}