title: communication between pipe using popen
body: <p>i want to :</p>
  <ol>
  <li>optimize this code eventually by using fgetc and feof.</li>
  <li>know if there is another do what this code do</li>
  </ol>
  <p>The basic idea of ​​my code is to create a counter that increments with each call to popen. As soon as the counter reaches a certain value, in this case 5, it communicates to all the processes before its state and then closes</p>
  <pre><code>    int main (int argc, char* argv[]) {
          
          int counter = atoi(argv[1]);
      
          if (counter == 5) {
              printf(&quot;I am number 5, I Quit !\n&quot;);
              fflush(stdout);
              sleep(1);
              return 0;
          }
      
          char new_command [20];
          snprintf(new_command, sizeof(new_command), &quot;%s %d&quot;, argv[0], counter+1);
          FILE* fp = popen(new_command, &quot;r&quot;);
      
          const struct timespec chock = {0,10000};
      
          char letter;
          char keep_on = 'y';
          while(keep_on == 'y') {
              while(fread(&amp;letter, sizeof(letter), 1, fp) != 0) {
                  printf(&quot;%c&quot;, letter);
                  if (letter == '\n') {
                      keep_on = 'n';
                  }
      
              }
              fflush(stdout);
              nanosleep(&amp;chock, NULL);
          }
      
          printf(&quot;%d: Finished\n&quot;, counter);
  
      
          return 0;
      }
  </code></pre>