代码示例:unix环境下signal处理

无干货,仅用作demo

#include <stdio.h>
#include  <signal.h>


static void print_sig(int);
int main(){
        signal(SIGQUIT, print_sig);
        signal(SIGKILL, print_sig);
        while(1){
                pause();//挂起以等待信号        
        }
}

static void print_sig(int signo){
        printf("Signal Encountered: %d\n", signo);
}

执行它,然后用另一个进程执行kill -3
pid,可以在控制台看到输出"Signal Encountered: 3";

但如果kill -9
pid,则看不到任何输出,因为UNIX环境规定kill -9不准block.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.