Actually when fork() executed,
child process created, therefore now two processes running from the next point point in the program
The execution of child processes is independent on Parent process therefore interleaved execution may happened
by using semaphore we are restricting that interleaving process therefore either Parent or Child process can enter into CS but at a time only one process.
Therefore Option D is correct.
For observing the difference,
fork();
Wait(sem);
printf(―Gate ―);
Signal(sem);
replace with
int t=fork();
Wait(sem);
if(t==0)
{
printf( ― CHILD LINE1 ― );
printf( ― CHILD LINE2 ― );
}
else
{
printf( ― PARENT LINE1 ― );
printf( ― PARENT LINE2 ― );
}
Signal(sem);
then o/p is ONE OF THESE
1) ― PARENT LINE1 ― ― PARENT LINE2 ― ― CHILD LINE1 ―― CHILD LINE2 ―
2) ― CHILD LINE1 ―― CHILD LINE2 ― ― PARENT LINE1 ― ― PARENT LINE2 ―
what happend if we doesn't use semaphores in the above example?
one of the possible outcome may be
1) ― PARENT LINE1 ― ― CHILD LINE1 ― ― CHILD LINE2 ― ― PARENT LINE2 ―
2) ― PARENT LINE1 ― ― CHILD LINE1 ― ― PARENT LINE2 ― ― CHILD LINE2 ―