父母和孩子之间的沟通

发布于 2024-09-02 00:30:40 字数 236 浏览 1 评论 0原文

新手问题:

在 Unix 上,在一个有父级和一些子级的程序中:
- 家长如何有效地提醒孩子做一些工作..?
- 或者孩子们如何等待父母信号开始做一些工作?

编辑:
该程序尝试并行执行复杂的计算,我已经使用共享内存作为所有子项的公共工作区来更新结果和数据传输。
我现在需要的是父母对所有孩子有效地说“开始”......(多次调用)

谢谢

Newbie question:

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?

EDIT:
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)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-09-09 00:30:40

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

  • 共享内存。
  • 信号量。
  • 管道。
  • 信号。
  • 内存映射文件。
  • 插座。

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

每个的效率在很大程度上取决于您的用例。如果您只需要通知孩子做某事,那么我可能会使用信号。如果您需要在进程之间传输更多信息,那么完全指定要求可能是个好主意。


可能要考虑的一件事是完全绕过所有进程间的东西,只使用线程。至少在 Linux 中,线程对于调度程序来说是一等公民。较旧的 UNIX 可能已经做出了区分(使用用户模式线程),但 Linux 的情况并非如此。

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


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

如果您的孩子只是在 select 循环中等待(例如)执行定期工作的 30 秒超时,则信号将导致其立即退出并返回 EINTR。这可以为您提供高效的 CPU 使用率,同时仍能立即做出响应。

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

  • Shared memory.
  • Semaphores.
  • Pipes.
  • Signals.
  • Memory-mapped files.
  • Sockets.

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.


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.

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.

栀梦 2024-09-09 00:30:40

看看 Beej 的 IPC 指南来了解这一切是如何运作的……这是一本很好的指南。 Beej 还涵盖了同一站点上的套接字。

Have a look at Beej's Guide to IPC to understand how it all works...it is an excellent guide. Beej also covers sockets as well on that same site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文