C 多处理/管道

标签 c pipe multiprocessing

我刚刚学习 fork() 在 C 中的工作原理。 这个想法是生成 3 个子进程,每个子进程向父进程发送一些信息。

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

int main()
{
    int fd[2];
    int pid[3] = {0,0,0};
    int status = 0;


for (int i = 0; i < 3; i++)
{
    pipe(fd);

    pid[i] = fork();
    if (pid[i] < 0)
    {
        return -1;
    }
    if (pid[i] == 0)
    {
        close(fd[0]);
        char *arr = malloc(sizeof(char));
        sprintf(arr, "%i", i);
        write(fd[1], arr, 1);
        exit(0);
    }


}

for(int j=0; j < 3; j++)
{
    close(fd[1]);
    if (pid[j] > 0)
    {
        sleep(0);
        pid[j] = wait(&status);
        char *out = malloc(20 *sizeof(char));
        read(fd[0], out, 6);
        printf("%s\n", out);
        free(out);
        printf("I am the parent\n");

    }
}


}

预期的输出是:

1
I am the parent
2
I am the parent
3
I am the parent

实际输出是: 2 我是家长 2 我是家长 2 我是家长

为什么会发生这种情况?

最佳答案

几点:

  1. 您在第一个 pipe(fd) 循环中调用 for 三次,并且在创建三个管道时,您仅保存对其中一个管道的引用。因此,在第二个 for 循环中,您将从每次创建的第三个管道中读取数据。您应该有一个数组来存储对所有三个管道的引用。

  2. 您应该检查所有可能失败的系统调用的返回结果。由于上面的第 1 点,如果您执行此操作,close(fd[1]) 将在三分之二的情况下失败,并且它会提醒您出现了问题。检查系统调用的返回不仅是为了防止出现不太可能发生的错误,而且还可以帮助您进行调试,因为它们失败的最可能原因是您做错了什么,就像这里的情况一样。

  3. 这里绝对不需要使用 malloc() - 常规的 char 数组就可以了。此外,当您需要至少两个字符(即单个数字和终止空字符)时,malloc(sizeof(char)); 会为一个字符分配空间。此外,您应该始终检查 malloc() 的返回,因为它可能会失败。另外,根据定义,sizeof(char) 始终是 1,因此它总是多余的。

  4. 要获得所需的输出,您应该将 1 添加到 i ,否则 i 将是 0 ,然后是 1 ,然后是 2 ,但您的示例输出表明您想要 1 ,然后是 23

  5. waitpid()wait() 更好,因为您想等待特定进程。同样,当您使用它时,您的 sleep() 调用是多余的。

  6. 虽然在退出之前没有必要在此处对管道进行 close(),但有时这样做会很有帮助,因为如果您做错了什么,它可能会引起您注意。

    <
  7. 您的 if (pid[j] > 0) 检查是不必要的,因为如果 fork() 失败或者它是 0 ,您已经终止,所以您已经知道当您到达这里时它将大于 0

  8. 如果您不打算使用 status 变量来检索进程的退出状态,则不需要它 - 您只需将 NULL 传递到 wait()waitpid() 即可。

  9. 小问题,但您不需要为第二个 j 循环使用不同的变量名称(即 for ),因为第一个 i 循环中 for 的范围仅限于该循环。如果您打算使用 i 作为循环计数器的通用名称,那么您不妨在任何可以使用的地方使用它。

  10. return EXIT_FAILUREreturn -1 更好,并且当您检索退出状态时,该值将转换为 255

  11. 这在现实中不太可能成为问题,但 fork() 返回类型 pid_t ,而不是类型 int ,因此最好将 pid 设为该类型的数组。

这是一些修改后的代码:

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

int main(void)
{
    int fd[3][2];
    pid_t pid[3];

    for ( int i = 0; i < 3; ++i ) {
        if ( pipe(fd[i]) == -1 ) {
            perror("pipe() failed");
            return EXIT_FAILURE;
        }

        if ( (pid[i] = fork()) == -1 ) {
            perror("fork() failed");
            return EXIT_FAILURE;
        }
        else if ( pid[i] == 0 ) {
            if ( close(fd[i][0]) == -1 ) {
                perror("close() failed");
                return EXIT_FAILURE;
            }

            char arr[100];
            sprintf(arr, "%d", i + 1);

            if ( write(fd[i][1], arr, 1) == -1 ) {
                perror("write() failed");
                return EXIT_FAILURE;
            }

            if ( close(fd[i][1]) == -1 ) {
                perror("close() failed");
                return EXIT_FAILURE;
            }

            return EXIT_SUCCESS;
        }
        else {
            if ( close(fd[i][1]) == -1 ) {
                perror("close() failed");
                return EXIT_FAILURE;
            }
        }
    }

    for ( int i = 0; i < 3; ++i ) {
        if ( waitpid(pid[i], NULL, 0) == -1 ) {
            perror("waitpid() failed");
            return EXIT_FAILURE;
        }

        char out[100] = {0};
        if ( read(fd[i][0], out, 99) == -1 ) {
            perror("read() failed");
            return EXIT_FAILURE;
        }

        printf("%s\nI am the parent\n", out);

        if ( close(fd[i][0]) == -1 ) {
            perror("close() failed");
            return EXIT_FAILURE;
        }
    }

    return EXIT_SUCCESS;
}

输出:

paul@horus:~/src/sandbox$ ./pipe
1
I am the parent
2
I am the parent
3
I am the parent
paul@horus:~/src/sandbox$ 

关于C 多处理/管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28289157/

相关文章:

c++ - PeekNamedPipe 失败,但 GetLastError 返回 0

python - 使用多处理池的并行处理循环

c - Turbo C 3.0 DOS 框中程序异常终止

c - 如何在 C 中只返回字符串的值?

c - 使用链接列表将文件中的单词读取到动态字符中

macos - Bash:管道输出到后台进程?

c++ - 当数组大小大于 1,000,000 时,Cuda 未给出正确答案

使用命名管道和信号量在两个进程之间进行通信

python - 在 Python 多处理失败的子进程中创建子进程

python - 使用multiprocessing访问mysql时,总是报如下错误,如何解决?