大理石墓安装:操作系统实验 信号操作函数 siginttest.c

来源:百度文库 编辑:偶看新闻 时间:2024/07/06 21:05:02


/**siginttest.c**/
#include
#include
#include
#include
/* 自定义gigint 处理函数,如果按Ctrl+c键,则会打印提示,而不是默认的退出*/
void my_func(int sigo_num){

 printf("if you want to quit ,please try 'ctrl+\\'.\n");
 }
 
 int main()
 {
  sigset_t set;
  struct sigaction action1,action2;
  /* 初始化信号集为空*/
  if(sigemptyset(&set)<0)
  {
   perror("sigemptyset");
   exit(1);
  }
  /* 将相应的信号加入信号集*/
  if(sigaddset(&set,SIGQUIT)<0){
   perror("sigaddset SIGQUIT");
   exit(1);
   
  }
  /*将相应的信号加入信号集*/
  if(sigaddset(&set,SIGINT)<0){
   perror("sigaddset SIGINT");
   exit(1);
   
  }
 
  /*设置信号屏蔽字*/
  if(sigprocmask(SIG_BLOCK,&set,NULL)<0){
   perror("sigprocmask SIG_BLOCK");
   exit(1);
 
 
  }else{
   printf("blocked,and sleep for 5s...\n");
   sleep(5);
   
  }
 
  if(sigprocmask(SIG_UNBLOCK,&set,NULL)<0){
   perror("sigprocmask SIG_UNBLOCK");
   exit(1);
  }else{
   printf("unblock\n");
   /* 此处可以添加函数功能模块 process()*/
   sleep(2);
   printf("if you want to quit this program,please try ...\n");
 
  }
 
  while(1){
   if(sigismember(&set,SIGINT)){
    sigemptyset(&action1.sa_mask);
    action1.sa_handler=my_func;
    sigaction(SIGINT,&action1,NULL);
   
   }else
   if(sigismember(&set,SIGQUIT)){
    sigemptyset(&action2.sa_mask);
    action2.sa_handler=SIG_DFL;
    sigaction(SIGTERM,&action2,NULL);
   
   }
 
  }
  return 0;
 
 
 
 }