Reputation: 33
I am trying to trap a signal raised by a child process. However, my trap callback function is never called. I have the following test code
#include <csignal>
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
std::this_thread::sleep_for (std::chrono::seconds(5));
std::cout << ">>> Signal Sent!" << std::endl;
raise(SIGUSR1);
return 0;
}
And this bash script
set -bm
set -e
KEEP_GOING=true
sigusr1()
{
echo "SIGUSR1 Recieved"
KEEP_GOING=false
}
trap sigusr1 SIGUSR1
./signalTest &
while $KEEP_GOING ; do
sleep 1s
echo "Waiting for signal"
done
When I run it I get the following
Waiting for signal
Waiting for signal
Waiting for signal
Waiting for signal
>>> Signal Sent!
[1]+ User defined signal 1 ./signalTest
Waiting for signal
Waiting for signal
Waiting for signal
Waiting for signal
Waiting for signal
^C
From the output I see that the signal is in fact sent, and in some capacity received. However the callback function in my trap is not executed.
Any thoughts?
Upvotes: 3
Views: 178
Reputation: 136208
raise
sends the signal to the calling thread.
kill
sends a signal to the specified process or thread.
To send the signal to the parent process, instead of
raise(SIGUSR1);
do
#include <unistd.h>
// ...
kill(getppid(), SIGUSR1);
Upvotes: 2