-7

Does this produce 16 copies of hello? If not, how many?

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

int main(void)
{

    printf("hello\n");
    fork();
    fork();
    fork();
    fork();
    return(0);

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ronnie
  • 17
  • 3
  • 9
    What happened when you tried? How many did you got? Did you try to check this logic using a pen and paper? – Sourav Ghosh Mar 03 '20 at 06:48
  • 1
    Each invocation of `fork()` results in two processes, the child and the parent. Thus the first fork results in two processes. The second `fork()` is reached by those two processes, yielding four processes. The third `fork()` is reached by those four, netting eight, and similarly the final `fork()` yields sixteen. – Pratik Sampat Mar 03 '20 at 06:54
  • 5
    This, as most questions which give the strong impression of being a blind homework dump, lacks focus on the specific programming problem you encountered when trying to work this out yourself. To fix, have a look at the compromise described here https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Yunnosch Mar 03 '20 at 06:56
  • 1
    @PratikSampat A good explanation of the forking. But you might want to add an explanation of how many of those processes output additional copies of the print from before all the forking. – Yunnosch Mar 03 '20 at 06:57
  • 1
    SO is not an online compiler. Run it and see for yourself. Also read the friendly `man`ual. – Lundin Mar 03 '20 at 07:52
  • 1
    @Lundin : while in general that sentiment is appropriate, in this case, unfortunately, the *way* you run it will influence the result, and a full understanding can't be easily derived from test runs. – Sander De Dycker Mar 03 '20 at 07:54
  • 1
    @Yunnosch That's just excuses for allowing an overly broad question. If the OP could narrow down the question and specify what's meant with "producing", it would be answerable. What if I'm to ask "what does a program produce?" with no more info provided. – Lundin Mar 03 '20 at 07:56
  • @Yunnosch, what's the fun in that? :) – Pratik Sampat Mar 03 '20 at 08:20

2 Answers2

10

It depends on whether you send the output to a file or pipe it to a program such as cat (or wc -l) or whether you leave it to write to the terminal. If it goes to the terminal, you should get just one line printed; if it goes to a pipe or file, you'll get 16.

See also printf() anomaly after fork().

The difference is that when the output goes to the terminal, it is line-buffered; when it goes to a 'non-interactive device' (file, pipe), it is fully-buffered.

  • When the output is line-buffered, the newline in the format forces the output to appear before any of the fork() calls is executed. There is nothing left for any of the processes to print.

  • When the output is fully buffered, the printf() doesn't force the buffered data to the output. Each of the 16 processes created from the 4 consecutive fork() calls has a copy of the same buffer, waiting to be flushed. When the processes exit, those buffers are flushed, and therefore 16 copies of the output appear.

See C11 §7.21.3 Files ¶3 and ¶7 for the standard specification.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

Since fork(); is called after the print statement. There will be only one copy of hello. The output will be:

hello

Mike
  • 4,041
  • 6
  • 20
  • 37
Maaz
  • 21
  • 1
  • 2
    If the program is called `hello`, try `hello` (which does produce one line of output), and then try `hello | cat` or `hello | wc -l`. What does that produce? – Jonathan Leffler Mar 03 '20 at 08:16