加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 19:02:53 所属栏目:安全 来源:网络整理
导读:看看下面的代码: #includeunistd.h#includestdlib.h#includestdio.h#includestring.h#includesys/types.hmain() { int pipdes[2]; char buff[50]; const char parent[]="Parent Writes. Child Readsn"; const char child[]="Child Writes. Parent Readsn"
看看下面的代码:
#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 Readsn";
    const char child[]="Child Writes. Parent Readsn";
    if(pipe(pipdes)==0) {
        pid_t pid=fork();
            if(pid<0)
                    printf("Errorn");
        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],50);
            printf("Child: %s",buff);
        }
    }
    else
        printf("Error in pipen");
}

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

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

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

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

如果您希望数据在两个方向上流动,则可以使用两对管道.我们称之为parent_pipe和child_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 Readsn";
        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);
            // NOTE: `strlen(child)` doesn't include the nul at the end!
            write(out,strlen(child) + 1);
        }
    }
    else {
        // this is the parent process
        const char parent[]="Parent Writes. Child Readsn";
        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,strlen(parent) + 1);
            read(in,buff);
        }
    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读