简体   繁体   中英

C receive signal from a specific process

I'm trying to solve a problem i've got in a C project where two process A and B comunicate. process B receive a signal from the user using bash and it send that signal to process A. process A must ignore any signal that comes from user but must receive it only from process B. The question Is: is it possible to write this comunication only with signals or do I have to use another data structure such as socket in order to make it happen?

Yes, this is possible with standard UNIX signals, which have a concept of a sender so your application can check who generated the signal.

If process A knows the PID of process B, it can register its signal handlers with sigaction() and SA_SIGINFO . Then, upon delivery or acceptance of the signal, process A can check the si_code and si_pid members of the siginfo_t structure passed to the handler. If not from B, simply take no action.

Something like:

static pid_t pid_of_B;

....

static void
my_handler(int sig_num, siginfo_t *si, void *ignored) {
    switch (si->si_code) {
    case SI_USER:   // sent via kill
    case SI_QUEUE:  // sent via sigqueue
        if (si->si_pid == pid_of_B) ...   // sent from B?
    ...
    }
}

No, this is not possible with signals. Signals have to concept of "a sender" so the application can not check who generated the signal.

But you can use pipes to pass data between two processes.

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