Home > Enterprise >  Communication between multiple child processes using SIGNALS
Communication between multiple child processes using SIGNALS

Time:11-29

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. 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. 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. There's an image above of the steps.

Important: I can only use signals (no pipes, semaphores and other ICP solutions)

CodePudding user response:

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; 2. signal from parent process to the other child process; For signal from child process to parent process, you can use a FIFO, for example use a PIPE; or a queue in shared 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. Also consider block/none block when you access a queue, or socket, etc. It's depend on your service logic.

CodePudding user response:

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, e.g. 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.

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)

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)

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.

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.

  • Related