Fork/join blocks provide the primary mechanism for creating concurrent processes.The all | any | none options specify when the code after the fork/join block executes.The default is all.
all :: The default is all. Code after the block executes after all of the concurrent processes are completed.
any :: When any is used, code after the block executes after any single concurrent process is completed.
none :: When none is used, code after the block executes immediately, without waiting for any of the processes to complete.
BEFORE fork time = 10 time = 10 Outside the main fork time = 15 delay 5 time = 20 delay 10 time = 30 delay 20
EXAMPLE : fork/join any
program main {
delay(10);
printf(" BEFORE fork time = %d \n",get_time(LO) );
fork {
delay (20);
printf("time = %d delay 20 \n ",get_time(LO) );
}
{
delay(10);
printf("time = %d delay 10 \n ",get_time(LO) );
}
{
delay(5);
printf("time = %d delay 5 \n ",get_time(LO) );
}
join any
printf(" time = %d Outside the main fork \n",get_time(LO) );
delay(40);
}
RESULTS
BEFORE fork time = 10 time = 15 delay 5 time = 15 Outside the main fork time = 20 delay 10 time = 30 delay 20
EXAMPLE : fork/join all
program main {
delay(10);
printf(" BEFORE fork time = %d \n",get_time(LO) );
fork {
delay (20);
printf("time = %d delay 20 \n ",get_time(LO) );
}
{
delay(10);
printf("time = %d delay 10 \n ",get_time(LO) );
}
{
delay(5);
printf("time = %d delay 5 \n ",get_time(LO) );
}
join all
printf(" time = %d Outside the main fork \n",get_time(LO) );
delay(40);
}
RESULTS
BEFORE fork time = 10 time = 15 delay 5 time = 20 delay 10 time = 30 delay 20 time = 30 Outside the main fork
When defining a fork/join block, encapsulating the entire fork inside braces ({}) results in the entire block being treated as a single thread, and the code executes consecutively.