Advertisement

信号(基本概念 signal函数)

阅读量:

基本概念

信号被视为一种软件类型的中断形式,并非单纯的中断类别。通常指的是那些由外部设备基于硬件请求触发的中断行为。具体表现为CPU在遇到程序错误或者在执行内部调用时对这些异步事件做出相应的响应。

信号与中断的区别

相同点:

(1)采用了相同的异步通信方式;

当出现信号或中断请求时(识别到信号源并收到中断指令),都需要立即暂停当前运行中的任务(程序)以转投处理相关事务(请求)。

(3)都在处理完毕后返回到原来的断点;

(4)对信号或中断都可进行屏蔽。

不同点:

(1)中断有优先级,而信号没有优先级,所有的信号都是平等的;

(2)信号处理程序是在用户态下运行的,而中断处理程序是在核心态下运行;

(3)中断响应是及时的,而信号响应通常都有较大的时间延迟。

常用信号

进程对信号的三种处理方式

(1)忽略信号

不采取任何操作、有两个信号不能被忽略:SIGKILL(9号信号)和SIGSTOP。

(2)捕获并处理信号

内核中断正在执行的代码,转去执行先前注册过的处理程序。

(3)执行默认操作

默认操作通常是终止进程,这取决于被发送的信号。

函数signal

复制代码
 #include <signal.h>

    
 typedef void (*sighandler_t)(int);
    
 sighandler_t signal(int signum,sighandler_t handler);
    
    
    
    
    代码解读
复制代码
 RETURN VALUE

    
 signal()  returns  the previous value of the signal handler, or SIG_ERR on error.  In the event of an error,  errno  is  set  to  indicate  the cause.
    
    
    
    
    代码解读

signal是一个包含signum和handler两个参数的函数;通过parameter signum指定捕获或阻止the signal;当接收特定signal时会调用"handler"function提供response.

handler函数应当接受一个int类型的参数(如接收的信号状态码),其功能类型为void

操作符也可以取以下两种特定的数值:SIG_IGN(用于屏蔽该事件),SIG_DFL(使操作符归位至默认状态)。

复制代码
 #define SIG_ERR (void (*)())-1

    
 #define SIG_DEL (void (*)())0
    
 #define SIG_IGN (void (*)())1
    
    
    
    
    代码解读

signal信号捕获函数

复制代码
 #include<stdio.h>

    
 #include<unistd.h>
    
 #include<signal.h>
    
 #include<stdlib.h>
    
 static void usr(int num)
    
 {
    
     printf("the signal number is %d\n",num);
    
     if(num==SIGINT)
    
     {
    
     printf("SIGINT signal occured\n");
    
     exit(0);
    
     }
    
 }
    
 int main()
    
 {
    
     if(signal(SIGINT,usr)==SIG_ERR)
    
     {
    
     printf("can't catch SIGINT signal\n");
    
     }
    
     for(;;)
    
     {
    
     pause();
    
     }
    
     return 0;
    
 }
    
    
    
    
    代码解读

屏蔽信号函数

复制代码
 #include<stdio.h>

    
 #include<unistd.h>
    
 #include<signal.h>
    
 #include<stdlib.h>
    
 static void usr(int num)
    
 {
    
     printf("the signal number is %d\n",num);
    
     if(num==SIGINT)
    
     {
    
     printf("SIGINT signal occured\n");
    
     exit(0);
    
     }
    
 }
    
 int main()
    
 {
    
     if(signal(SIGINT,SIG_IGN)==SIG_IGN)
    
     {
    
     signal(SIGINT,usr);
    
     }
    
     for(;;)
    
     {
    
     pause();
    
     }
    
     return 0;
    
 }
    
    
    
    
    代码解读

signal函数恢复默认行为

复制代码
 #include<stdio.h>

    
 #include<unistd.h>
    
 #include<signal.h>
    
 #include<stdlib.h>
    
 void usr(int num)
    
 {
    
     printf("signal num is %d\n",num);
    
     if(num==SIGINT)
    
     {
    
     printf("SIGINT signal occured!\n");
    
     }
    
 }
    
 int main()
    
 {
    
     char ch;
    
     if(signal(SIGINT,usr)==SIG_ERR)
    
     {
    
     printf("error!");
    
     }
    
     while((ch=getchar())!='e')
    
     {
    
     pause();
    
     }
    
     signal(SIGINT,SIG_DFL);
    
     for(;;)
    
     {
    
     pause();
    
     }
    
     return 0;
    
 }
    
    
    
    
    代码解读

全部评论 (0)

还没有任何评论哟~