Reputation: 21
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?
Upvotes: 1
Views: 527
Reputation: 58524
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?
...
}
}
Upvotes: 1
Reputation: 2093
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.
Upvotes: 0