没有 pthreads/sempahores、信号的 Unix 进程同步?

发布于 2024-09-28 21:12:43 字数 587 浏览 3 评论 0原文

我正在尝试编写一个不使用 pthreads 或信号量库的程序。我想与信号进行一些同步。我在一些 Unix 书籍中读到过它们,但我仍然不太明白。我写了一些模拟程序设计/代码,这不是我的程序,但它给出了我如何尝试组织进程/信号的想法。这看起来是一个合理的解决方案吗?

程序流程:

Main 分叉随机次数,并在每个子进程中执行“程序”。程序为子级分配一个值,1 或 2(例如输入或输出等)。

我想要一种方法来同步这些孩子。我认为使用信号是可能的,从我读到的内容来看,我想做一些类似的事情:

如果子== 1,发送父(主)SIGUSR1,否则如果子== 2发送父(主)SIGUSR2。当父进程服务该信号时,则结束。

现在回到主要部分,我想组织这些传入信号。我一次只接受一种类型,所以如果一个孩子向我发送 SIGUSR1,我将阻止所有 SIGUSR2,或者如果一个孩子先向我发送 SIGUSR2,那么我将阻止所有 SIGUSR1。 Main 将接收信号并为该类型的所有信号提供服务,直到不再有该类型,然后它将检查/解锁其他类型并为任何该类型(如果存在)提供服务,否则 main 将等待更多信号。

这看起来像是信号的正确使用吗?这对于信号来说是否可能?

谢谢!

I am attempting to write a program that doesn't use pthreads or semaphore libraries. I want to do some synchronization with signals. I have read about them in a few Unix books, but I still don't quite get it. I wrote up some mock program design/code, this isn't my program but it gives an idea of how I am trying to organize processes/signals. Does this seem like a plausible solution?

Program Flow:

Main forks a random number of times, and executes "program" in each child. The program assigns a value to the child, 1 or 2 (such as input or output etc.).

I want a way to synchronize these children. I thought using signals could be possible, and from what I read I would want to do something like:

If child == 1, send parent (main) SIGUSR1, else if child == 2 send parent (main) SIGUSR2. When the parent services the signal, then end.

Now back in main I want to organize these incoming signals. I would only accept one type at a time, so if a child sends me SIGUSR1, I will block all SIGUSR2s, or if a child sends me SIGUSR2 first then I block all SIGUSR1s. Main will receive the signal and service all signals of that type until there are no more of that type, then it will check/unblock the other type and service any of that type if they exist, else main will wait for more signals.

Does this seem like a proper use of signals and is this even possible with signals?

Thanks!

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

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

发布评论

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

评论(1

﹏半生如梦愿梦如真 2024-10-05 21:12:43

您没有说出为什么您想要使用信号。您只是想弄清楚信号是如何工作的吗?

这看起来像是信号的正确使用吗?这对于信号来说是否可能?

我不会采用您提出的那种考虑到信号的设计。你没有说孩子们在做什么,但我可能会使用管道、套接字,或者消息队列。

如果您可以添加一些有关您的高级目标的信息,我也许可以为您提供一些更好的方向。

You don't say why you want to use signals. Are you just trying to figure out how signals work?

Does this seem like a proper use of signals and is this even possible with signals?

I wouldn't approach the sort of design you've presented with signals in mind. You don't say what the children are doing, but I might use pipes, or sockets, or perhaps message queues.

If you can add some information about your high-level goals, I may be able to offer you some better direction.

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