简体   繁体   中英

fork and signal: how to send signals from parent process to specific child process

I need to fork two child-processes. One can receive the signal 3, print hello and send the signal 4 to the the other child process; The other can receive the signal 4, print world and send the signal 3 to the first child process.
To start, the father process will send the signal 3 to the first child process after sleeping for 3 seconds.
Then 3 seconds later, the father process will send SIGKILL to kill both of them.

I don't know how to send signals to a specific child process (I knew that we had a function kill to send signals but I don't know to use it here).

Here is my code:

#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

void func(int n)
{
        printf("ping\n");
        // how to send signal 4 to the second process?
}

void func2(int n)
{
        printf("pong\n");
        // how to send signal 3 to the first process?
}

int main()
{
        pid_t pid;
        int i;
        for(i = 0; i < 2; i++)
        {
                pid = fork();
                if(pid == 0)
                {
                        if(i == 0)
                        {
                                signal(3, func);
                        }
                        else
                        {
                                signal(4, func2);
                        }
                        while(1);
                }
                else
                {
                        if(i == 1)
                        {
                                sleep(3);
                                // how to send signal 3 to the first child process?
                                sleep(3);
                                // how to kill the two children?
                        }
                }
        }


        return 0;
}

When you fork you get the new pid . Per the kill manpage you call kill(pid_t pid, int sig); using the pid

you could use the popen() function to open a process by forking and opening a pipe to that process (instead of using fork() directly)

The parent knows the PID of each process so can then easily pass the pid of the second child to the first child.

The first child can use the pid and the kill()` function to pass a signal to the second child.

SO, use popen() to start the first child. use fork() to start the second child, then pass the pid from the second child to the first via the stream created with popen().

the handling of the pid value returned from the call to fork() is not being handled correctly.

The posted code is making the assumption that the call to fork() was successful... This is not a safe/valid assumption

The code also needs to check for the pid being -1 and appropriately handling that error.

when a child process completes, it should NOT sit in a while() loop but rather exit, using the exit() function.

The parent, should not just exit, as that leaves the two child processes as zombies. (zombies are very difficult to get rid of short of a system reboot.)

Rather, the parent should call wait() or even better waitpid() (and remember the child processes need to actually exit, NOT sit in a while() loop.

1) the func() and func2() should check the parameter to assure that it was the correct signal that was being processed.

2) the man page for signal() indicates that it should not be used. The man page suggest using: sigaction() ,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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