title: C Pthreads - Parent/Child process
body: <p>I've written a C program that creates a child thread. After creating the child thread, the parent thread should output two messages. The first being "I am the parent" and the second "The parent is done". The same should occur for the child thread "I am the child" and "The child is done". However I want to make sure, the second message of the child is always done before the second message of the parent. How can I achieve this in my code below?</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  int status = 0;
  
  void *print_child(void *arg)
  {
  
      while (status == 0)
      {
          printf("Signal hasn't changed..\n");
          sleep(1);
  
      }
      printf("The child has started...\n");
      printf("The child is done! \n ");
  }
  
  int main()
  {
      pthread_t child;
      pthread_create(&amp;child, NULL, &amp;print_child, NULL);
  
      sleep(2);
      printf("The parent has started...\n");
      printf("The parent is done! \n");
  
      status++;
  
      if (pthread_join(child, NULL))
      {
          printf("ERROR");
          exit(1);
      }
  }
  </code></pre>