angel208
angel208

Reputation: 281

fork(), runs always the parent first and then the child

im learning about fork() but something is working wrong in my ubuntu. im running this code:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
printf("--beginning of program\n");

int counter = 0;
pid_t pid = fork();

if (pid == 0)
{
    // child process
    int i = 0;
    for (; i < 5; ++i)
    {
        printf("child process: counter=%d\n", ++counter);
    }
}
else if (pid > 0)
{
    // parent process
    int j = 0;
    for (; j < 5; ++j)
    {
        printf("parent process: counter=%d\n", ++counter);
    }
}
else
{
    // fork failed
    printf("fork() failed!\n");
    return 1;
}

printf("--end of program--\n");

return 0;
} 

i know the parent and the child should run their code in no specific order so the code should return something like this:

-beginning of program
parent process: counter=1
parent process: counter=2
parent process: counter=3
child process: counter=1
parent process: counter=4
child process: counter=2
parent process: counter=5
child process: counter=3
child process: counter=4
child process: counter=5
--end of program--

But every time i run the program, they run in what it seems to be the same order. here is a capture of what i get every single time i run this program:

user@user-VirtualBox:~/Documents/S-O$ ./sample
--beginning of program
parent process: counter=1
parent process: counter=2
parent process: counter=3
parent process: counter=4
parent process: counter=5
--end of program--
user@user-VirtualBox:~/Documents/S-O$
child process: counter=1
child process: counter=2
child process: counter=3
child process: counter=4
child process: counter=5
--end of program--

it seems like the parent finish first, and then the child starts. but i guess thats not true. please note that the second shell prompt is opened by the program itself. any ideas of what may be happening?

Edit:

if i put an sleep(1) it works just fine. i still think that with no delay it shouldn't always have the same execution order. even counting until 100, it giver the same thing

Upvotes: 2

Views: 3866

Answers (2)

Manoj Chetry
Manoj Chetry

Reputation: 1

I too faced the same problem. The issue got solved just by adding a wait(NULL) instruction to the parent. As far as i know this happens because of the parent not made to wait for its child process and the child becomes an orphan here with no parent... Correct me if I am wrong. Happy learning

Upvotes: 0

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

Yes parent and children run their code in an almost unpredictable order (remember that there is no randomness), but your scheduler probably choose, after forking, to schedule the parent first and then the child. As both have a very small code, the parent is able to run all of its code, and then the children. To experiment something else you must do something much much more longer after forking in both processes (You will be able to see different interleaving of outputs for example).

The prompt is not produced by your code, but is an effect of it. When the parent process dies, the shell that ran it get control back and prompt you for another command, while in the same time the children is working and pollutes your terminal. The child has become an orphan process. To prevent this, you must tell the parent to wait its child by calling...wait().

Upvotes: 3

Related Questions