Home > OS >  How many processes will be created by the C program below?
How many processes will be created by the C program below?

Time:09-21

Can you give a explanation along with the answer. I'm having some issue with identifying the correct no of processes that have being created by these system calls.

I think there will be 8 processes created.

#include <stdio.h>

main()
{
    printf("Here comes the date. \n");    
    printf("Here it is. \n");

    fork();    
    fork();    
    fork();

    execl("/bin/date", "date", 0); //Line A

    printf("That was the date. \n"); //Line B    
    printf("Program will exit now. \n"); //Line C    
}

CodePudding user response:

The code shown creates seven processes. When the code starts, there is already a process executing the program. Then fork creates one more, so there are two.

Then those two processes execute fork, each creating one more process, so there are four.

Then those four processes execute fork, each creating one more process, so there are eight.

Thus seven new processes are created.

The execl does not create a new process; it just changes what program the process is executing.

  • Related