4

Why my child process starts from the first line of main? When I run my program, "Hello There" will be printed twice. Child process should start on the line after fork, am I right?

int main(){
       printf("Hello There!");
       pid_t PID;
       PID = fork();

       if(PID == 0){
          //Child
       }
       else{
          //parent
       }
}

This is huge problem to me because in my real program, I'm making private file with mmap before fork. And because of this what happens to me, parent and child have different private files..

Someguy
  • 85
  • 1
  • 10
  • It doesn't. The child starts right after returning from `fork()`. Show your whole code (or, even better, reduce your whole code to a [minimal form exhibiting your undesired behavior](http://stackoverflow.com/help/mcve)) -- the problem lies somewhere else. –  Oct 24 '15 at 19:10
  • and stylistic sidenote ... In C, for "normal" variables we normally use lowercase or camelCase, although it's not *syntactically* wrong to do otherwise. –  Oct 24 '15 at 19:13
  • sorry, overlooked the little fact that you never flush (neither implicitly through output of a *newline*, nor explicitly with e.g. `fflush()`) your *stdout* :) See Michael's answer, it probably hits the nail here, so the problem was *indeed* in your example code! -- guess you could rollback to your first version -- apologies again. –  Oct 24 '15 at 19:18
  • @FelixPalmen Hehe, it's a nasty cunning evil sneaky little lurker isn't it, I read the question and thought the same as you "no way" but I pasted it, compiled it, and my eyes almost popped out of their sockets! Took me a minute with gdb and some tests to figure it out. – Michael Oct 24 '15 at 19:29
  • @Michael "there be dragons" applies quite often with C it seems. Yes, indeed, and very good work :) Interesting enough to upvote both the question and the answer! –  Oct 24 '15 at 19:32
  • @Someguy: I just did the rollback and still leave my comment on purpose -- nice example I guess. Your original question was fine! –  Oct 24 '15 at 19:36

1 Answers1

6

It is NOT being called twice... It's an optical illusion! :)

BUT... your STDOUT is not being flushed, because there is no \n, your fork is then copying your whole state, both processes are proceeding, and eventually they flush, both flushing their output buffers (copied during fork()), and causing the output to appear twice.

If you add a \n, so change your printf line as follows:

printf("Hello There!\n");

You will now get the expected results.

Or flush your output explicitly:

printf("Hello There!");
fflush(stdout);
Michael
  • 3,639
  • 14
  • 29
  • Hmm ... that's probably indeed the solution! suggest rephrasing "not being printed twice" to something like "printf() is not called twice" and add manual flushing and maybe buffering modes :) –  Oct 24 '15 at 19:16
  • Yep, fair comment, done. – Michael Oct 24 '15 at 19:16
  • Thanks, it was the reason for printing twice! But I still have a problem :/ I have file with context "Hello " and I made private file from that with mmap. Then my child adds to the end of private file "World" but this can't be seen from the parent even though I made mmapping before fork – Someguy Oct 24 '15 at 19:22
  • 1
    @Someguy suggest to make that another question, just for the reason to be helpful for future readers :) –  Oct 24 '15 at 19:23
  • 1
    Ok, that's best phrased in another question, there's nowhere near enough info to deal with that here, and if you edit your question now that it's been answered and had upvotes it'll just confuse everyone. – Michael Oct 24 '15 at 19:24
  • Ok, I will do that. Thanks again! – Someguy Oct 24 '15 at 19:25