title: how to make a simple synchronization between two processes
body: <p>hi there i have a work about a programme which is like one child should print a number into a text file and the second child should take that number to print it onto screen simultaneously. but my code is work like first child finishes to printing the numbers <code>0</code> through <code>9</code> and then second child starts to read them onto screen. so i guess its a synchronization issue. here is my simple code ;</p>
  
  <pre><code>#include &lt;stdio.h&gt;     /* basic I/O routines.   */
  #include &lt;unistd.h&gt;    /* define fork(), etc.   */
  #include &lt;sys/types.h&gt; /* define pid_t, etc.    */
  #include &lt;sys/wait.h&gt;  /* define wait(), etc.   */
  #include &lt;signal.h&gt;    /* define signal(), etc. */
  #include &lt;pthread.h&gt;
  
  void write_num(void);
  void print_screen(void);
  //void catch_child(int);
  
  int main(int argc, char* argv[]) {
  
          int i, result, pid;
  
          pid = fork(); /* creates a new process, you only want the parent to spawn children? */
  
          switch (pid) {
  
               case -1:
                  /* fork error */
                  printf("Error occured with fork()\n");
                  exit(1);
               case 0:
                  /* child process */
                  write_num(); 
                  exit(0);
               default:
                   /* parent process*/
                  {
                  //wait(&amp;pid);
                  pid = fork(); /* fork new children here */
  
  
                  switch(pid) {
  
                      case -1:
                          printf("Error occured with fork()\n");
                          exit(1);
  
                      case 0: 
  
                          print_screen();
                          exit(0);
  
                      default:
                          break;
  
                           }
                  }
       }
      wait(&amp;pid);
      execl("/usr/bin/killall","killall","tail",(char *) 0);
      return 0;
  }
  
  void write_num(void){
  
   FILE* fptr;
   int i;
  
   fptr=fopen("textfile.txt","w");
  
      for(i=0; i&lt;10; i++){
  
          fprintf(fptr,"%d\n",i);
          fflush(stdout);
          sleep(1);
  
          }
  }
  
  void print_screen(void){
  
          execl("/usr/bin/tail","tail","-f","./textfile.txt",(char *) 0);
          sleep(1);
  
  }
  
  /* first, here is the code for the signal handler
  void catch_child(int sig_num)
  {
       when we get here, we know there's a zombie child waiting
      int child_status;
  
      wait(&amp;child_status);
      printf("child exited.\n");
  }*/
  </code></pre>
  
  <p>by the way, in Ubuntu i used to compile with <code>gcc -o process process.c -lpthread</code>.</p>
  
  <p>I will be appreciated if you can help.</p>