title: Program run in child process doesn't loop
body: <p>I have a specific question regarding the parent process reading the stdout from child. My problem is that when I run the program, the child program should execute a new program multiple times in a loop but it runs it only once and exits to the parent process. The child process is running a simple program that prints a message to stdout.
  Thanks in advance.</p>
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;time.h&gt;
  #include &lt;sys/wait.h&gt;
  
  #define ssec 1
  #define fsec 0
  #define secBuf 5
  #define decbase 10
  #define fgbuf 60
  
  volatile sig_atomic_t aborting = 0;
  
  
  void chld_handler(int sig) {
     if (sig == SIGINT) {
        aborting++;
     }
  }
  
  
  int rand_start(int low, int high) {
     int s;
     int r = 0;
     srand(time(NULL));
  
     s = rand();
  
     r = s % high + low;
  
     return r;
  }
  
  
  void Usage() {
     printf(&quot;Usage :schedule [-s&lt;seconds&gt;] [-f&lt;seconds&gt;] &lt;program&gt; &lt;emergency&gt; &lt;logfile&gt;\n&quot;);
     exit(1);
  }
  
  
  int main(int argc, char *argv[]) {
     /* getopt variablen */
     int opt;
     int index;
  
     int sflag = 0;
     int fflag = 0;
  
     char *svalue;
     char *fvalue;
  
     int sseconds = ssec;
     int fseconds = fsec;
  
     char *reactor;
     char *emergency;
     char *logfile;
  
     /* */
     char *endptr;
     /* Pipe and child */
  
     int pipepc1[2];
     int pipepc2[2];
  
     int child1_fd;
     int child2_fd;
  
     FILE *log;
     FILE *pipe_reader;
  
     char *p;
     char *buf;
     int waitFc;
     int status;
  
     /* prgm */
     int prg_r;
     int prg_e;
  
     /* termination */
     int cnt = 0;
     int start_period;
     p = malloc(fgbuf * sizeof(char));
     buf = malloc(fgbuf * sizeof(char));
  
     (void)signal(SIGINT, chld_handler);
  
     if (argc &lt; 4) {
        Usage();
     }
     else if (argc == 4) {
        reactor = argv[1];
        emergency = argv[2];
        logfile = argv[3];
     }
     else if (argc &gt; 4) {
        /* argumentenbehandlung*/
        while((opt = getopt(argc, argv, &quot;:s:f:&quot;)) != -1) {
           printf(&quot;sflag %d fflag %d \n&quot;, sflag, fflag);
           printf(&quot;opt  %c \n&quot;, opt);
           printf(&quot;optind %d \n &quot;, optind);
           switch (opt) {
  
           case 's':
              if (sflag == 0) {
                 sflag++;
                 svalue = optarg;
              }
              else {
                 fprintf(stderr, &quot;Widerholung der option -s\n&quot;);
             Usage();
              }
              break;
  
           case 'f':
              if (fflag == 0) {
                 fflag++;
             fvalue = optarg;
              }
              else {
                 fprintf(stderr, &quot;Widerholung der option -f\n&quot;);
             Usage();
              }
              break;
  
           case ':' :
              fprintf(stderr, &quot;Option -%c brauch ein argument\n&quot;, optopt);
              Usage();
              break;
  
           case '?' :
              fprintf(stderr, &quot;Nicht bekannte option -%c \n&quot;, optopt);
              Usage();
              break;
  
           default :
              assert(0);
           }/* switch */
        }/* while getopt */
  
        for (index = optind; index &lt; argc; index++) {
           if ((argc - index) == 3) {
              reactor = argv[index];
           }
           else if ((argc - index) == 2) {
              emergency = argv[index];
           }
           else if ((argc - index) == 1) {
              logfile = argv[index];
           }
        } /* for schleife*/
  
        /*  */
        if (sflag) {
           sseconds = (int)strtol(svalue, &amp;endptr, decbase);
           printf(&quot;%d ssec\n&quot;, sseconds);
        }
  
        if (fflag) {
           fseconds = (int)strtol(fvalue, &amp;endptr, decbase);
           printf(&quot;%d fsec\n&quot;, fseconds);
        }
     }
  
     /* pipeing*/
     if (pipe(pipepc1) == -1) {
        fprintf(stderr, &quot;Fehler bei der Erzeugung der pipe\n&quot;);
        exit(1);
     }
     else {
        printf(&quot;Pipe created\n&quot;);
     }
  
     /* erzeuge child1*/
     child1_fd = fork();
  
     if (child1_fd &lt; 0) {
        fprintf(stderr, &quot;Fehler beim asfuehren von fork\n&quot;);
        exit(0);
     }
  
     if (child1_fd == 0) {
        printf(&quot;**CHILD**\n&quot;);
        /* close pipe read*/
        if (close(pipepc1[0]) == -1) {
           fprintf(stderr, &quot;Konnte nicht das Read-Ende vom pipepc1 schliessen\n&quot;);
           exit(1);
        }
  
        if (close(1) == -1) {
           fprintf(stderr, &quot;Konnte nicht das Read-Ende vom pipepc1 schliessen\n&quot;);
           exit(1);
        }
  
        if (dup(pipepc1[1]) !=STDOUT_FILENO) {
           fprintf(stderr, &quot;Das setzen des Read-Endes als stdout is Fehlgeschlagen\n&quot;);
           exit(1);
        }
  
        if (fseconds == 0) {
           start_period = sseconds;
        }
        else
           start_period = rand_start(sseconds, (sseconds + fseconds));
  
        for (cnt = 0; cnt &lt; 5; cnt++) {
           sleep(start_period);
           fflush(stdout);
           prg_r = execl(reactor, &quot;&quot;, NULL);
           //printf(&quot;prg_r ist %d  \n&quot;, prg_r);                                                                                                                
        }
  
        if (close(pipepc1[1]) == -1) {
           fprintf(stderr, &quot;Das neue stdout konnte nich geschlossen werden\n&quot;);
           exit(1);
        }
     }
     else {
        printf(&quot;**PARENT**\n&quot;);
  
        log = fopen(logfile, &quot;w&quot;);
  
        /* schliesse pipe read*/
        close(pipepc1[1]);
  
        pipe_reader = fdopen(pipepc1[0], &quot;r&quot;);
  
        while ((buf = fgets(p, fgbuf, pipe_reader)) != NULL) {
           printf(&quot;from Child : %s \n&quot;, buf);
           fflush(pipe_reader);
        }
  
        fclose(log);
  
        waitFc = waitpid(child1_fd, &amp;status, 0);
  
        if (waitFc == -1) {
           fprintf(stderr, &quot;Das Warten ist fehlgeschlagen\n&quot;);
           exit(1);
        }
  
        printf(&quot;child is done\n und waitFc = %d\n und satus %d&quot;, waitFc, status);
        fclose(pipe_reader);
        close(pipepc1[1]);
     }
  
     printf(&quot;argc = %d \n&quot;, argc);
     exit(0);
  }
  </code></pre>
  <p>and the reactor program :</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  int main() {
     srand(time(NULL));
     int i;
     int s;
  
     s = rand() % 7;
     /* printf(&quot;%d \n&quot;, s);*/
  
     if (s != 6) {
        printf(&quot;OK\n&quot;);
        fflush(stdout);
     }
     else {
        printf(&quot;PRESSURE TOO HIGH - IMMEDIATE SHUTDOWN REQUIRED&quot;);
        exit(EXIT_FAILURE);
     }
  }
  </code></pre>