Linux c 开发-11 Singnal用法试验

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sig_handler(int signum);
int main()
{
	signal(SIGINT,sig_handler);
	pause();
	
}
void sig_handler(int signum)
{
    if(0 > signum)
    {
        fprintf(stderr,"sig_handler param err. [%d]\n",signum);
        return;
    }
    if(SIGINT == signum)
    {
        printf("Received signal [%s]\n",SIGINT==signum?"SIGINT":"Other");
    }
    if(SIGQUIT == signum)
    {
        printf("Received signal [%s]\n",SIGQUIT==signum?"SIGQUIT":"Other");
    }

    return;
}

 

信号处理可以用来模拟操作系统的中断功能,如上代码,当按Ctrl+C 时,就会输出信息

 

 

  

posted @ 2021-03-28 19:15  zhaogaojian  阅读(121)  评论(0编辑  收藏  举报