C Aus Pipe-Blöcken lesen, bis das Kind beendet ist

Der übergeordnete Prozess erstellt N untergeordnete Elemente, von denen jedes durch exec ersetzt wird. Es besteht eine Kommunikation zwischen Parent und Exec über ein Array von Pipes (int pipefd [N] [2];)

Dasexec schreibt mit diesen Befehlen in die Pipe:

char msg[50];
sprintf( msg, "\tsent from pid: %d, pi= %f", getpid(), pi);
printf("%s\n",msg);
write(i1, msg, strlen(msg)+1);

und der Elternteil liest mit diesen:

for (i=0;i<N;i++) {
   close(pipefd[i][1]);  // close the write end of the pipe in the parent

   read(pipefd[i][0], buffer, sizeof(buffer));
   printf("\n-C-\n");

   if (buffer[0] == '\t'){
     printf("%s\n",buffer);
   }
   int j;
   for (j=0; j<100; j++) {
      buffer[j]='\n';
   }
   close(pipefd[i][0]);
}

Jetzt ist das Problem dasnur Nachdem das untergeordnete Element beendet wurde, wird der Lesevorgang freigegeben und der Puffer gedruckt. Ich möchte den Puffer sofort drucken, nachdem der Exec in die Pipe geschrieben hat.

Unten ist der ganze Code:

Übergeordnete Datei:

#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>

#define N 5
pid_t *pid;
int pipefd[N][2];

int flag = 0;
int count_ctrl_c = 0;
void signal_handler(int sig){
    signal(sig, SIG_IGN);
    printf("\n");

    flag = 1;
    signal(SIGINT, signal_handler);
}

int main(int argc, char *argv[]) {


    int i;
    for (i = 0; i < N; i++) {
        pipe(pipefd[i]);
    }

    int parent_pid = getpid();
    pid= (pid_t *)malloc(N * sizeof(pid_t));

    for (i = 0; i < N; i++) {
        pid[i] = fork();
        if (pid[i] == 0) //The parent process will keep looping
        {

            char b[50];
            sprintf( b, "%d", i+1);

            char i0[50];
            sprintf( i0, "%d", pipefd[i][0]);

            char i1[50];
            sprintf( i1, "%d", pipefd[i][1]);

            char par_id[50];
            sprintf( par_id, "%d", parent_pid);

            execl("***the/path/to/exec/calculate***", b,i0,i1,par_id,NULL);
        }
    }

    if (parent_pid == getpid()) {
        signal(SIGINT, signal_handler);


        while(1){
            if (flag){

                printf("\n-A-\n");
                char buffer[100];
                int i;
                for (i=0;i<N;i++) {
                    // Apostellei to shma SIGUSR2 se ola ta paidia tou
                    kill(pid[i], SIGUSR2);
                }

                printf("\n-B-\n");
                for (i=0;i<N;i++) {
                    close(pipefd[i][1]);  // close the write end of the pipe in the parent

                    read(pipefd[i][0], buffer, sizeof(buffer));
                    printf("\n-C-\n");
                    if (buffer[0] == '\t'){
                        printf("%s\n",buffer);
                    }
                    int j;
                    for (j=0; j<100; j++) {
                        buffer[j]='\n';
                    }
                    close(pipefd[i][0]);
                }
                //exit(0);
                printf("finished reading\n");

                flag = 0;
                count_ctrl_c++;
                if (count_ctrl_c == 2) {
                    exit(0);
                }
            }

        }
    }
}

berechne.c

#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>

#define N 5

int i0,i1,parent_pid;

int flag = 0;
int time_to_term = 0;
double pi;

void signal_handler2(int sig);
void signal_handler(int sig);

void signal_handler2(int sig){
    signal(sig, SIG_IGN);
    signal(SIGALRM, SIG_IGN);
    flag = 1;
    signal(SIGUSR2, signal_handler2);
    signal(SIGALRM, signal_handler);
}

void signal_handler(int sig){
    signal(sig, SIG_IGN);
    pid_t pid = getpid();
    printf("time: %d, pid: %d, pi: %1.10f\n", time_to_term, pid, pi);
    exit(0);
}

int main(int argc, char *argv[]) {
    int pid;
    signal(SIGINT, SIG_IGN);
    signal(SIGUSR2, signal_handler2);
    signal(SIGALRM, signal_handler);

    time_to_term = atoi(argv[0]);   
    alarm(time_to_term);

    i0 = atoi(argv[1]);
    i1 = atoi(argv[2]);
    parent_pid = atoi(argv[3]);

    double mul = 1.0;
    double par = 2.0;
    pi = 3.0;

    while(1){


        pi = pi + (mul * (4.0 / (par * (par + 1.0) * (par + 2.0))));
        mul = mul * (-1.0);
        par += 2;

        sleep(1);

         if (flag) {
            signal(SIGALRM, SIG_IGN);
            close(i0);

            char msg[50];
            sprintf( msg, "\tsent from pid: %d, pi= %f", getpid(), pi);
             printf("%s\n",msg);
            write(i1, msg, strlen(msg)+1);

            close(i1);
            flag = 0;

            signal(SIGALRM, signal_handler);
            //exit(0);


        }
    }

}

Antworten auf die Frage(3)

Ihre Antwort auf die Frage