title: 3 Processes Synchronization using 2 pipes
body: <p>I am trying to create 2 child processes using fork() and synchronize them so they display a message one after another five times infinitely.</p>
  
  <pre><code>e.g.   Process 1 has process (pid)
         ...
         Process 2 has process (pid)
         ...
         Process 3 has process (pid)
         ...
         Process 1 has process (pid)
         ...
         ...
  </code></pre>
  
  <p>I know this could be done easier using 3 pipes but I was wondering if it would be possible to do it with just 2. For some reason the 1st process never gets the chance to run. Keep in mind that I can only use pipes to solve this problem.</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  int main()
  {
      pid_t p, p1, mypid;
      int i;
      int a = 1;
      int b = 1;  
      int fd[2];      
      int fd1[2]; 
      pipe(fd);   
      p = fork();
      if (p &lt; 0) {perror("Fork"); exit(1); }
      else if (p == 0)
      {
          mypid = getpid();        
          while(1)
          {           
              close(fd[1]);       
              if (read(fd[0], &amp;a, sizeof(int)) == 0)                  
              {           
                  for(i = 0; i &lt; 5; i++) {printf("Process 2 has process %d\n", mypid); }      
                  close(fd[0]);       
                  a = 1;              
                  write(fd[1], &amp;a, sizeof(int)); } }
          }
      else
      {   
          p = fork();
          if (p &lt; 0) {perror("Fork"); exit(1); }  
          pipe(fd1);          
          if (p &gt; 0)
          {
              mypid = getpid();               
              while(1)
              {               
                  close(fd1[1]);          
                  if (read(fd1[0], &amp;b, sizeof(int)) == 1)
                  {
                      for(i = 0; i &lt; 5; i++) {printf("Process 1 has process %d\n", mypid); }                  
                      close(fd[0]);
                      a = 0;                  
                      write(fd[1], &amp;a, sizeof(int)); }                            
                  close(fd[1]);               
                  if ((read(fd1[0], &amp;b, sizeof(int)) == 0) &amp;&amp; (read(fd[0], &amp;a, sizeof(int)) == 1))                
                  {
                      close(fd1[0]);
                      b = 0;
                      write(fd1[1], &amp;b, sizeof(int)); }
              } 
          }           
          else
          {               
              while(1)
              {                   
                  close(fd1[1]);
                  if (read(fd1[0], &amp;b, sizeof(int)) == 0)
                  {               
                      mypid = getpid();       
                      for(i = 0; i &lt; 5; i++) {printf("Process 3 has process %d\n", mypid); }
                      close(fd1[0]);          
                      b = 1;                  
                      write(fd1[1], &amp;b, sizeof(int)); }
              }           
          }
      }
      return(1); }
  </code></pre>
  
  <p>-It is a project for my unix class so I would really appreciate it if you just pointed out my mistakes instead of solving it for me.
  -I think I misunderstood the concept of the pipe/read. Please let me know if I missed something and perhaps link a source to get more specific details.
  -The program might contain some minor mistakes(Missing {} etc).</p>