【发布时间】:2020-03-15 01:20:08
【问题描述】:
我正在程序 y 中创建一个带有 fork 的子进程。在那个孩子身上,我用 exec 运行另一个程序,我希望那个程序中的函数(我们称之为程序 x)返回一些东西给我。有没有办法将此返回值传递给父级? 我提供了某种伪代码来演示我想要在下面做什么。
程序.x:
int main(int argc, char** argv)
{
if(argc != 2)
{
printf("argument count does not match\n");
return -1;
}
printf("task1!\n");
...
char *value = "want this"; // how to pass this to the parent in the program y?
...
}
程序 y:
int main(int argc, char *argv[])
{
int fd[2];
pipe(fd);
pid_t p;
p = fork();
if(p==-1)
{
printf("There is an error while calling fork()");
}
if(p==0)
{
printf("We are in the child process\n");
printf("Calling hello.c from child process\n");
char *args[] = {"Hello", "C", "Programming", NULL};
execv("./hello", args);
close(fd[0]);
write(fd[1], ???, ??);
close(fd[0]);
}
else
{
printf("We are in the parent process");
wait(NULL);
close(fd[1]);
read(fd[0], ???,???);
close(fd[0]);
}
return 0;
}
【问题讨论】:
标签: c operating-system pipe fork exec