C语言 Fork 父子通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14170647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fork parent child communication
提问by Emil Grigore
I need some way for the parent process to communicate with each child separately.
我需要某种方式让父进程分别与每个子进程通信。
I have some children that need to communicate with the parent separately from the other children.
我有一些孩子需要与其他孩子分开与父母沟通。
Is there any way for a parent to have a private communication channel with each child?
有没有办法让父母和每个孩子都有一个私人的沟通渠道?
Also can a child for example, send to the parent a struct variable?
例如,孩子也可以向父母发送一个结构变量吗?
I'm new to these kind of things so any help is appreciated. Thank you
我对这些东西很陌生,所以任何帮助都值得赞赏。谢谢
回答by cmc
(I'll just assume we're talking linux here)
(我只是假设我们在这里谈论的是 linux)
As you probably found out, fork()itself will just duplicate the calling process, it does not handle IPC.
正如您可能发现的那样,fork()它本身只会复制调用过程,它不处理IPC。
From fork manual:
fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent.
来自前叉手册:
fork() 通过复制调用进程来创建一个新进程。新进程称为子进程,它与调用进程完全相同,称为父进程。
The most common way to handle IPC once you forked() is to use pipes, especially if you want "a private comunication chanel with each child". Here's a typical and easy example of use, similar to the one you can find in the pipemanual (return values are not checked):
在 forked() 之后处理 IPC 的最常见方法是使用管道,特别是如果您想要“与每个孩子都有一个私人通信渠道”。这是一个典型且简单的使用示例,类似于您在pipe手册中可以找到的示例(不检查返回值):
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char * argv[])
{
int pipefd[2];
pid_t cpid;
char buf;
pipe(pipefd); // create the pipe
cpid = fork(); // duplicate the current process
if (cpid == 0) // if I am the child then
{
close(pipefd[1]); // close the write-end of the pipe, I'm not going to use it
while (read(pipefd[0], &buf, 1) > 0) // read while EOF
write(1, &buf, 1);
write(1, "\n", 1);
close(pipefd[0]); // close the read-end of the pipe
exit(EXIT_SUCCESS);
}
else // if I am the parent then
{
close(pipefd[0]); // close the read-end of the pipe, I'm not going to use it
write(pipefd[1], argv[1], strlen(argv[1])); // send the content of argv[1] to the reader
close(pipefd[1]); // close the write-end of the pipe, thus sending EOF to the reader
wait(NULL); // wait for the child process to exit before I do the same
exit(EXIT_SUCCESS);
}
return 0;
}
The code is pretty self-explanatory:
代码是不言自明的:
- Parent forks()
- Child reads() from the pipe until EOF
- Parent writes() to the pipe then closes() it
- Datas have been shared, hooray!
- 父叉()
- 孩子从管道中读取()直到 EOF
- 父母将()写入管道然后关闭()它
- 数据已共享,万岁!
From there you can do anything you want; just remember to check your return values and to read dup, pipe, fork, wait... manuals, they will come in handy.
从那里你可以做任何你想做的事情;请记住检查您的返回值并阅读dup, pipe, fork, wait... 手册,它们会派上用场。
There are also a bunch of other ways to share datas between processes, they migh interest you although they do not meet your "private" requirement:
还有很多其他方法可以在进程之间共享数据,尽管它们不满足您的“私有”要求,但您可能会对它们感兴趣:
- shared memory "SHM", the name says it all...
- sockets, they obviously work as good if used locally
- FIFO fileswhich are basically pipes with a name
- 共享内存 "SHM",名字说明了一切......
- 套接字,如果在本地使用,它们显然也能正常工作
- FIFO 文件,基本上是带有名称的管道
or even a simple file... (I've even used SIGUSR1/2 signalsto send binary datas between processes once... But I wouldn't recommend that haha.) And probably some more that I'm not thinking about right now.
甚至是一个简单的文件......(我什至使用过 SIGUSR1/2信号在进程之间发送二进制数据一次......但我不建议这样做哈哈。)而且可能还有一些我没有想到的现在。
Good luck.
祝你好运。

