2
int main(void)
{
   printf("Hello, world! \n");
   fork();
   return 0;
}

why it is print only 2 hello worlds? if evert time when the system excute the function fork() new procces is created, it need to print "Hello, world! \n" forever?

ido kahana
  • 165
  • 2
  • 8
  • 1
    `fork` splits off a new process from the point at which the `fork` is called. – Christian Gibbons Jul 20 '18 at 15:05
  • Fork() create a child process from the function it called. In your case it executed once and if you want to create more than one child process then put in the loop. – danglingpointer Jul 20 '18 at 15:08
  • 2
    Also, from the looks of it, it should only be printing `"Hello, world! "` once, as you fork after the print. – Christian Gibbons Jul 20 '18 at 15:08
  • 1
    i run it on http://rextester.com/l/c_online_compiler_gcc and it is print hello worlds twice – ido kahana Jul 20 '18 at 15:13
  • Same behavior on [ideone.com](https://ideone.com/eEAlly). Interesting. – Fred Larson Jul 20 '18 at 15:19
  • 1
    @idokahana I seems like that site fails to treat the stdout buffer correctly. I think it isn't flushing the buffer until the end of the process (after it's forked into two processes) and thus it prints twice. What should happen, though, is the buffer gets flushed before the fork due to the newline character being printed. If you manually flush the buffer with `fflush(stdout)` before forking, it prints correctly. – Christian Gibbons Jul 20 '18 at 15:20
  • 1
    Should be the same (printed twice) if you run the program with stdout redirection. That changes buffering mode too – Ingo Leonhardt Jul 20 '18 at 15:28
  • Very closely related to [`printf()` anomaly after `fork()`](https://stackoverflow.com/questions/2530663/) — if not a duplicate of it. – Jonathan Leffler Jul 20 '18 at 15:53

2 Answers2

6

When you are executing your program, fork creates a new process and continues execution at the point that you call fork().

So when you reach fork(), the program has already called printf("Hello, world! \n");, and both the parent and child processes just return 0; and the program finished execution.

If you just want to print "Hello world" forever, just do:

while(true) {
    printf("Hello, world! \n");
}

If you wanted to make a fork bomb (bad):

while(true) {
    fork();
    printf("Hello, world! \n");
}

I wouldn't recommend running this code, as its unsafe and will probably crash your terminal/computer.

eqwert
  • 507
  • 6
  • 15
  • 1
    Depending on what system you're on you can run `ulimit -u N` to limit yourself to N running processes. So even if you accidentally (or not) run a fork bomb it won't break the machine. It may still be difficult to kill all of the processes though (I suggest SIGSTOP followed by SIGKILL) – Kevin Jul 20 '18 at 15:20
  • @Kevin Ah, I didn't know about that `ulimit` command. That could save quite a few accidental mistakes. Thanks! – eqwert Jul 20 '18 at 15:23
  • It's saved my butt a few times :) – Kevin Jul 20 '18 at 15:23
6

This Program should be printing Hello world once. still if it prints it twice it is because the line buffer is not cleared.
The line buffer should be cleared because there is \n in your printf.still its not cleared means this is about the platform you are using to execute the code.
You can verify this by adding fflush(stdout) after the printf().

int main(void)
{
   printf("Hello, world! \n");
   fflush(stdout);
   fork();
   return 0;
}
LearningC
  • 3,182
  • 1
  • 12
  • 19
  • I don't think the issue is the compiler so much as the system running it. The site OP used lets you choose different compilers and they exhibit the same issue (I tried it with both gcc and clang on the site). – Christian Gibbons Jul 20 '18 at 15:23
  • Its about the platform/site he is using. in system/other sites "\n" will clear the line buffer. ya it may not be in the compiler they are using. its somewhere in thier system probably. – LearningC Jul 20 '18 at 15:27
  • @ChristianGibbons anyway i cant tell its the compiler. edited the answer. thanks – LearningC Jul 20 '18 at 15:29
  • 3
    It's about stdout redirection, which changes buffering mode. I've just compiled in linux: `./a.out` --> one print, `./a.out | more` --> two prints. And of cours all online compilers have to redirect stdout – Ingo Leonhardt Jul 20 '18 at 15:34