以下建议代码:
-
正确检查错误
-
正确设置struct sigaction
-
干净地编译
现在,提议的代码:
#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 signal\n", 21);
}
else
{
write( 1, "unexpected signal received\n", 27 );
}
}
int main( void )
{
struct sigaction act;
memset( &act, '\0', 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: %d\n", getpid());
while (1)
pause();
return 0;
}