相关函数 signal,sigprocmask() ,sigpending,sigsuspend, sigemptyset
表头文件 #include<signal.h>
定义函数 int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
函数说明 sigaction()会依参数signum指定的信号编号来设置该信号的处理函数。参数signum可以指定SIGKILL和SIGSTOP以外的所有信号。
如参数结构sigaction定义如下
struct sigaction {
void (*sa_handler)(int);void (*sa_sigaction)(int, siginfo_t *, void *);sigset_t sa_mask;int sa_flags;void (*sa_restorer)(void);
}
-
#include <signal.h>
-
#include <unistd.h>
-
#include <stdio.h>
-
-
static void int_hander(int s)
-
{
-
printf("Catch a signal sigint
");
-
}
-
-
int main(void)
-
{
-
int i;
-
struct sigaction act,oact;
-
-
act.sa_handler = int_hander;
-
sigemptyset(&act.sa_mask); //清空此信号集
-
act.sa_flags = 0;
-
sigaction(SIGINT, &act, &oact);
-
-
while(1)
-
{
-
for(i=0;i<5;i++)
-
{
-
write(1,".",1);
-
sleep(1);
-
}
-
write(1, "
", 1);
-
}
-
-
sigaction(SIGINT, &oact, NULL); //恢复成原始状态
-
-
return 0;
- }