Title: Introduction to Concurrency
1Introduction to Concurrency
ConcurrencyExecute two or more pieces of code
at the same time Why?No choice-
Geographically distributed data-
Interoperability of different machines- A piece
of code mustserve many other client processes-
To achieve reliability By Choice to achieve
speedup sometimes makes programming easier (e.g.
UNIX pipes)
2Possibilities for Concurrency
Architecture
Architecture
- Uniprocessor with
- I/O channel
- I/o processor
- DMA
Multiprogramming, multiple process system
Programs
Network of uniprocessors
Distributed programming
Multiple CPUs
Parallel programming
3Definitions
Concurrent process execution can be-
interleaved,or- physically simultaneousInterlea
ved Multiprogramming on uniprocessor Physically
simultaneous Uni-or multiprogramming on
multiprocessor Process, thread, or
task Schedulable unit of computation Granularity
Process size or computation to communication
ratio- Too small excessive overhead- Too
large less concurrency
4Precedence Graph
Consider writing a program as a set of
tasks. Precedence graph Specifies execution
ordering among tasks
S0 A X Y S1 B X Y S2 C Z 1 S3 C
A - B S4 W C 1
S0
S2
S1
S3
S4
Parallel zing compilers for computers with vector
processors build dependency graphs
5Cyclic Precedence Graphs
What does the following graph represent ?
S1
S2
S3
6Examples of Concurrency in Uniprocessors
- Example 1 Unix pipes
- Motivations
- fast to write code
- Fast to execute
- Example2 Buffering
- Motivation
- required when two asynchronous processes must
communicate - Example3 Client/ Server model
- Motivation
- - geographically distributed computing
7Operating System Issues
SynchronizationWhat primitives should OS
provide? CommunicationWhat primitives should OS
provide to interface communication
protocol? Hardware supportNeeded to implement
OS primitives Remote executionWhat primitives
should OS provide?- Remote procedure call(RPC)-
Remote command shell Sharing address
spacesMakes programmer easier Lightweight
threadsCan a process creation be as cheap as a
procedure call?
8Parallel Language Constructs
FORK and JOIN FORK LStarts parallel execution at
the statement labeled L and at the statement
following the fork JOIN CountRecombines Count
concurrent computations CountCount-1 If (Co
untgt0) Then Quit Join is an atomic operation
9Definition Atomic Operation
- If I am a process executing on a processor, and I
execute an atomic operation, then all other
processes executing on this or any other
processor - Can see state of system before I execute or after
I execute - but cannot see any intermediate state while I am
executing - Example bank teller/ Joe has 1000, split
equally between savings and checking accounts/ - Subtract 100 from Joes savings account
- Add 100 to Joes checking account
- Other processes should never read Joes balances
and find he has 900 in both accounts.
10Concurrency Conditions
Let Si denote a statement Read set of Si R(Si)
a1,a2,,an) Set of all variables referenced
in Si Write set of Si W(Si) b1, b2, ,
bm, Set of all variables changed by si C A
- B R(C A - B) A , B W(C A - B)
C Scanf(d , A) R(scanf(d ,
A)) W(scanf(d , A))A
11Bernsteins Conditions
- The following conditions must hold for two
statements S1 and S2 to execute concurrently with
valid results - R(S1) INTERSECT W(S2)
- W(S1) INTERSECT R(S2)
- W(S1) INTERSECT W(S2)
- These are called the Berstein Conditions.
12Fork and Join Example 1
S1
S2
S1 A X Y S2 B Z 1 S3 C A -
B S4 W C 1
S3
Count 2 FORK L1 A X Y Goto
L2 L1 B Z 1 L2 JOIN Count C A -
B W C 1
S4
13Structured Parallel Constructs
PARBEGIN / PAREND
PARBEGIN Sequential execution splits off into
several concurrent sequences PAREND Parallel
computations merge
PARBEGIN Statement 1 Statement
2 Statement Nl PAREND
PARBEGIN Q C mod 25 Begin N N - 1 T
N / 5 End Proc1(X , Y) PAREND
14Fork and Join Example 2
S1
S1 Count 3 FORK L1 S2 S4 FORK
L2 S5 Goto L3 L2 S6 Goto L3 L1 S3 L3
JOIN Count S7
S2
S3
S4
S6
S5
S7
Up to three tasks may concurrently execute
15Parbegin / Parend Examples
Begin PARBEGIN A X Y B Z 1
PAREND C A - B W C 1 End
Begin S1 PARBEGIN S3 BEGIN
S2 S4 PARBEGIN S5 S6
PAREND End PAREND S7 End
16Comparison
Unfortunately, the structured concurrent
statement is not powerful enough to model all
precedence graphs.
S1
S1
S1
S1
S1
S1
S1
17Comparison(contd)
Fork and Join code for the modified precedence
graph
S1 Count 12 FORK L1 S2 S4 Count22
FORK L2 S5 Goto L3 L1 S3 L2 JOIN
Count1 S6 L3 JOIN Count2 S7
18Comparison(contd)
There is no corresponding structured construct
code for the same graph However, other
synchronization techniques can supplement Also,
not all graphs need implementing for real-world
problems
19Overview
System Calls - fork( ) - wait( ) - pipe( ) -
write( ) - read( ) Examples
20Process Creation
Fork( ) NAME fork() create a new
process SYNOPSIS include ltsys/types.hgt
include ltunistd.hgt pid_t fork(void) RETURN
VALUE success parent- child pid child-
0 failure -1
21Fork() system call- example
include ltsys/types.hgt include
ltunistd.hgt include ltstdio.hgt Main() printf(
ld parent process id ld\n, getpid(),
getppid()) fork() printf(\nld parent
process id ld\n, getpid(), getppid())
22Fork() system call- example
17619 parent process id 12729 17619 parent
process id 12729 2372 parent process id
17619
23Fork()- program structure
include ltsys/types.hgt include
ltunistd.hgt include ltstdio.hgt Main() pid_t
pid if((pid fork())gt0) / parent
/ else if ((pid0) /child/ else
/ cannot fork exit(0)
24Wait() system call
Wait()- wait for the process whose pid reference
is passed to finish executing SYNOPSIS includelts
ys/types.hgt includeltsys/wait.hgt pid_t wait(int
stat)loc) The unsigned decimal integer process
ID for which to wait RETURN VALUE success-
child pid failure- -1 and errno is set
25Wait()- program structure
include ltsys/types.hgt include
ltunistd.hgtinclude ltstdlib.hgt include
ltstdio.hgt Main(int argc, char argv) pid_t
childPID if((childPID fork())0) /child/
else / parent wait(0) exit(0)
26Pipe() system call
Pipe()- to create a read-write pipe that may
later be used to communicate with a process
well fork off. SYNOPSIS Int pipe(pfd) int
pfd2 PARAMETER Pfd is an array of 2 integers,
which that will be used to save the two file
descriptors used to access the pipeRETURN
VALUE0 success-1 error.
27Pipe() - structure
/ first, define an array to store the two file
descriptors/Int pipe2/ now, create the
pipe/int rc pipe (pipes) if(rc -1)
/ pipe() failed/ Perror(pipe) exit(1)
If the call to pipe() succeeded, a pipe will be
created, pipes0 will contain the number of its
read file descriptor, and pipes1 will contain
the number of its write file descriptor.
28Write() system call
Write() used to write data to a file or other
object identified by a file descriptor. SYNOPSIS
include ltsys/types.hgt Size_t write(int fildes,
const void buf, size_t nbyte) PARAMETER fildes
is the file descriptor, buf is the base address
of area of memory that data is copied
from, nbyte is the amount of data to
copy RETURN VALUE The return value is the actual
amount of data written, if this differs from
nbyte then something has gone wrong
29Read() system call
Read() read data from a file or other object
identified by a file descriptor SYNOPSIS include
ltsys/types.hgt Size_t read(int fildes, void buf,
size_t nbyte) ARGUMENT fildes is the file
descriptor, buf is the base address of the
memory area into which the data is read, nbyte
is the maximum amount of data to read. RETURN
VALUE The actual amount of data read from the
file. The pointer is incremented by the amount of
data read.