1.1 概述
使用kill -l查看系统支持的信号类型如下:
其中,1-31是定期信号,32-64是实时信号。两者的区别在于:内核不支持定期信号的排队,导致,如果某一个定期信号在得到处理前被重复发送了多次,那么只会保留最后一个发送的信号;内核支持实时信号的排队,即重复发送的多个相同的实时信号都会被进程接收并处理。
1.2 系统调用
1.3 示例程序
#include
#include
#include
#include
#include
#include
#define UNKNOWN "unknown"
#define name_path 20
char sig_list[][name_path] = {
"SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGIO", "SIGPWR", "SIGSYS", UNKNOWN, UNKNOWN, "SIGRTMIN", "SIGRTMIN+1", "SIGRTMIN+2", "SIGRTMIN+3", "SIGRTMIN+4", "SIGRTMIN+5", "SIGRTMIN+6", "SIGRTMIN+7", "SIGRTMIN+8", "SIGRTMIN+9", "SIGRTMIN+10", "SIGRTMIN+11", "SIGRTMIN+12", "SIGRTMIN+13", "SIGRTMIN+14", "SIGRTMIN+15", "SIGRTMAX-14", "SIGRTMAX-13", "SIGRTMAX-12", "SIGRTMAX-11", "SIGRTMAX-10", "SIGRTMAX-9", "SIGRTMAX-8", "SIGRTMAX-7", "SIGRTMAX-6", "SIGRTMAX-5", "SIGRTMAX-4", "SIGRTMAX-3", "SIGRTMAX-2", "SIGRTMAX-1", "SIGRTMAX"
};
int list_len = 64;
void current_time(){
struct timeval tv;
gettimeofday(&tv,NULL);
printf("current time = %ldms\n", tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
void sig_op(int sig, siginfo_t *info, void *other){
sigset_t set;
if(sigemptyset(&set) < 0){
printf("sig_op: sigemptyset error!\n");
return;
}
if(sigaddset(&set, sig) < 0){
printf("sig_op: sigaddset error!\n");
return;
}
if(sigprocmask(SIG_BLOCK, &set, NULL) < 0){
printf("sig_op: sigprocmask error!\n");
return;
}
printf("Recievd sig %d\n", sig);
switch(sig){
case 62:
printf("I am going to sleep 10 sec.\n");
current_time();
sleep(10);
current_time();
printf("I have been waken up.\n");
break;
case 63:
printf("I am doing something cool!\n");
break;
case 64:
printf("Bye!\n");
exit(0);
}
if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0){
printf("sig_op: sigprocmask unblock error!\n");
return;
}
}
int main(int argc, char *argv[]){
struct sigaction act, old_act;
act.sa_sigaction = sig_op;
act.sa_flags = SA_SIGINFO;
if(sigemptyset(&act.sa_mask) < 0){
printf("sigemptyset error!\n");
exit(-1);
}
int i;
for(i = 0; i < list_len; i ++){
if(sigaction(i + 1, &act, &old_act) < 0){
printf("%s = %d: sigaction fail!\n", sig_list[i], i + 1);
}
}
while(1){};
return 0;
}