父母与子女之间的沟通 [英] Communicating between a parent and its children

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

问题描述

新手问题:

在Unix上,在具有父级和一些子级的程序中:
-父母该如何有效地提醒孩子做一些工作呢?
-还是孩子们如何等待父母的信号开始做一些工作?

On Unix, in a program with a parent and some children:
- How can the parent alert the children efficiently to do some work.. ?
- Or how can the children wait for parent signal to start doing some work?


该程序尝试并行执行复杂的计算,我已经将共享内存用作所有子代的通用工作空间,以更新结果和进行数据传输.
我现在需要的是父母对所有孩子都说有效的开始"(多次)


This program tries to do a complex computation in parallel, I have already used shared memory as a common workspaces for all children to update results and for data transfer.
What I need now is the parent say "start" efficiently to all its children...(called many times)

谢谢

推荐答案

您的ipc标记说明了一切.您需要研究进程间通信:

Your ipc tag says it all. You need to look into inter-process communuication:

  • 共享内存.
  • 信号量.
  • 管道.
  • 信号.
  • 内存映射文件.
  • 套接字.

毫无疑问,还有其他可能性,但这是一个好的开始.

No doubt there are other possibilities but that's a good start.

每种方法的效率很大程度上取决于您的用例.如果您只需要通知孩子做某事,则可能是我要使用的信号.如果您需要在流程之间传递更多的信息,最好完全指定需求.

How efficient each is depends pretty much on your use case. If you only need to notify a child to do something, signals is probably what I'd use. If you need to transfer some more information between processes, it's probably a good idea to fully specify the requirements.

您可能要考虑的一件事是完全绕开所有进程间的东西,只使用线程.至少在Linux中,线程是调度程序的一等公民.较早的UNIX(在用户模式线程中)可能有所区别,但Linux并非如此.

One thing you may want to consider is to bypass all the inter-process stuff altogether and just use threads. At least in Linux, threads are first class citizens to the scheduler. Older UNIXes may have made a distinction (with user-mode threads) but that is not the case with Linux.

我发现这样做比较简单,并且您的信息会自动共享(请记住,您仍然需要使用互斥锁等保护共享的内容).

I've found that it's simpler to do it that way and your information is automatically shared (keeping in mind you still need to protect shared stuff with mutexes and such).

如果您已经致力于共享内存,另一种可能性是使用信号.假设您已经为每个孩子留出了一部分内存(他们知道他们在哪里),信号可能是通知一堆孩子进行并行工作的最快方法.

Another possibility if you're already committed to shared memory is to use signals. Assuming you've set aside sections of that memory for each child (and they know where they are), signals are probably the quickest way to notify a bunch of children for parallel work.

如果您的孩子只是在select循环中等待(例如,超时30秒)以进行定期工作,则有信号会导致它在EINTR时立即退出.这样可以使您有效地使用CPU,同时仍然可以立即做出响应.

If your children just wait around in a select loop with (for example) a 30-second timeout for doing periodic work, a signal will cause it to exit immediately with EINTR. That gives you your efficient CPU usage while still giving immediate response.

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

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