【问题标题】:How to write a value to parent process from child process?如何从子进程向父进程写入值?
【发布时间】:2017-02-24 03:40:37
【问题描述】:
#include <stdio.h>
#include <unistd.h>

int fd[2];
int check = pipe(fd);

int main()
{
    pid_t pid;
    int read_int=0;

    pid=fork();

    if(pid==0)
    {
        execl("./getvalue.out","getvalue.out",(char *)0);
    }
    else
    {
        close(fd[1]);
        read(fd[0],&read_int,sizeof(read_int));
        printf("Receive: %d\n",read_int);
        close(fd[0]);
    }
    wait(0);
}

这是getvalue.cpp:(用于创建getvalue.out

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

int main(void)
{
    int fd[2];
    int write_int=100;
    close(fd[0]);
    write(fd[1],&write_int,sizeof(write_int));
    close(fd[1]);
}

所以read_int 始终是0,而不是使用execl() 从子进程接收的100 的值。我猜问题是他们没有共享管道?

【问题讨论】:

标签: c linux pipe


【解决方案1】:

您的 getvalue 代码的 fd[] 不再指向管道。将父进程中的 fd[1] 值作为命令行参数传递给子进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 2012-03-04
    • 2013-07-18
    • 2014-10-08
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多