sigaction使用(signal siginfo_t)
时间:2010-04-18 来源:ubuntuer
sigaction使用
可以参考的几个man
man sigaction
man 7 signal
Signal Value Action Comment
----------------------------------------------------------------------
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at tty
...............
这里注意下sigkill和sigstop不能忽略 捕捉。
kill -s signal pid
先看看struct sigaction的定义
struct sigaction
{
void (*sa_handler)(int); /* addr of signal handler or
SIG_IGN, SIG_DFL */
sigset_t sa_mask; /* additional signals to block */
int sa_flags; /* signal options */
/* alternate handler */
void (*sa_sigaction)(int, siginfo_t *, void *);
};
如果不设置sa_flags的话,初始化为void (*sa_handler)(int);//这个就和signal差不多了哦
void handler(int signo);
如果设置了SA_SIGINFO则,初始化为void (*sa_sigaction)(int, siginfo_t *, void *);
//多了个siginfo,这个结构体的定义参见APUE,还有哥context,用于标识信号传递时进程的上下文
void handler(int signo, siginfo_t* info, void* context);
注意两者不可以同时使用...
1.简简单单的只是完成跟signal一样的功能,不用传任何值给信号处理函数
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
void sig_op(int signo)
{
printf("the signo is %d\n",signo);
}
int main(int argc,char** argv)
{
struct sigaction act;
pid_t pid;
pid=getpid();
sigemptyset(&act.sa_mask);
act.sa_handler=sig_op;
act.sa_flags=0;
printf("the pid is %d",pid);
if(sigaction(SIGPIPE,&act,NULL)==-1)
printf("install error~!\n");
while(1)
{
sleep(1);
printf("wait for signal\n");
}
return 0;
}
[kenthy@kenthy c_c]$ ./sig
the pid is 3882
wait for signal
wait for signal
wait for signal
wait for signal
the signo is 13//这个时候另一个terminal kill -s SIGPIPE 3882
wait for signal
wait for signal
wait for signal
2. sigaction本色使用
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
/*
struct siginfo
{
int si_signo;
int si_errno;
int si_code;
pid_t si_pid;
uid_t si_uid;
void* si_addr;
int si_status;
long s_band
}
*/
void sig_op(int signo, siginfo_t* info, void* context)
{
printf("the signo is %d\n",signo);
printf("sig pid is %d\n", (int)(info->si_pid));
printf("sig uid is %d\n", (int)(info->si_uid));
}
int main(int argc,char** argv)
{
struct sigaction act;
struct sigaction oact;
pid_t pid;
pid=getpid();
sigemptyset(&act.sa_mask);
act.sa_handler=sig_op;
act.sa_flags=SA_SIGINFO;
printf("the pid is %d",pid);
if(sigaction(SIGPIPE,&act,&oact)==-1)
//这里还可以保存原来的signal处理方式,以便有的时候需要恢复oact。
//sigaction(sig,&oact,NULL)
printf("%s","install error~!\n");
while(1)
{
sleep(1);
printf("%s","wait for signal\n");
}
return 0;
}
[kenthy@kenthy c_c]$ ./sig
the pid is 3904
wait for signal
wait for signal
wait for signal
wait for signal
wait for signal
wait for signal
the signo is 13 //这个时候另一个terminal kill -s SIGPIPE 3904
sig pid is 3135
sig uid is 500
wait for signal
wait for signal
uid是500可以理解....
sig pid是3135不解,也不想解