1.信号的基本概念:
信号全称为软中断信号,也有人称作软中断,信号机制是进程之间相互传递消息的一种方法。
2.信号的作用:
进程之间可以互相通过系统调用kill发送软中断信号,通知进程发生了某个事件。
内核也可以因为内部事件而给进程发送信号,通知进程发生了某个事件
例如:中断用户键入中断键(CTRL+C),则会通过信号机制停止一个程序。
3.信号发生的条件:
1. 当用户按某些终端键时,如在使用vim时可用CTRL+Z将vim放在后台
2. 硬件异常产生信号:除数为零,无效的内存引用等
3. 进程调用kill()函数可将信号发送给另一个进程或进程组。如:kill(gepid(),SIGABRT)限制是:接收信号的进程和发送信号的进程的所有者必须相同。
4. kill命令。常用此命令终止一个失控的后台进程。
5. 当检测到某种软件已经发生。如SIGALRM是进程设置的闹钟超时时产生。
4.内核在信号出现时的三种处理方式:
1忽略此信号
2捕捉此信号
3执行系统默认动作
#defineSIG_ERR ((__sighandler_t) -1) /* Error return. */
#defineSIG_DFL ((__sighandler_t) 0) /* Default action. */
#defineSIG_IGN ((__sighandler_t) 1) /* Ignore signal. */
Signal函数的返回值和第二个参数可以用他们表示。
5. 信号与中断的相似点:
1 .采用了相同的异步通信方式;
2.当检测出有信号或中断请求时,都暂停正在执行的程序而转去执行相应的处理程序;
3.都在处理完毕后返回到原来的断点;
4.对信号或中断都可进行屏蔽。
6. 信号与中断的区别:
1.中断有优先级,而信号没有优先级,所有的信号都是平等的;
2.信号处理程序是在用户态下运行的,而中断处理程序是在核心态下运行;
3.中断响应是及时的,而信号响应通常都有较大的时间延迟。
7.信号的头文件
这些信号都被定义为正整数,而且不存在信号为零的信号
/* Signal number definitions. Linux version.
Copyright (C) 1995,1996,1997,1998,1999,2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
. */
#ifdef _SIGNAL_H
/* Fake signal functions. */
#define SIG_ERR ((__sighandler_t) -1) /* Error return. */
#define SIG_DFL ((__sighandler_t) 0) /* Default action. */
#define SIG_IGN ((__sighandler_t) 1) /* Ignore signal. */
#ifdef __USE_UNIX98
# define SIG_HOLD ((__sighandler_t) 2) /* Add signal to hold mask. */
#endif
/* Signals. */
#define SIGHUP 1 /* Hangup (POSIX). */
#define SIGINT 2 /* Interrupt (ANSI). */
#define SIGQUIT 3 /* Quit (POSIX). */
#define SIGILL 4 /* Illegal instruction (ANSI). */
#define SIGTRAP 5 /* Trace trap (POSIX). */
#define SIGABRT 6 /* Abort (ANSI). */
#define SIGIOT 6 /* IOT trap (4.2 BSD). */
#define SIGBUS 7 /* BUS error (4.2 BSD). */
#define SIGFPE 8 /* Floating-point exception (ANSI). */
#define SIGKILL 9 /* Kill, unblockable (POSIX). */
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
#define SIGSEGV 11 /* Segmentation violation (ANSI). */
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
#define SIGPIPE 13 /* Broken pipe (POSIX). */
#define SIGALRM 14 /* Alarm clock (POSIX). */
#define SIGTERM 15 /* Termination (ANSI). */
#define SIGSTKFLT 16 /* Stack fault. */
#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */
#define SIGCHLD 17 /* Child status has changed (POSIX). */
#define SIGCONT 18 /* Continue (POSIX). */
#define SIGSTOP 19 /* Stop, unblockable (POSIX). */
#define SIGTSTP 20 /* Keyboard stop (POSIX). */
#define SIGTTIN 21 /* Background read from tty (POSIX). */
#define SIGTTOU 22 /* Background write to tty (POSIX). */
#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */
#define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */
#define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */
#define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */
#define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */
#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */
#define SIGPOLL SIGIO /* Pollable event occurred (System V). */
#define SIGIO 29 /* I/O now possible (4.2 BSD). */
#define SIGPWR 30 /* Power failure restart (System V). */
#define SIGSYS 31 /* Bad system call. */
#define SIGUNUSED 31
#define _NSIG 65 /* Biggest signal number + 1
(including real-time signals). */
#define SIGRTMIN (__libc_current_sigrtmin ())
#define SIGRTMAX (__libc_current_sigrtmax ())
/* These are the hard limits of the kernel. These values should not be
used directly at user level. */
#define __SIGRTMIN 32
#define __SIGRTMAX (_NSIG - 1)
#endif /* included. */