Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1758816
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: C/C++

2012-02-09 19:51:56

  1. #include <signal.h>
  2. int sigsuspend(const sigset_t *sigmask)

将进程的信号屏蔽字设置为sigmask的值。在捕捉到一个信号或是发生了一个信号终止该进程之前,该进程被挂起。如果捕捉到一个信号并从该信号处理程序返回,则sigsuspend返回,并将进程的信号屏蔽字设置为调用sigsuspend之前的值
  1. #include "apue.h"
  2. static void sig_int(int);

  3. int
  4. main(void)
  5. {
  6.     sigset_t newmask,oldmask,waitmask;
  7.     pr_mask("program start:");

  8.     if (signal(SIGINT,sig_int) == SIG_ERR)
  9.         err_sys("signal(SIGINT) error");
  10.     sigemptyset(&waitmask);
  11.     sigaddset(&waitmask,SIGUSR1);
  12.     sigemptyset(&newmask);
  13.     sigaddset(&newmask,SIGINT);

  14.     /*
  15.      *block sigint and save current signal mask
  16.      */
  17.     if (sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
  18.         err_sys("sig_block error");

  19.     /*
  20.      * critical region code
  21.      */
  22.     printf("this is critical region code \n");
  23.     pr_mask("in critical region code");

  24.     /*
  25.      * Pause allowing all signals except sigusr1
  26.      */
  27.     if (sigsuspend(&waitmask) != -1)
  28.         err_sys("sigsuspend error");
  29.     pr_mask("after return from suspend");
  30.     /*
  31.      *
  32.      * reset signal mask which unblocks sigint
  33.      */

  34.     if (sigprocmask(SIG_SETMASK,&oldmask,NULL) <0)
  35.         err_sys("sig_setmask error");
  36.     /*
  37.      * and continue processing
  38.      */
  39.     pr_mask("program exit");
  40.     exit(0);
  41. }
  42. static void
  43. sig_int(int signo)
  44. {
  45.     pr_mask("\n in sig_int");
  46. }

  1. #include "apue.h"
  2. #include <errno.h>

  3. void pr_mask(const char *str)
  4. {
  5.     sigset_t sigset;
  6.     int errno_save;

  7.     errno_save = errno ; /* we can be called by signal handles */
  8.     if (sigprocmask(0,NULL,&sigset) < 0)
  9.         err_sys("sigprocmask error");
  10.     printf("%s",str);

  11.     if (sigismember(&sigset,SIGINT)) printf("SIGINT");
  12.     if (sigismember(&sigset,SIGQUIT)) printf("SIGQUIT");
  13.     if (sigismember(&sigset,SIGUSR1)) printf("SIGUSR1");
  14.     if (sigismember(&sigset,SIGALRM)) printf("SIGALRM");
  15.     /* remaining signals can go here */
  16.     printf("\n");
  17.     errno = errno_save;
  18. }

  1. #include "apue.h"
  2. /* reliable version of signal() ,using posix signaction()*/
  3. Sigfunc *
  4. signal(int signo,Sigfunc * func)
  5. {
  6.     struct sigaction act,oact;
  7.     
  8.     act.sa_handler = func;
  9.     sigemptyset(&act.sa_mask);
  10.     act.sa_flags = 0;
  11.     if ( signo == SIGALRM){
  12.     #ifdef SA_INTERRUPT
  13.         act.sa_flags |= SA_INTERRUPT;
  14.     #endif
  15.     } else{
  16.     #ifdef SA_RESTART
  17.         act.sa_flags |= SA_RESTART;
  18.     #endif
  19.     }
  20.     if (sigaction(signo,&act,&oact) < 0)
  21.         return(SIG_ERR);
  22.     return(oact.sa_handler);
  23. }
program flow:
main()
block sig_int
critical code
sigprocmask    ------------ set sig_int 屏蔽字
pr_mask() -----------   sig_int
sigsuspend() ------------set sig_usr1  并使进程挂起

ctr+c
中断处理程序
pr_mask()-------------sig_int,sigusr1 
返回
exit()

  1. ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ gcc -Wall -o c10-15 c10-15.c error
  2. .c pr_mask.c c10-12.c

  3. ubuntu@ubuntu-virtual-machine:~/Desktop/apue$ ./c10-15

  4. program start:

  5. this is critical region code

  6. in critical region code
  7. SIGINT

  8. ^C

  9.  in sig_intSIGINTSIGUSR1

  10. after return from suspend
  11. SIGINT

  12. program exit



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