Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4710626
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: C/C++

2010-04-18 14:21:30

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
#include
#include
#include

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
#include
#include
#include

/*
  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不解,也不想解

阅读(6583) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~