Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram run in child process doesn't loop
    text
    copied!<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> <p><pre><code></p> <p> #include &lt;sys/types.h> #include &lt;stdio.h> #include &lt;string.h> #include &lt;unistd.h> #include &lt;stdlib.h> #include &lt;signal.h> #include &lt;unistd.h> #include &lt;assert.h> #include &lt;time.h> #include &lt;sys/wait.h> #define ssec 1 #define fsec 0 #define secBuf 5 #define decbase 10 #define fgbuf 60 </p> <p>volatile sig_atomic_t aborting = 0;</p> <p>void chld_handler (int sig) { if(sig == SIGINT){</p> <pre><code> aborting++; } } </code></pre> <p>int rand_start(int low, int high){ int s; int r = 0; srand(time(NULL));</p> <p>s = rand();</p> <p>r = s % high + low;</p> <p>return r;</p> <p>}</p> <p>void Usage(){</p> <p>printf("Usage :schedule [-s] [-f] \n"); exit(1); </p> <p>}</p> <p>int main(int argc, char *argv[]){</p> <p>/<em>getopt variablen</em>/ int opt; int index;</p> <p>int sflag = 0; int fflag = 0;</p> <p>char *svalue; char *fvalue;</p> <p>int sseconds = ssec; int fseconds = fsec;</p> <p>char *reactor; char *emergency; char *logfile;</p> <p>/**/ char *endptr; /<em>Pipe and child</em>/</p> <p>int pipepc1[2]; int pipepc2[2];</p> <p>int child1_fd; int child2_fd;</p> <p>FILE *log; FILE *pipe_reader;</p> <p>char *p;<br> char *buf; int waitFc; int status; /<em>prgm</em>/ int prg_r; int prg_e;</p> <p>/<em>termination</em>/ int cnt = 0; int start_period; p = malloc(fgbuf * sizeof(char)); buf = malloc(fgbuf * sizeof(char));</p> <p>(void)signal(SIGINT,chld_handler);</p> <p>if(argc &lt; 4){</p> <pre><code>Usage(); </code></pre> <p>}else if(argc == 4){</p> <pre><code>reactor = argv[1]; emergency = argv[2]; logfile = argv[3]; </code></pre> <p>}else if(argc > 4){</p> <pre><code>/*argumentenbehandlung*/ while((opt = getopt(argc,argv,":s:f:")) != -1){ printf("sflag %d fflag %d \n",sflag,fflag); printf("opt %c \n", opt); printf("optind %d \n ",optind); switch(opt){ case 's': if(sflag == 0){ sflag++; svalue = optarg; }else{ fprintf(stderr,"Widerholung der option -s\n"); Usage(); } break; case 'f': if(fflag == 0){ fflag++; fvalue = optarg; }else { fprintf(stderr,"Widerholung der option -f\n"); Usage(); } break; case ':' : fprintf(stderr,"Option -%c brauch ein argument\n",optopt); Usage(); break; case '?' : fprintf(stderr,"Nicht bekannte option -%c \n",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("%d ssec\n", sseconds); } if(fflag){ fseconds = (int)strtol(fvalue,&amp;endptr,decbase); printf("%d fsec\n", fseconds); } </code></pre> <p>}</p> <p>/<em>pipeing</em>/</p> <p>if(pipe(pipepc1) == -1){</p> <pre><code>fprintf(stderr,"Fehler bei der Erzeugung der pipe\n"); exit(1); </code></pre> <p>}else {printf("Pipe created\n");}</p> <p>/<em>erzeuge child1</em>/ child1_fd = fork();</p> <p>if(child1_fd &lt; 0){</p> <pre><code>fprintf(stderr,"Fehler beim asfuehren von fork\n"); exit(0); </code></pre> <p>}</p> <p>if(child1_fd == 0){ printf("<strong>CHILD</strong>\n"); /<em>close pipe read</em>/ if(close(pipepc1[0]) == -1){</p> <pre><code> fprintf(stderr,"Konnte nicht das Read-Ende vom pipepc1 schliessen\n"); exit(1); } if(close(1) == -1){ fprintf(stderr,"Konnte nicht das Read-Ende vom pipepc1 schliessen\n"); exit(1); } if(dup(pipepc1[1]) !=STDOUT_FILENO){ fprintf(stderr,"Das setzen des Read-Endes als stdout is Fehlgeschlagen\n"); 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,"",NULL); //printf("prg_r ist %d \n", prg_r); } if(close(pipepc1[1]) == -1){ fprintf(stderr,"Das neue stdout konnte nich geschlossen werden\n"); exit(1); } </code></pre> <p>}else{</p> <pre><code>printf("**PARENT**\n"); log = fopen(logfile,"w"); /*schliesse pipe read*/ close(pipepc1[1]); pipe_reader = fdopen(pipepc1[0],"r"); while((buf = fgets(p,fgbuf,pipe_reader)) != NULL){ printf("from Child : %s \n",buf); fflush(pipe_reader); } fclose(log); waitFc= waitpid(child1_fd,&amp;status,0); if(waitFc == -1){ fprintf(stderr,"Das Warten ist fehlgeschlagen\n"); exit(1); } printf("child is done\n und waitFc = %d\n und satus %d",waitFc,status); fclose(pipe_reader); close(pipepc1[1]); </code></pre> <p>}</p> <p>printf("argc = %d \n", argc); exit(0); </p> <p>} </code></p> <p>and the reactor program : <code></p> <p> #include &lt;stdio.h> #include &lt;stdlib.h> #include &lt;time.h> </p> <p>int main(){</p> <p>srand(time(NULL)); int i; int s;</p> <code>s = rand() % 7; /*printf("%d \n",s);*/ if(s != 6){ printf("OK\n"); fflush(stdout); }else { printf("PRESSURE TOO HIGH - IMMEDIATE SHUTDOWN REQUIRED"); exit(EXIT_FAILURE); </code></pre> <p>}</p> <p>}</p> <p></code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload