C语言-根据信号处理

Posted 爱吃香蕉的猴子0000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言-根据信号处理相关的知识,希望对你有一定的参考价值。

Hello, 大家好,我是爱吃香蕉的猴子,记录一个linux很实用的程序,根据监听linux上的一些信号


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

void diediedie(int sig);
int catch_signal(int sig, void(*handler)(int));
int main(void)
{
    if (catch_signal(SIGINT, diediedie) == -1) 
    {
        fprintf(stderr, "Can't map the handler"); 
        exit(2);
    }
    char name[30];
    printf("Enter your name :");
    fgets(name, 30, stdin);
    printf("Hello %s\\n", name);

    return 0;
}

void diediedie(int sig)
{
    puts("\\n");
    puts("Goodbye cruel world ...\\n");
    exit(1);
}

int catch_signal(int sig, void(*handler)(int)) 
{
    struct sigaction action;
    action.sa_handler = handler;
    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    return sigaction(sig, &action, NULL);
}


                          Code的搬运工V1.0

以上是关于C语言-根据信号处理的主要内容,如果未能解决你的问题,请参考以下文章

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

SQL Select 语句的用法

C语言-根据信号处理

C语言-根据信号处理

C语言预处理 编译 汇编 链接四个阶段