Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109329
  • 博文数量: 27
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-07 00:14
文章分类

全部博文(27)

文章存档

2015年(1)

2014年(20)

2013年(6)

我的朋友

分类: C/C++

2014-01-26 10:42:30

1 子进程会继承父进程的signal block mask,不论是直接fork得到的,还是最终执行exec。
      例如 父进程 A 阻塞了SIGINT 信号,进程 A 启动top,那么我们不能通过crlt+c 将父进程杀死。

2 子进程会继承父进程的pengding 信号,也就是说当父进程创建子进程的时候,父进程已经有信号INT 在pending列表上
     那么,子进程也会有INT 信号在pending 列表上。


3 进程内的所有线程都共享信号处理函数


4 直接fork得到的子进程会继承父进程的信号处理函数,但是通过execve 过程中,信号的处理函数被复位。


5 留意 sigqueue
        int sigqueue(pid_t pid, int sig, const union sigval value)  ===> queue a signal and data to a process
        typedef union sigval
        {
             int sigval_int;
             void  *sigval_ptr;
         }  sigval_t;

        If the receiving process has installed a hanlder for this signal using the SA_SIGINFO flag to sigaction, then it can
obtain this data via the si_value feild of the siginfo_t structure passed as the second argument to the handler。


6 sigwait 和 sigsuspend、pause 的区别
    sigwait 系列函数剥夺信号处理函数被执行的权利,sigsuspend、pause 会等待信号处理函数执行完毕才返回。



点击(此处)折叠或打开

  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>


  7. void sig_usr1(int signum, siginfo_t *info, void *context)
  8. {
  9.     if (info->si_code == SI_QUEUE)
  10.     {
  11.         const char ** datas = info->si_value.sival_ptr;
  12.         while (*datas)
  13.         {
  14.             fprintf(stderr, "[INFO] ############# data: %s \n", *datas);
  15.             datas++;
  16.         }
  17.     }
  18. }
  19. int main(int argc, char *argv[])
  20. {

  21.     struct sigaction act;
  22.     act.sa_flags = SA_SIGINFO;
  23.     act.sa_sigaction = sig_usr1;
  24.     sigemptyset(&act.sa_mask);

  25.     if (sigaction(SIGUSR1, &act, NULL) != 0)
  26.     {
  27.         fprintf(stderr, "[ERROR] ############ faield to install signal dospositon .!");
  28.         exit(-1);
  29.     }

  30.     static const char * txts[] = {
  31.         "Hello, world",
  32.         "Just for test, can you see it ",
  33.         NULL
  34.     };
  35.     union sigval sigval;
  36.     sigval.sival_ptr = txts;
  37.     if (sigqueue(getpid(), SIGUSR1, sigval) != 0)
  38.     {
  39.         fprintf(stderr, "[ERROR] ########### failed to queue signal, err msg: %s\n", strerror(errno));
  40.         exit(-1);
  41.     }

  42.     /*
  43.      * do other thing here.
  44.      */

  45.     return 0;
  46. }
              Author: weijiaqiang
              Date:   Sun Feb 23 21:15:28 2014 +0800

               测试: sigwait 函数的功能
                      该函数是等待信号集上的某个信号产生,即使该信号已经被block。 信号的处理函数没有被调用。
               但是,如果信号没有被block也没有为信号显示提供一个handler,那么当信号产生时程序就会对信号进行默认处理

点击(此处)折叠或打开

  1. #include "../common.h"
  2. #include <signal.h>


  3. void sig_usr1(int signo, siginfo_t* info, void* context)
  4. {
  5.     cerr << "In the signal dispotion .!" << endl;
  6. }
  7. int main(int argc, char *argv[])
  8. {
  9.     InitLogSystem(argv[0]);
  10.     sigset_t sigset;
  11.     sigemptyset(&sigset);
  12.     sigaddset(&sigset, SIGUSR1);

  13.     /* ============== 测试信号的处理函数是否被调用 ============= */
  14.     //struct sigaction act;
  15.     //act.sa_flags = SA_SIGINFO;
  16.     //act.sa_sigaction = sig_usr1;
  17.     //sigemptyset(&act.sa_mask);
  18.     //if (sigaction(SIGUSR1, &act, NULL) != 0)
  19.     //{
  20.     //    LogSysErrAndExit("main", "sigaction");
  21.     //}
  22.     
  23.     /* ========= 测试阻塞信号,sigwait 的反应 ======== */    
  24.     //if (sigprocmask(SIG_BLOCK, &sigset, NULL) != 0)
  25.     //{
  26.     //    LogSysErrAndExit("main", "sigprocmask");
  27.     //}
  28.     //Log4cxxLogger::GetObj().Info("main", " block signal ok.!");


  29.     int signo;
  30.     if (sigwait(&sigset, &signo) != 0)
  31.     {
  32.         LogSysErrAndExit("main", "sigwait");
  33.     }

  34.     Log4cxxLogger::GetObj().Info("main", " got the signal .!");
  35.     return 0;
  36. }


        

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