Need to know how fork works?

前端 未结 7 1785
[愿得一人]
[愿得一人] 2020-12-15 13:32

I am trying the following C code:

int main()
{
    printf(\"text1\\n\");
    fork();
    printf(\"text2\\n\");
    return 0;
}

I was expect

相关标签:
7条回答
  • 2020-12-15 13:58

    The forked process gets a copy of the variable memory, and at the time of the fork the output buffer has yet to be flushed. No output has been written to the console when you fork, only buffered up. Both processes thus continues with text1 already in the buffer, and thus both print it.

    0 讨论(0)
  • 2020-12-15 14:04

    It is because forked process starts after fork, not from very beginning. exec starts process from entry point and will print what you expect.

    0 讨论(0)
  • 2020-12-15 14:10

    fork() creates a new process by copying everything in the current process into the new process. That typically includes everything in memory and the current values of the CPU registers with some minor adjustments. So in effect, the new process gets a copy of the process's instruction pointer as well so it resumes at the same point where the original process would continue (the instruction following the fork()).


    To address your update, printf() is buffered. Normally the buffer is flushed when it encounters a newline character at the end, '\n'. However since you have omitted this, the contents of the buffer stays and is not flushed. In the end, both processes (the original and the child) will have the output buffer with "text1" in it. When it eventually gets flushed, you'll see this in both processes.

    In practice, you should always flush files and all buffers (that includes stdout) before forking to ensure that this does not happen.

    printf("text1");
    fflush(stdout);
    fork();
    

    The output should look like this (in some order):

    text1text2
    text2
    
    0 讨论(0)
  • 2020-12-15 14:12

    fork clones the current process. The new process will "start" at the fork call, not at the start of main as you seem to expect. Thus when you print the first time there is 1 process, then when you fork there are two.

    Since you are forking after printing "text1", it is only printed once.

    In the second example the duplicated output is due to output buffering - printf doesn't actually output anything to the screen until it is flushed or it hits a newline ('\n').

    Consequently the first call to printf actually just wrote data to a buffer somewhere, the data was then copied into the second process' address space, and then the second call to printf would have flushed the buffer, complete with "text1" in both buffers.

    0 讨论(0)
  • 2020-12-15 14:12

    Problem 1 : the output as
          text1
          text2
          text2

    This is because fork() create exact copy (child) of parent process and both processes start their execution right after the system call fork().

    Problem 2 : the output as
          text1text2 
          text1text2 

    This is all about buffering. Refer this link and learn about fork() basics. http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

    0 讨论(0)
  • 2020-12-15 14:13

    from man 2 fork: fork returns 0 to the child process.

    value = fork();
    if( value == -1 ) {
      printf( "fork failed\n" );
      exit(1);
    }
    if( value ) {
      printf( "test1\n" );
    } else {
      printf( "test2\n" };
    }
    
    0 讨论(0)
提交回复
热议问题