叉家长亲子沟通 [英] Fork parent child communication

查看:130
本文介绍了叉家长亲子沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些方法父进程分开每个孩子沟通。

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?

我是新来的这类事情,所以任何帮助AP preciated。谢谢

I'm new to these kind of things so any help is appreciated. Thank you

推荐答案

(我只是假设我们在这里谈论的Linux)

(I'll just assume we're talking linux here)

您可能已经发现了,叉()本身只是重复调用进程,它不处理的 IPC

As you probably found out, fork() itself will just duplicate the calling process, it does not handle IPC.

从叉手动:

fork()的创建通过复制调用进程的新进程。
  新的过程,称为子,是完全相同的副本
  主叫过程称为父

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.

处理IPC一旦你分叉()最常见的方式是使用管道,特别是如果你想要的每个孩子一个私人的交际夏奈尔。这里使用一个典型的简单的例子,类似于一个你可以在管道找到手动(返回值不检查):

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 pipe manual (return values are not checked):

   #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;
   }

在code是pretty不言自明的:

The code is pretty self-explanatory:


  1. 父叉()

  2. 孩子在读()从管直到EOF

  3. 家长写()来管然后关闭()它

  4. DATAS已经共享,万岁!

从那里,你可以做任何你想要的;只记得要检查您的返回值和读 DUP 管道 ...手册,他们会派上用场。

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.

还有一堆其他的方式来共享进程之间DATAS,他们migh你感兴趣,虽然他们不符合你的私人的要求:

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 files which are basically pipes with a name

甚至一个简单的文件...(我甚至用SIGUSR1 / 2 信号在进程间发送二进制DATAS一次......但我不会建议,哈哈。)
而且很可能多一些,我不是想现在。

or even a simple file... (I've even used SIGUSR1/2 signals to 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.

祝你好运。

这篇关于叉家长亲子沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆