I've got a problem with my pipeline. I looked through the topics, but did not find anything for my problem.
My pipeline works fine, but I want to know when my children terminate. So I want to use waitpid to check my children. But that does not work.
My pipe:
int interpretiere_pipeline(Kommando k){
int pipe1[] = { -1, -1 };
int pipe2[] = { -1, -1 };
int status, pid, laenge, i;
Kommando command;
Liste l;
i = 0;
status = 0;
laenge = k->u.sequenz.laenge;
l = k->u.sequenz.liste;
printf("Anzahl der Elemente der Pipe: %i!\n", laenge);
while(i < laenge) {
pipe2[0] = pipe1[0];
pipe2[1] = pipe1[1];
command = (Kommando) listeKopf(l);
if (i > 0) {
close(pipe2[1]);
}
if (pipe(pipe1) < 0) {
perror("Pipe-Fehler");
exit(1);
}
/* fuehre Kindprozesse aus -> speichere pid */
pid = fork();
switch (pid) {
case -1:
/* Fehler bei Fork -> Abbruch */
perror("fork-Fehler");
exit(1);
case 0:
/* Leseströme umlegen auf vorherigen Prozess (außer beim ersten Prozess) */
if (pipe2[0] != -1) {
if(dup2(pipe2[0], STDIN_FILENO) == -1){
perror("dup2 failed\n");
exit(1);
}
}
/* Schreibströme umlegen auf nachfolgenden Prozess (außer beim letzten Prozess) */
if (i < laenge - 1) {
if(dup2(pipe1[1], STDOUT_FILENO) == -1){
perror("dup2 failed\n");
exit(1);
}
}
/* fuehre Kommandos aus */
interpretiere_einfach(command, 0);
exit(1);
break;
default:
/* schreibe Prozesse in die Struktur */
printf("Name : %s\n",command->u.einfach.worte[0]);
pGlobal = erzeugeProzess( pid, "test", "RUNNING");
/* fuege Struktur in Liste ein */
procTable = listeAnfuegen(procTable, &pGlobal);
/* Zaehler fue while-Schleife*/
i++;
sleep(2);
break;
}
l = listeRest(l);
}
return status;
}
My waitpid
waitpid(pid, &kind_status, 0);
if(WIFEXITED(kind_status)){
printf("Kind mit der PID %d wurde beendet\n",pid);
if (WEXITSTATUS(kind_status) == 0) {
/** setze status exit(0) */
/*printf("Kind erfolgreich\n");*/
p->status = "exit(0)";
}
/* nicht erfolgreiche ausgeführt */
else
{
/** setze status exit(1) */
/*printf("Kind nicht erfolgreich\n");*/
p->status = "exit(1)";
}
}
else if(WIFSIGNALED(kind_status)){
/*printf("Kind mit der PID %d wurde durch Signal abgebrochen. Signalnummer: %d\n",pid, WTERMSIG(kind_status));*/
p->status = "signal";
}
i = listeLaenge(procTable);
procTable = listeAnfuegen(procTable, p);
/*procTable = listeAnfuegen(procTable, &pGlobal);*/
if(listeLaenge(procTable) <= i) {
fprintf(stderr, "Fehler beim Schreiben in die Liste!");
}
can I place it under my pipe in a loop with the length of the pipe?