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?

8
  • 1
    fork splits off a new process from the point at which the fork is called. Commented Jul 20, 2018 at 15:05
  • 3
    Also, from the looks of it, it should only be printing "Hello, world! " once, as you fork after the print. Commented Jul 20, 2018 at 15:08
  • 1
    i run it on rextester.com/l/c_online_compiler_gcc and it is print hello worlds twice Commented Jul 20, 2018 at 15:13
  • 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. Commented Jul 20, 2018 at 15:20
  • 1
    Should be the same (printed twice) if you run the program with stdout redirection. That changes buffering mode too Commented Jul 20, 2018 at 15:28

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

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).
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.
@ChristianGibbons anyway i cant tell its the compiler. edited the answer. thanks
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
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.

3 Comments

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 Ah, I didn't know about that ulimit command. That could save quite a few accidental mistakes. Thanks!
It's saved my butt a few times :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.