1

I read somewhere that in Unix, after fork() is executed successfully, both processes will start their execution at the next statement following the fork() call. Meanwhile when I run this code in C

#include <stdio.h>
int main()
{
   printf(" do ");
   if(fork()!=0) printf ("ma ");
   if(fork()==0) printf ("to \n");
   else printf("\n")

    return 0;
}

one possible output is

 do ma
 do ma to
 do 
 do to

"printf(" do ");" is before the fork() call so how comes "do" is repeated several times in the output?

Artemisia
  • 45
  • 1
  • 5
  • 2
    you're forking TWICE... it should be `pid = fork(); if (pid) { ... } else { ...}` to detect if you're in the child or parent. and since your `do` output has no line break, it sits in the output buffer until a line break is encountered, which is **AFTER** your fork. so you have two buffers with `do` sitting in them. – Marc B May 19 '15 at 20:19
  • Someone else find the dups - there are loads - stdout buffering thing. – Martin James May 19 '15 at 20:20
  • Than you for your answer. I know I'm forking twice, it is done on purpose, it is an exercice from my teacher where I should predict the output, but I still don't understand why do is written several times as it is before any of the two fork() calls. – Artemisia May 19 '15 at 20:28
  • OK, I found one of the dups:) – Martin James May 19 '15 at 20:29
  • I think the "do"s are there because they are buffered in the parent output memory which is copied to all children. – Tim3880 May 19 '15 at 20:32

3 Answers3

2

If you see the definition of fork it says that the child and parent are exact same copy and both start executing at the next instruction.
Now, Since C doesn't flush output until a newline is printed or you explicitly flush output, the "do" stays in buffer and gets copied to child too.
use:

printf(" do ");
fflush(stdout);

or

printf(" do \n"); //Notice the newline in the end

and you will get expected output

dev_ankit
  • 516
  • 4
  • 8
0
   if(fork()!=0) printf ("ma ");

This line creates two processes; one of them is the parent and runs the printf statement, and one is the child and doesn't.

   if(fork()==0) printf ("to \n");

Both processes run this statement; the result is four processes:

  1. Parent-parent; this runs the first printf and prints " do ma "
  2. Parent-child; this runs both and prints " do ma to \n"
  3. Child-parent; this runs neither and prints " do "
  4. Child-child; this runs the second and prints " do to \n"

The issue you have is that you think the fork return "skips" to the next statement; rather, when fork returns, you now have two processes with different return values. Typically, printf is not flushed until the program ends or you print a newline.

rlbond
  • 65,341
  • 56
  • 178
  • 228
0

I think the "do"s are there because they are buffered in the parent output memory which is copied to all children.

If you add "\n" to the first "do":

printf(" do\n");

you will not see any "do" after the fork.

Tim3880
  • 2,563
  • 1
  • 11
  • 14