Title: Concurrency
1Concurrency
A _______ is a unit of software that is capable
of executing concurrently with other similar
units.
Synonyms
_______ -- user-defined process in Ada
_________ -- light weight process
____________ -- user processes for O.S.
interaction
2Process Operations
_______ a process
____________ a process
__________ a process
____________ a process
Example a terminal application shell (OS X)
How do you...
1) Create a shell?
2) Start the execution of a shell?
3) Suspend a shell?
4) Destroy a shell?
3Example 2 Unix commands in a C-shell
What suffix is used to cause a Unix command to
run in the background?
What command suspends destroys a running
process?
Example 3 Dijkstras parbegin - parend
parbegin process1 process2
process3 ? ? ? processn parend
4Example 4 Unix script programs
? A fork function creates a child process and
that shares code with the parent.
? Both the child and its parent continue
executing immediately following the fork.
? The fork function returns the child process ID
to its parent and 0 to the child.
? Suspending and resuming process execution is
determined by the O.S.
? Children processes share resources (files,
etc.) with their parent.
? Processes are killed at the end of their code.
? A wait function can be called to suspend a
parent awaiting a child.
sample
childProcessNum fork() if (childProcessNum0)
// child process code goes here else
// parent process code goes here ? ? ? do
finiNum wait(status) while
(finiNum!childProcessNum)
5Example 5 C/C Threads
C and C require the use of OS-specific
libraries.
Linux - C
include ltpthread.hgt include ltstdio.hgt void
thread1code(void arg) //code for Thread
1 goes here return arg void
thread2code(void arg) //code for Thread
2 goes here return arg int main()
int errcode pthread_t thread1, thread2
/ holds thread info / int status /
holds return code / if (errcodepthread_cre
ate(thread1, NULL, thread1code, NULL))
printf("Thread 1 creation failed\n") if
(errcodepthread_create(thread2, NULL,
thread2code, NULL)) printf("Thread 2
creation failed\n") // code for the main
thread goes here pthread_join(thread1,(
void ) status) pthread_join(thread2,(voi
d ) status) exit(0)
6Example C/C Threads
Windows - C
include "stdafx.h include ltwindows.hgt using
namespace DWORD WINAPI thread_code_a(void junk)
// code for Thread a goes here return
0 DWORD WINAPI thread_code_b(void junk)
// code for Thread b goes here return
0 int main() DWORD thread_a, thread_b
CreateThread(NULL, 0, thread_code_b, NULL, 0,
thread_b) CreateThread(NULL, 0,
thread_code_a, NULL, 0, thread_a) // more
main thread code goes here return 0
7Example 6 Ada Tasks
? An Ada task is created and begins execution
when its enclosing program unit executes.
? A program terminates only when all its tasks
terminate.
? Each task has a single line of control.
sample
with text_io use text_io procedure Write_em
is task WriteAs task body WriteAs
is begin for j in 1..10
loop put('A') new_line
end loop end WriteAs task
WriteBs task body WriteBs is
begin for j in 1..10 loop
put('B') new_line end
loop end WriteBs begin --
Write_em procedure null -- procedure body
must be have least one instruction end
Write_em