Open In App

Fork and Join Constructs in Concurrency

Last Updated : 06 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite – Process Synchronization
Fork :
The fork instruction is the that instruction in the process execution that produces two concurrent executions in a program. One of the concurrent executions starts at statement labeled and other execution is the continuation of the execution at the statement following the fork instruction. The fork systems call assignment has one parameter i.e. Label (L).

Join :
The join instruction is the that instruction in the process execution that provides the medium to recombine two concurrent computations into a single one. The join instruction has one parameter integer count that specifies the number of computations which are to be joined. It decrements the integer by one. If the value of the integer after decrement is non-zero then the process terminates otherwise the process continues execution with the next statement.

Example-1:
Construct the precedence graph for the following fork/join program.

     S1;
     count1: = 2;
     fork L1;
     S2;
     S4;
     count2: = 2;
     fork L2;
     S5;
     Go to L3;
L1:  S3;
L2:  join count1;
     S6;
L3:  join count2;
     S7;

Solution :

Example-2 :
Write a fork/join program for the following precedence graph.

Solution :
After S1 fork statement is required to create a child.
After S4 fork statement is required to create a child.
All processes need to join before S7.

     S1;
     count: = 3;
     fork L1;
     S2;
     S4;
     fork L2;
     S5;
     Go to L3;
L2:  S6;
     GOTO L3;
L1:  S3;
L3:  join count;
     S7; 

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads