简体   繁体   English

多个子进程之间使用 SIGNALS 进行通信

[英]Communication between multiple child processes using SIGNALS

Image clarification: After all processes have been created, a signal will be sent from the previous process following the red arrow I need to create a program in which I fork() multiple processes.图像澄清:创建所有进程后,将按照红色箭头从前一个进程发送信号我需要创建一个程序,其中我 fork() 多个进程。 Then the child process will randomly send a signal to the child process "next" to it (imagine a graph).然后子进程会随机发送一个信号给它“下一个”的子进程(想象一个图)。 From my understanding, I can communicate between the parent process and its child using kill() and their PIDs, but I haven't found a way to do it between child processes.据我了解,我可以使用 kill() 及其 PID 在父进程及其子进程之间进行通信,但我还没有找到在子进程之间进行通信的方法。 Is it even possible?有可能吗? I'm only allowed to use signals for communication.我只被允许使用信号进行通信。

So far, what I tried is child sending a signal to the parent, with the parent then killing the sibling child process.到目前为止,我尝试的是子进程向父进程发送信号,然后父进程杀死兄弟进程。 However, this doesn't work when you increase the number of processes (which is what I need to do) because of all the PIDs I don't have.但是,当您增加进程数(这是我需要做的)时,这不起作用,因为我没有所有 PID。 There's an image above of the steps.步骤上面有一张图片。

Important: I can only use signals (no pipes, semaphores and other ICP solutions)重要提示:我只能使用信号(没有管道、信号量和其他 ICP 解决方案)

Since the parent process have list of all the child process, yes it's good way for child process to send the "signal" to the parent process, which further on dispatch the "signal" to the proper child process.由于父进程有所有子进程的列表,是的,这是子进程向父进程发送“信号”的好方法,父进程进一步将“信号”分派给适当的子进程。 Thus involves two factors: 1. signal from child process to parent process;因此涉及两个因素: 1. 子进程向父进程发送信号; 2. signal from parent process to the other child process; 2. 父进程向其他子进程发出信号; For signal from child process to parent process, you can use a FIFO, for example use a PIPE;对于从子进程到父进程的信号,您可以使用 FIFO,例如使用 PIPE; or a queue in shared memory;或共享 memory 中的队列; or socket;或插座; For signal from parent process to child process you can use semaphore, variable in shared memory, PIPE, etc. Basically it's related to inter-process communication, what you need consider is to avoid race condition when multiple process trying to access same resource (variable), which you can use a lock to protect.对于从父进程到子进程的信号,您可以使用信号量,共享变量 memory、PIPE 等。基本上它与进程间通信有关,您需要考虑的是避免多个进程尝试访问同一资源时的竞争条件(变量),您可以使用锁来保护它。 Also consider block/none block when you access a queue, or socket, etc. It's depend on your service logic.当您访问队列或套接字等时,还要考虑块/无块。这取决于您的服务逻辑。

The best approach (the shell does it for you when you start a job controlled pipeline, but you can create one yourself) is to create a new process group and then send a signal from the last process to start (if you can know which process started the last in any way) to the process group (this will make all processes in the group to receive the signal, eg this is made by the control tty driver to send a SIGHUP signal to the whole process group when a hungup is detected on the tty) The main problem you have is that the parent process doesn't know how many levels are under its group of children.最好的方法(shell 在您启动作业控制管道时为您完成,但您可以自己创建一个)是创建一个新的进程组,然后从最后一个进程发送信号以启动(如果您知道哪个进程以任何方式启动最后一个)到进程组(这将使组中的所有进程接收信号,例如,当检测到挂断时,控制 tty 驱动程序将SIGHUP信号发送到整个进程组) tty) 你遇到的主要问题是父进程不知道它的子进程下有多少层。

In your drawing, anyway, there's a signal being sent from processes labeled 4 and 5, to process 9 (which is a sibling, a child of their parent) there's no way for you to know that process 9 is a child of your parent, so you cannot get the process dependency tree (you can parse the ps command output, but that's nonportable and very error prone, so you will end in a non-end way)无论如何,在你的绘图中,有一个信号从标记为 4 和 5 的进程发送到进程 9(它是兄弟,他们父母的孩子)你无法知道进程 9 是你父母的孩子,所以你无法得到进程依赖树(你可以解析ps命令output,但那是不可移植的,而且很容易出错,所以你会以一种非结束的方式结束)

The process group solution only leaves out the processes that themselves create new process groups and become a process group leader, but even the processes that get reparented by means of the death of their parents do conserve the process group id (it is used for job control, so is will work fine on your problem)进程组解决方案只排除了自己创建新进程组并成为进程组领导者的进程,但即使是通过父进程死亡而重新设置父进程的进程也确实保存了进程组 ID(用于作业控制,因此可以很好地解决您的问题)

Read about it on termios man page, and how the shell controls jobs to kill the correct process set, when you press Control-C at the terminal.termios手册页上阅读相关信息,以及当您在终端按下 Control-C 时 shell 如何控制作业以终止正确的进程集。

Next time, show your code, and we'll be able to help you in a better way.下次,展示您的代码,我们将能够以更好的方式帮助您。 Read How to create a Minimal, Reproducible Example for instructions on how to post and get better help in StackOverflow.阅读How to create a Minimal, Reproducible Example了解有关如何在 StackOverflow 中发布和获得更好帮助的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM