The fork-join is a basic method of expressing concurrency within a computation. This is implemented in the UNIX operating system as system calls.
The fork() call creates a new child process which runs concurrently with the parent. The child process is an exact copy of the parent except that it has a new process id. The fork() creates concurrency.
The join() is called by both the parent and the child. The child process calls join() after it has finished execution. This operation is done implicitly. The parent process waits until the child joins and continues later.
The join() call breaks the concurrency because the child process exits. The join() inform the parent that child operation is finished.
There are two join scenarios:
- The child joins first and then the parent joins without waiting for any process.
- The parent process joins first and wait, the child process joins, and the parent continues thereafter.
The parent executes “fork L” statement. It creates a child process which runs concurrently- one process (parent) continues after the fork operation and other (child) statement runs at statement labeled L.
S1; fork L; S2 /* Statement right after fork operation */ ... ... L: S3 /* statement labeled L */
The fork splits the single operation into two independent operation – S2 and S3.
The join call combines the two concurrent process into one. The process which executes join first will be terminated and the other process continues. If it is parent S2 that terminates first, then it will wait for S3 to finish.
If S3 join first, then S2 will join later after finishing the execution without waiting.