如何在C中制作父子双向管道

时间:2018-11-15 05:50:06

标签: c pipe fork

我正在尝试建立双向管道,父级向子级发送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;}

1 个答案:

答案 0 :(得分: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)不再重要。