发送和通过管道接收信息

发送和通过管道接收信息

本文介绍了发送和通过管道接收信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地了解父母和多个子进程之间的管道,所以我做了一个简单的程序会派生两个子进程,让他们有一个值( I ),有他们改变该值,然后打印出来。

但它不工作,因为程序打印 I ,如果它是不变的,并打印修改 I 里面的孩子。我显然不能发送 I 通过正确的变量,所以我应该怎么解决这个问题?

INT主(INT ARGC,CHAR *的argv []){
    INT I = 0;
    INT pipefd [2];
    INT pipefd1 [2];
    管(pipefd);
    管(pipefd1);
    将为pid_t CPID;
    CPID = fork()的;
    CPID = fork()的;
    如果(CPID == 0)//这是孩子
    {
        关闭(pipefd [1]); //第一管的紧密写端
        关闭(pipefd1 [0]); //第二管道的关闭读端
        读(pipefd [0],&放大器I,的sizeof(ⅰ));
        I = I * 2;
        的printf(子进程I =%d个\\ N,I); //这个我打印高达20倍
        写(pipefd1 [1],&放大器I,的sizeof(ⅰ));
        关闭(pipefd [0]); //关闭管道的读端
        关闭(pipefd1 [1]);
        出口(EXIT_SUCCESS);
    }
    其他
    {
        关闭(pipefd [0]); //第一管的紧密读端
        关闭(pipefd1 [1]); //第二管道的关闭写端
        我= 10;
        写(pipefd [1],&放大器I,的sizeof(ⅰ));
        读(pipefd1 [1],&放大器I,的sizeof(ⅰ));
        的printf(%d个\\ N,I); //这个我打印10倍
        关闭(pipefd [1]);
        关闭(pipefd1 [0]);
        出口(EXIT_SUCCESS);
    }
}


解决方案

主要的问题是,你没有创建两个子进程。你创建三个。

  CPID = fork()的;
CPID = fork()的;

第一个中的一个子进程的结果被建立。在这一点上,无论是孩子和家长执行下一条语句,这也是一个。因此,父进程会创建一个新的孩子,第一个孩子也创建了一个孩子。这就是为什么一切都打印了两次。

您需要检查叉的返回值马上做别的事情了。

如果您要删除的一个电话,您还是会最终获得错误值i 父。这是因为它是从管道错误的结束阅读。

孩子被写入 pipefd1 [1] ,但随后的父母试图从 pipefd1 [1] 为好。它应该从 pipefd1是阅读[0]

编辑:

删除误差取样code这假定管道是双向的,而他们没有。

I'm trying to better understand pipes between a parent and multiple child processes, so I made a simple program that spawns two child processes, gives them a value (i), has them change that value, and then prints it out.

However it's not working, as the program prints i as if it was unaltered, and prints the altered i inside the children. I'm obviously not sending the i variable through correctly, so how should I fix this?

int main ( int argc, char *argv[] ){
    int i=0;
    int pipefd[2];
    int pipefd1[2];
    pipe(pipefd);
    pipe(pipefd1);
    pid_t cpid;
    cpid=fork();
    cpid=fork();
    if (cpid ==0)  //this is the child
    {
        close(pipefd[1]);   // close write end of first pipe
        close(pipefd1[0]);  // close read end of second pipe
        read(pipefd[0], &i, sizeof(i));
        i=i*2;
        printf("child process i= %d\n",i);  //this prints i as 20 twice
        write(pipefd1[1],&i, sizeof(i));
        close(pipefd[0]); // close the read-end of the pipe
        close(pipefd1[1]);
        exit(EXIT_SUCCESS);
    }
    else
    {
        close(pipefd[0]);   // close read end of first pipe
        close(pipefd1[1]);  // close write end of second pipe
        i=10;
        write(pipefd[1],&i,sizeof(i));
        read (pipefd1[1], &i, sizeof (i));
        printf("%d\n",i); //this prints i as 10 twice
        close(pipefd[1]);
        close(pipefd1[0]);
        exit(EXIT_SUCCESS);
    }
}
解决方案

The main problem is that you're not creating two child processes. You're creating three.

cpid=fork();
cpid=fork();

The first fork results in a child process being created. At that point, both the child and the parent execute the next statement, which is also a fork. So the parent creates a new child and the first child also creates a child. That's why everything is printing twice.

You need to check the return value of fork immediately before doing anything else.

If you were to remove one of the fork calls, you'd still end up with the wrong value for i in the parent. That's because it's reading from the wrong end of the pipe.

The child is writing to pipefd1[1], but the parent is then trying to read from pipefd1[1] as well. It should be reading from pipefd1[0].

EDIT:

Removed erroneous sample code which assumed pipes are bidirectional, which they are not.

这篇关于发送和通过管道接收信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:52