1

I m doing some C program using fork()
But I'm getting unexpected output.

#include <stdio.h>

main(){
  printf("\nStart of Program");
  fork();
  printf("\nEnd of Program");
}

Output:

Start of Program
End of ProgramStart of Program
End of Program

Why do I get this output ?

Output should be this:

Start of Program
End of Program
End of Program

Swap
  • 165
  • 3
  • 12

5 Answers5

2

This issue is happening because of buffering.

Quoting this post.

When the output of your program is going to a terminal (screen), it is line buffered. When the output of your program goes to a pipe, it is fully buffered.

The posts also contains line by line explanation.

Community
  • 1
  • 1
Haris
  • 12,120
  • 6
  • 43
  • 70
0

This is because of printf(), printf is buffering the output and only print when he reaches a '\n', so when reaching printf("\nEnd of Program"); printf() prints the content of the buffer which is "Start of Program". Because fork() duplicates your stack, printf() buffer is duplicated too. Try:

#include<stdio.h>
main(){
printf("Start of Program\n");
fork();
printf("End of Program\n");
}
Kotshi
  • 338
  • 3
  • 15
0

Few things here need to check.

From where it is printing "student". I do not see any where you are printing student in your code.

The ideal output is

Start of Program
End of Program
End of Program

If you are not getting above output I will suspect, when fork executed and child got created probably your standard output print was not complete. Flush or \n as last char in print is a must to complete the print job,

To verify this what you can do is, before fork just use fflush. Also in printf use \n as last char.

#include<stdio.h>
main()
{
    printf("Start of Program\n");
    fflush(NULL);
    fork();
    printf("End of Program\n");
}

Try this and let us know.

Austin
  • 1,709
  • 20
  • 40
0
when fork is come then new process is created. 

so end of program before fork call process and after fork call is printed. so that's logic.

-2

I compiled that code myself, and I get following output:

Start of Program

End of Program

Start of Program

End of Program

Which is correct since fork() spawns exactly the same child process its forked from.

Edit: Thanks to the comments, I found out whats really going on. Fork behaves as expected, but the input buffer is not getting flushed before the child process is executed. I've modified the original code to this:

#include <stdio.h>

main(){
  printf("\nStart of Program\n");
  fflush(stdout);
  fork();
  printf("\nEnd of Program\n");
}

And the output now reads:

Start of Program

End of Program

End of Program

Which indicates that indeed, fork() only spawns the child process executing downwards from after the fork() call.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • try with \n at end of printf() like printf("Start of Program\n"); It will gives another Output. – Swap Oct 21 '15 at 08:41
  • @SwapnilDewakar same, except there is 1 additional line break between the lines. – Magisch Oct 21 '15 at 08:43
  • Use `fflush(stdout);` after the first `printf()` and you will see the difference. `fork()` continues **after** the fork in the child and the parent process, so it looks like the first printf is executed twice which it is not - the output is a result of buffering. See answer by @Haris. – Andreas Fester Oct 21 '15 at 08:45
  • @SwapnilDewakar Corrected the Answer. – Magisch Oct 21 '15 at 08:51