UNIX中的管道可以双向工作吗?

Utu*_*mbu 8 c unix pipe

看看下面的代码:

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

main() {
    int pipdes[2];
    char buff[50];
    const char parent[]="Parent Writes. Child Reads\n";
    const char child[]="Child Writes. Parent Reads\n";
    if(pipe(pipdes)==0) {
        pid_t pid=fork();
        if(pid<0)
              printf("Error\n");
        if(pid==0){
            read(pipdes[0],buff,50);
            printf("Parent: %s",buff);
            write(pipdes[1], child, strlen(child));
            exit(0);
        }
        else if(pid>0) {
            write(pipdes[1], parent, strlen(parent));
            wait(pid);
            read(pipdes[0], buff, 50);
            printf("Child: %s", buff);
        }
    }
    else
        printf("Error in pipe\n");
}
Run Code Online (Sandbox Code Playgroud)

现在,我在这里创建了一个管道,但这两个进程都可以读写.管道不应该是单向的.此外,当我把传统的'close(pipdes [0])'用于父级和'close(pipdes [1])'用于子级时,代码不起作用,尽管我添加了open(pipdes [0])函数后来.

我对UNIX和管道的概念仍然是原始的,所以我可能在这里有点蹩脚,但请你协助.

cHa*_*Hao 21

在某些系统上,管道可以是双向的.但它们并非必须如此,并且任何假设它们都是不可移植的.特别是,它们不在Linux上.

实际上,您的代码存在问题 - 两个进程都试图读取和写入同一个管道.管道的预期用途是孩子写,父母读,反之亦然.你现在正在做的事情目前适用于你,因为你正在阅读和写作一次和wait孩子.但是当你在尝试以你正在做的方式做事时循环,你不能wait- 并且没有同步,孩子将经常(但不总是!)最终阅读它打算发送给父母的东西,而副反之亦然.

如果您希望数据在两个方向上流动,则可以使用两对管道.让我们把它们parent_pipechild_pipe.父母将读取parent_pipe[0]和写入child_pipe[1],孩子将读取child_pipe[0]和写入parent_pipe[1].

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

int main() {
    int parent_pipe[2];
    int child_pipe[2];
    char buff[50];

    if(pipe(parent_pipe) || pipe(child_pipe)) {
        perror("pipe(...)");
        exit(1);
    }

    // As noted elsewhere, you're using `fork()` incorrectly.
    // `fork()` returns 0 to the child, and a pid to the parent, or -1 if an error
    // occurs.
    int pid = fork();
    if (pid == -1) {
        perror("fork()");
        exit(1);
    }

    if (pid == 0) {
        // this is the child process.  read from child_pipe, write to parent_pipe
        const char child[]="Child Writes. Parent Reads\n";
        int in, out;
        in = child_pipe[0];
        // in = parent_pipe[0];  // uncomment me to test with one pipe pair
        out = parent_pipe[1];

        for (int i = 0; i < 10; ++i) {
            read(in,buff,50);
            printf("Parent: %s",buff);
            // NOTE: `strlen(child)` doesn't include the nul at the end!
            write(out, child, strlen(child) + 1);
        }
    }
    else {
        // this is the parent process
        const char parent[]="Parent Writes. Child Reads\n";
        int in, out;
        in = parent_pipe[0];
        out = child_pipe[1];
        // out = parent_pipe[1];  // uncomment me to test with one pipe pair

        for (int i = 0; i < 10; ++i) {
            write(out, parent, strlen(parent) + 1);
            read(in, buff, 50);
            printf("Child: %s", buff);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用一对创建的UNIX套接字socketpair(AF_LOCAL, SOCK_STREAM, 0, sockdes)(sockdes我们重命名pipdes的地方,因为它现在是套接字,而不是管道).孩子会读取和写入sockdes[0],父母会读取和写入sockdes[1].或相反亦然.


Klo*_*lox 6

在POSIX.1-2001中,管道是单向的。从手册页:

pipe()创建一对文件描述符,指向管道索引节点,并将它们放置在filedes指向的数组中。filedes [0]用于读取,filedes [1]用于写入。

顺便说一句,您对的使用fork是错误的:为父母和孩子fork返回。 表示有错误。pid>0pid==0pid<0