in Operating System edited by
4,691 views
33 votes
33 votes
The concurrent programming constructs fork and join are as below:

Fork <label> which creates a new process executing from the specified label

Join <variable> which decrements the specified synchronization variable (by $1$) and terminates the process if the new value is not $0$.  

Show the precedence graph for $S1$, $S2$, $S3$, $S4$, and $S5$ of the concurrent program below.

$N=2$
$M=2$
Fork $L3$
Fork $L4$
$S1$
$L1$ : join $N$
$S3$
$L2$ : join $M$
$S5$
$L3:S2$
Goto $L1$
$L4:S4$
Goto $L2$
Next:
in Operating System edited by
4.7k views

3 Comments

when the first fork call two parallel process be executed simultaneously one is S2 (specified by label)

but i have a doubt in next instruction of first fork call 

@Bikram sir for this is second fork ,label will be executed in parallel of S2?

http://uclab.khu.ac.kr/lectures/2003_autumn_os/lecture3.pdf

how can draw precedence graph for two fork simultaneously?

1
1

set2018

See the edited answer now .

1
1
s5 here means next ?

and also unable to understand how the s1 s2 combined
0
0

1 Answer

41 votes
41 votes
Best answer

$S1, S2, S3, S4$ and $S5$ are the statements to be executed.

Fork() creates a child to execute in parallel.

There will be $3$ processes running concurrently. 
One will execute S1, $2^{nd}$ will execute $S2$ and $3^{rd}$ will execute $S4$.

Initially there is one process which started execution. Suppose this process name is $P_0.$

It executes $N=2$, $M=2$ and after that it executes

Fork: $L3$,
At $L3$ these is a statement $S2$, Fork creates a new process suppose $P_1$ which starts its execution from level $L3$, means it starts executing $S2$.

$P_0$ executes fork $L4$, it creates another new process $P_2$ which starts its execution from level $L4$ means it starts executing $S4$.

When $P_1$ finishes executing $S2$, it executes next line which is goto $L1.$
When $P_2$ finishes executing $S4$, it executes next line which is goto $L2.$

$L1$ is executed by both processes $P_0($ which has executed S1$)$  and $P_1($ which has executed S2$)$
Hence, S1 and S2 are combined together, as either $P_0$ or $P_1$ will terminate $(\because N = 2)$ and only one process will continue its execution.

Similarly $L2$ is executed by two processes $P_2($ which executed S4) and one of $P_0$ or $P_1($ which executed S3$).$ So, $S4$ and S3 are joined together, as one of them will terminate $(\because M = 2)$ and then one which survives will execute the final statement $S5$.

www.csc.lsu.edu/~rkannan/Fork_Cobegin_Creationtime.docx 
http://www.cis.temple.edu/~giorgio/old/cis307s96/readings/precedence.html

edited by

4 Comments

this image can give some more  details :

1
1

hat tells process to terminate after S5? Why doesn't the process continue its execution of statements in L3?

there should be GOTO next ; after S5 and before L3

3
3
how can the child processes created by fork() use the same shared variable N and M….
0
0

Related questions