The terminate statement terminates all active descendants of the current thread in which it was called.If any of the child processes have other descendants, the terminate command terminates them as well.
This example forks off several child processes within a task. After any of the child processes is complete, the code continues to execute. Before the task is completed, all remaining child processes are terminated.
program main {
task1();
printf(" Time = %d Task completed \n",get_time(LO) );
}
RESULTS
before fork time = 10 time = 15 delay 5 time = 20 delay 10 time = 30 delay 20 time = 30 delay 15 time = 40 delay 25 time = 50 delay 35 Time = 115 Task completed
program main {
task1();
printf(" Time = %d Task completed \n",get_time(LO) );
}
RESULTS
before fork time = 10 time = 15 delay 5 Time = 115 Task completed
Both the solutions ahow that the task is completed at time 115 .When teriminate is not used, all the chaild process execute,when teriminate is use,all the chaild processes are teriminated.
Suspend_thread:
The suspend_thread() system task is used to temporarily suspend the current thread.It suspends the current thread and allows other ready concurrent threads to run. When all ready threads have had one chance to block, the suspended thread resumes execution.
EXAMPLE : without suspend_thread
program main {
call();
delay(40);
printf(" END OF SIMUALTION \n");
}
task call(){
shadowinteger i; // using shadow variable
delay(10);
for(i = 0; i < 3; i++) {
fork
{
delay(10);
printf(" time = %0d: i is %0d \n",get_time(LO),i);
printf( " Suspending the thread \n");
// suspend_thread();
printf( " After Suspending the thread \n");
printf(" time = %0d: i is %0d \n",get_time(LO),i);
}
join none
}
}
RESULTS
time = 20: i is 0 Suspending the thread After Suspending the thread time = 20: i is 0 time = 20: i is 1 Suspending the thread After Suspending the thread time = 20: i is 1 time = 20: i is 2 Suspending the thread After Suspending the thread time = 20: i is 2 END OF SIMUALTION
EXAMPLE : with suspend_thread
program main {
call();
delay(40);
printf(" END OF SIMUALTION \n");
}
task call(){
shadowinteger i; // using shadow variable
delay(10);
for(i = 0; i < 3; i++) {
fork
{
delay(10);
printf(" time = %0d: i is %0d \n",get_time(LO),i);
printf( " Suspending the thread \n");
suspend_thread();
printf( " After Suspending the thread \n");
printf(" time = %0d: i is %0d \n",get_time(LO),i);
}
join none
}
}
RESULTS
time = 20: i is 0 Suspending the thread time = 20: i is 1 Suspending the thread time = 20: i is 2 Suspending the thread After Suspending the thread time = 20: i is 0 After Suspending the thread time = 20: i is 1 After Suspending the thread time = 20: i is 2 END OF SIMUALTION
The solution shows that all the child threads are suspended until all the calls are made and then reumed.