I try to run a simple program (code below), that should receive and handle the SIGUSR1
signal. It works well on “real” Linux, but if I run it on WSL after sending SIGUSR1
it prints
User defined signal 1
and terminates.
AFAIK this means that SIGUSR1 wasn’t handled by program and default handler was called. How can I make signal handling on WSL work properly?
Thanks in advance!
Source code:
JavaScript
x
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void handle_signal(int signo)
{
write(1, "Recieved user signaln", 22);
}
int main()
{
struct sigaction act;
act.sa_handler = handle_signal;
sigfillset(&(act.sa_mask));
sigaction(SIGUSR1, &act, NULL);
printf("PID: %dn", getpid());
while (1)
pause();
return 0;
}
Advertisement
Answer
The following proposed code:
- properly checks for errors
- properly sets up the struct sigaction
- cleanly compiles
And now, the proposed code:
JavaScript
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void handle_signal(int signo);
void handle_signal(int signo)
{
if( signo == SIGUSR1 )
{
write( 1, "Received user signaln", 21);
}
else
{
write( 1, "unexpected signal receivedn", 27 );
}
}
int main( void )
{
struct sigaction act;
memset( &act, '', sizeof( act ) );
act.sa_handler = handle_signal;
//sigfillset(&(act.sa_mask)); // enable catching all signals
if( sigaction(SIGUSR1, &act, NULL) != 0)
{
perror( "sigaction failed" );
exit( EXIT_FAILURE );
}
printf("PID: %dn", getpid());
while (1)
pause();
return 0;
}