【问题标题】:C multiprocessing/pipesC多处理/管道
【发布时间】:2015-02-03 00:07:11
【问题描述】:

我只是在学习 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 我是家长

为什么会这样?

【问题讨论】:

  • 每次通过您的第一个for 循环时,您调用pipe(fd) 并丢失您最后一次围绕循环创建的管道的描述符。三个子进程需要三个管道,这里不是一个。您应该检查 all 系统调用的返回值,而不仅仅是 fork() - close(fd[1]) 如果有的话,会向您大喊大叫。
  • 我认为通过在循环内调用 pipe(fd) 可以创建 3 个管道。如果这失败了,那么我该如何制作 3 个管道?
  • @user3614293 创建一个 3x2 数组来保存 3 对文件描述符,就像您对 pids 所做的那样。
  • 正在创建三个管道,但您只保留对最后一个管道的引用。您应该执行int fd1[2], fd2[2], fd3[2](或int fd[3][2])之类的操作,然后分别对它们中的每一个调用pipe()
  • 谢谢,会试试的。

标签: c pipe multiprocessing


【解决方案1】:

你在打电话

pipe(fd);

在第一个 for 循环中多次。考虑在循环之前移动它,因为每次调用它时它都会返回一对新的文件描述符用于读取/写入。

另外,close(fd[1]) 只能从父进程调用一次。

【讨论】:

    【解决方案2】:

    几点:

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

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

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

    4. 1234563你想要1,然后是2,然后是3
    5. waitpid()wait() 好,因为你想等待一个特定的进程。同样,当您使用它时,您的sleep() 调用也是多余的。

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

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

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

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

    10. return EXIT_FAILURE 优于 return -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$ 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 2012-01-17
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2017-07-16
      相关资源
      最近更新 更多