程式語言 - GNU - C/C++ - Signal



NumberNameFunction
1SIGHUPHangup detected on controlling terminal or death of controlling process
2SIGINTInterrupt from keyboard (Ctrl+c)
3SIGQUITQuit from keyboard
4SIGILLIllegal instruction
5SIGTRAPTrace trap
6SIGABRTAbort signal from abort
6SIGIOTI/O trap
7SIGBUSBus error
8SIGFPEFloating point exception
9SIGKILLKill signal
10SIGUSR1User define signal 1
11SIGSEGVInvalid memory reference
12SIGUSR2User define signal 2
13SIGPIPEWrite to pipe with no readers
14SIGALRMTimer signal from alarm
15SIGTERMTermination signal
16SIGTKFLTStack fault
17SIGCHLDChild status has changed
18SIGCONTContinue after stop
19SIGSTOPStop(Ctrl+z)
20SIGTSTPStop signal generated from keyboard
21SIGTTINBackground read attempted from control terminal
22SIGTTOUBackground write attempted to control terminal
23SIGURGUrgent condition present on socket
24SIGXCPUCPU time limit exceeded
25SIGXFSZFile size limit exceeded
26SIGVTALRMVirtual time alarm
27SIGPROFProfiling timer alarm
28SIGWINCHWindow changed
29SIGIOI/O is possible on a descriptor
29SIGPOLLI/O is possible on a descriptor
30SIGPWRUPS power Failure
31SIGUNUSEDUnused

main.c

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
 
void handle_signal(int sig)
{
    printf("User break\n");
    signal(SIGINT, SIG_DFL);
}
 
int main(int argc, char *argv[])
{
    signal(SIGINT, handle_signal);
    pause();
    return 0;
}

編譯、執行

$ gcc main.c -o main
$ ./main 
    ^C User break