linux signal 详解

时间:2021-06-09 09:24:11   收藏:0   阅读:22

参考https://man7.org/linux/man-pages/man7/signal.7.html

signal是什么

定位

作用

用法

谁产生的signal

也是改变程序状态的原因,主要以下三种

各个信号的具体产生原因

Signal      Standard   Action   Comment

SIGABRT      P1990      Core    Abort signal from abort(3)
SIGALRM      P1990      Term    Timer signal from alarm(2)
SIGBUS       P2001      Core    Bus error (bad memory access)
SIGCHLD      P1990      Ign     Child stopped or terminated
SIGCLD         -        Ign     A synonym for SIGCHLD
SIGCONT      P1990      Cont    Continue if stopped
SIGEMT         -        Term    Emulator trap
SIGFPE       P1990      Core    Floating-point exception
SIGHUP       P1990      Term    Hangup detected on controlling terminal or death of controlling process
SIGILL       P1990      Core    Illegal Instruction
SIGINFO        -                A synonym for SIGPWR
SIGINT       P1990      Term    Interrupt from keyboard

SIGIO          -        Term    I/O now possible (4.2BSD)
SIGIOT         -        Core    IOT trap. A synonym for SIGABRT
SIGKILL      P1990      Term    Kill signal
SIGLOST        -        Term    File lock lost (unused)
SIGPIPE      P1990      Term    Broken pipe: write to pipe with no readers; see pipe(7)
SIGPOLL      P2001      Term    Pollable event (Sys V); synonym for SIGIO
SIGPROF      P2001      Term    Profiling timer expired
SIGPWR         -        Term    Power failure (System V)
SIGQUIT      P1990      Core    Quit from keyboard
SIGSEGV      P1990      Core    Invalid memory reference
SIGSTKFLT      -        Term    Stack fault on coprocessor (unused)
SIGSTOP      P1990      Stop    Stop process
SIGTSTP      P1990      Stop    Stop typed at terminal
SIGSYS       P2001      Core    Bad system call (SVr4); see also seccomp(2)
SIGTERM      P1990      Term    Termination signal
SIGTRAP      P2001      Core    Trace/breakpoint trap
SIGTTIN      P1990      Stop    Terminal input for background process
SIGTTOU      P1990      Stop    Terminal output for background process
SIGUNUSED      -        Core    Synonymous with SIGSYS
SIGURG       P2001      Ign     Urgent condition on socket (4.2BSD)
SIGUSR1      P1990      Term    User-defined signal 1
SIGUSR2      P1990      Term    User-defined signal 2
SIGVTALRM    P2001      Term    Virtual alarm clock (4.2BSD)
SIGXCPU      P2001      Core    CPU time limit exceeded (4.2BSD); see setrlimit(2)
SIGXFSZ      P2001      Core    File size limit exceeded (4.2BSD); see setrlimit(2)
SIGWINCH       -        Ign     Window resize signal (4.3BSD, Sun)

Real-time signals

范围

特性

谁发给的进程

什么时候发的

如何处理signal

默认处理行为

Term   Default action is to terminate the process.
Ign    Default action is to ignore the signal.
Core   Default action is to terminate the process and dump core
Stop   Default action is to stop the process.
Cont   Default action is to continue the process if it is currently stopped.

用户可以自定义信号处理函数

如何自定义处理函数

注册自定义信号处理函数

#include <iostream>
#include <signal.h>
#include <unistd.h>

volatile bool s_quit = false;
void SignalHandler(int signal) {
    std::cout << "Receive signal : " << signal << "\n";
    if(signal == SIGINT) {
        std::cout << "Receive Ctrl + C!\n";
    } else if (signal == SIGRTMIN) {
        s_quit = true;
        std::cout << "Exit gracefully!\n";
    }
}

int main(int arg, char *args[]) {
    //注册信号
    signal(SIGINT, SignalHandler);
    signal(SIGRTMIN, SignalHandler);
    while (!s_quit) {
        sleep(1);
    }
    return 0;
}

volatile关键字

原因

作用

原文:https://www.cnblogs.com/jamgun/p/14865007.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!