c - 通过 pipe() 从子进程接收到正确结果后程序挂起

标签 c pipe fork

我正在拆分一个文件,通过 pipe() 发送,让 child 找到他们指定的文件部分的总和,通过 pipe() 将计算出的总和返回给父级,并让父级计算子级总和的总和。

我有一个工作程序。我的问题是它在收到显示正确的最终值后挂起。

我不确定我在做什么让父级期望更多信息,但我敢打赌这与包含我的子代码的 for() 循环 有关。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

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

int i, j, len, fpos=0, val, count=0, total=0, alltotal=0;
pid_t pid;
int nums = 1000;
FILE * file;

printf("How many children to use: ");
scanf("%d", &numchild);
printf("\nWill use %d child process(es).\n", numchild);

int fd[2*numchild][2]; //parent+child pipe

// create all pipes
for (i=0; i<2*numchild; i++)
{
    pipe(fd[i]);
}

for (i=0; i<numchild; i++)
{
    if((pid = fork()) == 0) // child process
    {
        pid = getpid();

        // read from parent
        len = read(fd[i][0], &fpos, sizeof(fpos));
        if (len > 0)
        {
            file = fopen("file1.dat", "r");
            fseek (file, fpos, SEEK_SET);
            count = 0;
            total = 0;

            printf("Child(%d): Recieved position: %d\n", pid, fpos);

            // read from file starting at fpos
            // add values read to a total value
            while (count < (nums/numchild))
            {
                fscanf(file, "%i", &val);
                total += val;
                count++;
            }
            //write to parent
            write(fd[i+numchild][1], &total, sizeof(total));
            printf("Child(%d): Sent %d to parent.\n", pid, total);
        }
        else
        {
            printf("Child(%d): Error with len\n", pid);
        }

        _exit;
    }

    // parent process
    pid = getpid();

    fpos = ((i*nums*5)/numchild); // 5 is the offset of the file values

    // write to child process
    printf("Parent(%d): Sending file position to child\n", pid);
    write(fd[i][1], &fpos, sizeof(fpos));

    // wait for child responce
    len = read(fd[i+numchild][0], &total, sizeof(total));
    if (len > 0)
    {
        printf("Parent(%d): Recieved %d from child.\n", pid, total);
        alltotal += total;
        printf("Parent(%d): Total: %d\n", pid, alltotal);
    }
    else
    {
        printf("Parent(%d): Error with len\n", pid);
    }
}
}

最佳答案

我不能问更多的问题,但如果这是在 Linux 或类 Unix 系统上(可能都是 posix):

您必须为主程序中的每个子进程执行wait(man 2 wait),否则您将创建僵尸进程。

不知道您在什么环境中运行,因此我无法对此进行测试以确定这是否是您无法正常退出的原因。

此外(这更像是一条评论),在循环的每个循环中,您都会 fork 一个 child ,然后向其提供数据,然后获得响应,然后打印总数。这真的是你想要做的吗?如果您一次只运行一个 child ,则无需创建一堆管道。

我的猜测是您想要一些实际的并发。您可以通过创建一个循环来创建所有子项,然后是另一个向它们提供数据的循环,然后是查看结果并对结果求和的第三个循环,然后是等待每个子项终止的第四个循环(以避免僵尸)。

我会考虑使用 pollselect 来读取返回值——毕竟,不能保证 children 会按顺序完成。

关于c - 通过 pipe() 从子进程接收到正确结果后程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950654/

相关文章:

在 c 中创建动态数组在 realloc() 时出错

c - Flex/Bison EOF 从标准输入与文件传播

c - 为什么我从对等方接收到的数据与预期输出不匹配?

php - Apache 不想在 FIFO(管道)中 fopen 或 fwrite

c - C 中的多管道

C管道: Bad file descriptor

c - ARM Keil 错误 #17 预期为 "]"、C3910W : Old syntax

c++ - (C++, Windows) 在没有控制台窗口的情况下生成子进程(使用 CreateProcess)

linux - 管道打开 '|' 和 '|-' 之间的区别(安全管道打开)

php - 在后台运行一个ffmpeg进程