title: Can the order of execution of fork() be determined?
body: <p>I'm working on an exercise on the textbook "Operating System Concepts 7th Edition", and I'm a bit confused about how does <code>fork()</code> work. From my understanding, <code>fork()</code> creates a child process which runs concurrently with its parent. But then, how do we know exactly which process runs first? I meant the order of execution.</p>
  
  <blockquote>
    <p><strong>Problem</strong><br>
    Write a C program using <code>fork()</code> system call that generates the Fibonacci sequence in the child process. The number of sequence will be provided in the command line.</p>
  </blockquote>
  
  <p>This is my solution:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  void display_fibonacci_sequence( int n ) {
      int i = 0;
      int a = 1;
      int b = 1;
      int value;
      printf( "%d, %d, ", a, b );
      for( ;i &lt; n - 2; ++i ) {
          value = a + b;
          printf( "%d, ", value );
          a = b;
          b = value;
      }
      printf( "\n" );
  }
  
  int main( int argc, char** argv ) {
      int n;
      pid_t pid;
      pid = fork();
      if( argc != 2 ) {
          fprintf( stderr, "Invalid arguments" );
          exit( -1 );
      }
      n = atoi( argv[1] );
  
      if( pid &lt; 0 ) {
          fprintf( stderr, "Fork failed" );
          exit( -1 );
      }
      else if( pid == 0 ) {
          display_fibonacci_sequence( n );
      }
      else { // parent process
          // what do we need to do here? 
      }
  }
  </code></pre>
  
  <p>To be honest, I don't see any difference between using <code>fork</code> and not using <code>fork</code>. Besides, if I want the <em>parent</em> process to handle the input from user, and let the <em>child</em> process handle the display, how could I do that?</p>