5
void child(int pid){
    printf("Child PID:%d\n",pid);
    exit(0);    
}
void parent(int pid){
    printf("Parent PID:%d\n",pid);
    exit(0);
}

void init(){
    printf("Init\n");//runs before the fork
}


int main(){

    init();//only runs for parent i.e. runs once
    printf("pre fork()");// but this runs for both i.e. runs twice
    //why???

    int pid = fork();

    if(pid == 0){
        child(pid); //run child process
    }else{
        parent(pid);//run parent process
    }
    return 0;
}

output:

Init
pre fork()Parrent PID:4788
pre fork()Child PID:0

I have a process in a Unix OS (Ubuntu in my case). I can't for the life of me understand how this works. I know the fork() function splits my programs in two processes but from where? Does it create a new process and run the whole main function again, and if so why did the init() only run once and the printf() twice?

Why does the printf("pre fork()"); run twice and the init() function only once?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
boogie666
  • 650
  • 10
  • 24

1 Answers1

24

There's only one process until the fork. That is, that path is executed only once. After the fork there are 2 processes so the code following that system call is executed by both processes. What you ignore is that both terminate and both will call exit.

In your code you're not flushing stdio. So both processes do that (exit flushes stdio buffers) - that's why you're seeing that output.

Try this:

printf("pre fork()\n");
                  ^^ should flush stdout

Or maybe

printf("pre fork()\n");
fflush(stdout);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Wow, this is so subtle! +1 from me! – André Caron Jan 14 '12 at 16:33
  • 2
    Simply putting a newline in does not necessarily flush the buffer. Run the code with stdout redirected to a regular file and you will see exactly the same behavior. stdout is *not* line buffered by default unless it is a tty. – William Pursell Jan 15 '12 at 16:15
  • 1
    @WilliamPursell That's completely right. And even if the destination is a tty, the standard doesn't guarantee `\n` will trigger a flush. – cnicutar Jan 15 '12 at 16:29