#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
signal() sets the disposition of the signal signum to handler, which is
either SIG_IGN, SIG_DFL, or the address of a programmer-defined function (a "signal handler")
The signals SIGKILL and SIGSTOP cannot be caught or ignored. ——即信号SIGKILL和信号SIGSTOP不能被捕获或者忽略。
NAME
<pre> sigaction - examine and change a signal action
SYNOPSIS
#include <signal.h>
int sigaction(int signum, const struct sigaction *act,
struct sigaction *oldact);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sigaction(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
If act is non-NULL, the new action for signal signum is installed from
act. If oldact is non-NULL, the previous action is saved in oldact.
The sigaction structure is defined as something like:
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
On some architectures a union is involved: do not assign to both
sa_handler and sa_sigaction.
The sa_restorer element is obsolete and should not be used. POSIX does
not specify a sa_restorer element.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
/* Define the signal handle function */
void my_func(int sig_no)
{
switch(sig_no)
{
case SIGINT:
printf("I have get SIGINT\n");
break;
case SIGQUIT:
printf("I have get SIGQUIT\n");
break;
default:
break;
}
}
int main()
{
struct sigaction action;
printf("Waiting for signal SIGINT or SIGQUIT...\n");
/* Initialize the sigaction structure*/
action.sa_handler = my_func;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
/* Send certain signal and go to handle_fun */
sigaction(SIGINT, &action, 0);
sigaction(SIGQUIT, &action, 0);
pause();
alarm(3);
printf("I'm over!\n");
exit(0);
}#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
/* Define the signal handle function */
void my_func(int sig_no)
{
printf("If you want to quit, please try SIGQUIT\n");
}
int main()
{
sigset_t set, pendset;
struct sigaction action1, action2;
/* step1: Initialize the sigset with Null. */
if(sigemptyset(&set) < 0)
{
perror("sigemptyset");
exit(1);
}
/* Add certain signal to sigset */
if(sigaddset(&set, SIGQUIT) < 0)
{
perror("sigaddset");
exit(1);
}
if(sigaddset(&set, SIGINT) < 0)
{
perror("sigaddset");
exit(1);
}
if(sigismember(&set, SIGINT) < 0)
{
sigemptyset(&action1.sa_mask);
action1.sa_handler = my_func;
action1.sa_flags = 0;
sigaction(SIGINT, &action1, NULL);
}
if(sigismember(&set, SIGQUIT) < 0)
{
sigemptyset(&action2.sa_mask);
action2.sa_handler = my_func;
action2.sa_flags = 0;
sigaction(SIGQUIT, &action2, NULL);
}
/* step2: Set the value of mask word */
if(sigprocmask(SIG_BLOCK, &set, NULL) < 0)
{
perror("sigprocmask");
exit(1);
}
else
{
printf("Signal set was blocked, Press any key!\n");
getchar();
}
/* Delete the signal of set */
if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)
{
perror("sigprocmask");
exit(1);
}
else
{
printf("Signal set was unblocked, Press any key!\n");
}
while(1);
exit(0);
}
原文:http://blog.csdn.net/chen22075x/article/details/41604695