2

I'm having difficulty understanding the behavior of fork(). I thought the child process will execute the lines "after" the fork(). So I expected to see only one "Hello world!", but this code:

printf("Hello World!\n");
fork();
return 0;

outputs two "Hello World". Why is that?

I also noticed from online examples using pipe() that pipes are created before forking a child. How does the child also have a pipe when it was created after the creation of the pipe in the parent process?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
9ganzi
  • 181
  • 10
  • Dupe: [Duplicated output using printf() and fork() in C](https://stackoverflow.com/questions/59860561/duplicated-output-using-printf-and-fork-in-c) – Marco Bonelli Feb 28 '21 at 22:16
  • Can you give us the simplest *complete* code that replicates this problem? Is this code from `main` or does that `return` go somewhere? – David Schwartz Feb 28 '21 at 22:21
  • Ask one question per post. A question about output appearing twice is separate from a question about why both child and parent have a pipe that the parent opened. – Eric Postpischil Feb 28 '21 at 22:26
  • The `printf` function is buffered. It can write to a buffer and then not flush the buffer until you call `fflush` or your process terminates. You should not allow both forked processes to terminate normally if there's any chance the parent arranged to do something on normal termination that you don't want the child to also do. – David Schwartz Feb 28 '21 at 22:27

2 Answers2

3

The first question is more than likely related to your program's file buffering mode, it's likely using full buffering setup, meaning that the stream is only written once the buffer (stdout) is full, this will delay the output, and the child process will also output, because it has the same stdout.

If you use fflush(stdout) right after the printf, or if you change your buffering mode (setvbuf) to line buffered or not buffered at all, you will prevent the duplicate output because it will happen before the fork.

As for the second question, the child process duplicates the code of the parent process, after the fork, as you correctly mentioned, but it also duplicates the file table. By creating the unnamed pipes before the fork you will assure the child has the same file descriptors. This is commonly used to setup pipe comunication between child and parent processes.

You can check the /proc/<process id>/fd folder for each one of the two processes to confirm this.

Footnote

For future reference, these are two different subjects, albeit tangential, they belong in different questions, you can see one of the problems here, the question was close with duplicates related to the first question but not the second, though by chance it didn't remain unanswered, it's still burried in a different matter and virtually undiscoverable by other users.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • If child is a duplicate of the parent, shouldnt the program create infinite number of child process because of the fork()? Does child process skip the line it was forked()? – 9ganzi Feb 28 '21 at 22:33
  • 1
    @9ganzi, the code is only duplicated after the fork, I updated my answer with the reason for program's behavior, and solutions, – anastaciu Feb 28 '21 at 23:18
0

When we do fork the child process get the exact copy of address space of parent process. So such behaviour should be expected. The pipe system call returns a file descriptor and both parent and child process have this descriptor .