【问题标题】:How to make parent and child bidirectional pipe in C如何在C中制作父子双向管道
【发布时间】:2018-11-15 05:50:06
【问题描述】:

我正在尝试做一个双向管道,父母向孩子发送 n 个数字(int),孩子将它们加倍返回。我无法弄清楚我的错误是什么? 我扫描了数字 n 是父母,通过 fd1[1] 发送它,然后继续发送那些 n 数字让孩子加倍。 在孩子身上,我读了数字 n,然后对于我读到的每个数字,我加倍并发回。

int main(){
    int pid,n,c,p,k,nbread;
    char buf1[2], buf2[2];
    int fd1[2], fd2[2];
    pipe(fd1);
    pipe(fd2);
    pid=fork();
    if(pid==0){
        close(fd1[1]);
        close(fd2[0]);
        read(fd1[0],buf2,sizeof(int));
        n = atoi(buf2);
        for(int i = 0; i<n;i++){
            nbread = read(fd1[0],buf2,sizeof(int));
            sleep(3);
            if(nbread == -1)
            exit(1);
            c = atoi(buf2);
            c = c*2;
            sprintf(buf2,"%d",c);
            write(fd2[1],buf2, sizeof(int));
        }
        close(fd1[0]);
        close(fd2[1]);
    }
    close(fd1[0]);
    close(fd2[1]);
    printf("Enter integer: ");
    scanf("%d",&p);
    sprintf(buf1,"%d",p);
    write(fd1[1],buf1,sizeof(int));
    sleep(3);
    for(int i=0;i<n;i++){
        sprintf(buf1,"%d",i);
        write(fd1[1],buf1,sizeof(int));
        read(fd2[0],buf1,sizeof(int));
        printf("number is: %s",buf1);
    }
    close(fd1[1]);
    close(fd2[0]);
    wait(NULL);
    return 0;}

【问题讨论】:

  • 你做过调试吗?
  • “双向管道”不同于使用两个管道在两个进程之间进行双向通信。
  • @benc 输出:输入整数:3 输入整数:
  • 您需要让子代码退出,而不是同时尝试执行父代码。如果你使用函数会更容易——be_childish()be_parental() 是我通常使用的名称,因为它们的长度相同。您的缓冲区(buf1buf2)很小;只对个位数的数字足够大。好的,如果你真的打算输入,但非常脆弱(并且不必要地脆弱)。
  • 第二个Enter integer 是因为子进程在关闭其管道后继续进入父代码。

标签: c pipe fork


【解决方案1】:

修复父循环以测试 p 而不是 n 修复了主要问题。确保缓冲区足够大也是一个好主意。写入整个缓冲区是可以的,但不一定是理想的。

此代码有效;它有更多的调试输出。

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

int main(void)
{
    int pid, n, c, p, k, nbread;
    char buf1[12], buf2[12];
    int fd1[2], fd2[2];
    pipe(fd1);
    pipe(fd2);
    pid = fork();
    if (pid == 0)
    {
        close(fd1[1]);
        close(fd2[0]);
        read(fd1[0], buf2, sizeof(buf2));
        n = atoi(buf2);
        printf("Child read %d\n", n);
        for (int i = 0; i < n; i++)
        {
            printf("child dozes...\n");
            sleep(3);
            printf("child wakes...\n");
            nbread = read(fd1[0], buf2, sizeof(buf2));
            if (nbread == -1)
            {
                fprintf(stderr, "child exits after read failure\n");
                exit(1);
            }
            c = atoi(buf2);
            c = c * 2;
            sprintf(buf2, "%d", c);
            write(fd2[1], buf2, sizeof(buf2));
            printf("Child wrote [%s]\n", buf2);
        }
        close(fd1[0]);
        close(fd2[1]);
        printf("Child done\n");
        exit(0);
    }
    else
    {
        close(fd1[0]);
        close(fd2[1]);
        printf("Enter integer: ");
        scanf("%d", &p);
        sprintf(buf1, "%d", p);
        write(fd1[1], buf1, sizeof(buf1));
        printf("Parent wrote [%s]\n", buf1);
        printf("parent dozes...\n");
        sleep(3);
        printf("parent wakes...\n");
        for (int i = 0; i < p; i++)
        {
            sprintf(buf1, "%d", i);
            write(fd1[1], buf1, sizeof(buf1));
            printf("parent wrote [%s]\n", buf1);
            read(fd2[0], buf2, sizeof(buf2));
            printf("number is: %s\n", buf2);
        }
        close(fd1[1]);
        close(fd2[0]);
        wait(NULL);
    }
    return 0;
}

样本输出:

Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6

代码将子代码和父代码放入单独的ifelse 块中。它不会检测到 pipe()fork() 中的故障,这是次优的。孩子exit(0) 不再重要了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多