signal设置对某一信号对应动作,signal机制可以被理解成进程的软中断
man 一下
SYNOPSIS
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
DESCRIPTION
The behavior of signal() varies across UNIX versions, and has also var‐
ied historically across different versions of Linux. Avoid its use:
use sigaction(2) instead. See Portability below.
signal() sets the disposition of the signal signum to handler, which is
either SIG_IGN, SIG_DFL, or the address of a programmer-defined func‐
tion (a "signal handler").
If the signal signum is delivered to the process, then one of the fol‐ 第一个参数signum:指明了所要处理的信号类型,它可以取除了SIGKILL和SIGSTOP外的任何一种信号。
lowing happens:
* If the disposition is set to SIG_IGN, then the signal is ignored.
* If the disposition is set to SIG_DFL, then the default action asso‐
ciated with the signal (see signal(7)) occurs.
* If the disposition is set to a function, then first either the dis‐
position is reset to SIG_DFL, or the signal is blocked (see Porta‐
bility below), and then handler is called with argument signum. If
invocation of the handler caused the signal to be blocked, then the
signal is unblocked upon return from the handler.
The signals SIGKILL and SIGSTOP cannot be caught or ignored.
RETURN VALUE
signal() returns the previous value of the signal handler, or SIG_ERR
on error. In the event of an error, errno is set to indicate the
cause.
ERRORS
EINVAL signum is invalid.
实际操作:
#include <stdio.h>#include <unistd.h>#include <signal.h>void my_sig_func(int signo){printf("get a signal : %d\n", signo);}int main(int argc, char **argv){int i = 0;signal(SIGIO, my_sig_func);while (1) {printf("Hello, world %d!\n", i++);sleep(2);}return 0;}
一些常用的Signal :
具体的调用机制是怎么样子的呢?写在后面一篇韦东山学习里面。