1

父进程创建 N 个子进程,每个子进程都用 exec 替换自己。parent 和 exec 之间通过管道数组(int pipefd[N][2];)进行通信

exec使用以下命令写入管道:

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

父母读到这些:

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]);
}

现在的问题是,只有在子进程终止后,读取才会解除阻塞并打印缓冲区。我想要做的是在 exec 写入管道后立即打印缓冲区。

以下是所有代码:

父文件:

#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);
                }
            }

        }
    }
}

计算.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);


        }
    }

}
4

3 回答 3

1

恕我直言,您的设计并不是很好,因为所有子进程都继承了您创建的所有管道,这是对系统资源的浪费。正确的做法是:

在父进程中:

  1. dupfd 的#0 和#1 保留供父进程以后使用;在这些新的 fd 上使用fcntlwithF_DUPFD_CLOEXEC来防止继承exec

  2. closefd #0

  3. closefd #1

  4. 创建管道

  5. 如上所述,防止继承管道的读取 fd

  6. dup2写入管道的 fd 使其成为 fd #1;close原来写fd

  7. fork&exec子进程

  8. 为所有必要的孩子重复步骤 3 到 7

  9. dup2将原始 fd 的 #0 和 #1 的副本存储回 #0 和 #1 以恢复printf/scaf功能

  10. 用于select轮询读取所有管道的 fd,如果您希望在 #0 上有任何输入,则可能是 #0

如果需要双向通信,则在第 4 步创建两个管道,并对所述过程进行适当调整,然后重复第 2 步到第 7 步以创建子节点。

在子进程中(之后exec

按要求进行所有处理。使用 fd #1 或 printf 或其他方式写入父级。PID子进程可能总是获得它的父进程getppid()

于 2012-09-30T13:34:04.707 回答
1

对此进行故障排除的一般提示:使用该工具运行管道的两侧strace(您可以使用它strace -f来跟踪分叉),以便您可以验证实际从管道写入/读取的内容。

我怀疑在这种情况下,孩子没有写任何东西!这是因为您使用的 stdio 层(printf())检查它是否正在写入终端或其他任何东西。在终端的情况下,它会在每个换行符之后刷新输出,但在其他情况下,它仅在写入大块数据后才会刷新(GNU 系统上为 8KiB)。尝试在printf( )之后使用fflush()手动刷新输出。如果有帮助,您可以使用setvbuf()调整标准输出缓冲模式。

于 2012-09-30T11:55:40.453 回答
0

您的操作系统可能正在缓冲写入。

在调用. ioctl_ 此外,请检查您的返回值,以防发生一些阴险的事情。有关更多信息,请参见http://pubs.opengroup.org/onlinepubs/7908799/xsh/ioctl.htmlFLUSHWwriteioctl

于 2012-10-01T01:59:53.610 回答