Linux System Programming 学习笔记(十) 信号
2024-08-24 07:52:06
1. 信号是软中断,提供处理异步事件的机制
异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0)
内核使用以下三种方法之一来处理信号:
(1) 忽略该信号。SIGKILL和SIGSTOP不能被忽略。
(2) 捕捉并且处理该信号。The kernel will suspend execution of the process’s current code path and jump to a previously registered function.
SIGKILL和SIGSTOP不能被捕捉
(2) 执行默认操作。
SIGCHLD:进程终止时,内核向其父进程发送SIGCHLD信号,默认是忽略,如果父进程需要子进程终止信息,而需要显式处理,通常是调用wait函数
SIGINT:用户输入中断字符 Ctrl-C
SIGKILL,SIGSTOP:kill系统调用发出的信号,不能被忽略,不能被捕捉,结果总是终止进程
SIGSEGV:段错误
2. 基本的信号管理
#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal (int signo, sighandler_t handler);
signal() removes the current action taken on receipt of the signal signo and instead handles the signal with the signal handler specified by handler
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
/* handler for SIGINT and SIGTERM */
static void signal_handler (int signo)
{
if (signo == SIGINT)
printf ("Caught SIGINT!\n");
else if (signo == SIGTERM)
printf ("Caught SIGTERM!\n");
else {
/* this should never happen */
fprintf (stderr, "Unexpected signal!\n");
exit (EXIT_FAILURE);
}
exit (EXIT_SUCCESS);
}
int main (void)
{
/*
* Register signal_handler as our signal handler
* for SIGINT.
*/
if (signal (SIGINT, signal_handler) == SIG_ERR) {
fprintf (stderr, "Cannot handle SIGINT!\n");
exit (EXIT_FAILURE);
}
/*
* Register signal_handler as our signal handler
* for SIGTERM.
*/
if (signal (SIGTERM, signal_handler) == SIG_ERR) {
fprintf (stderr, "Cannot handle SIGTERM!\n");
exit (EXIT_FAILURE);
}
/* Reset SIGPROF's behavior to the default. */
if (signal (SIGPROF, SIG_DFL) == SIG_ERR) {
fprintf (stderr, "Cannot reset SIGPROF!\n");
exit (EXIT_FAILURE);
}
/* Ignore SIGHUP. */
if (signal (SIGHUP, SIG_IGN) == SIG_ERR) {
fprintf (stderr, "Cannot ignore SIGHUP!\n");
exit (EXIT_FAILURE);
}
for (;;)
pause ();
return ;
}
#include <signal.h>
int sigaction (int signo, const struct sigaction *act, struct sigaction *oldact); struct sigaction {
void (*sa_handler)(int); /* signal handler or action */
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask; /* signals to block */
int sa_flags; /* flags */
void (*sa_restorer)(void); /* obsolete and non-POSIX */
};
sigaction() changes the behavior of the signal identified by signo, signo不能为SIGKILL和SIGSTOP
If act is not NULL, the system call changes the current behavior of the signal as specified by act
信号行为的继承:
3. 发送信号
int ret;
ret = kill (, SIGHUP);
if (ret)
perror ("kill");
上述代码表示:向pid为1722的进程发送SIGHUP信号
上述代码与以下shell语句等同:
$ kill -HUP
/* a simple way for a process to send a signal to itself */
#include <signal.h>
int raise (int signo);
raise (signo);
等同于:
kill (getpid (), signo);
4. 可重入
A reentrant function is a function that is safe to call from within itself (or concurrently, from another thread in the same process).
为了确保可重入,函数不能操作static变量,只能操作 stack-allocated data,并且不能调用 不可重入函数
最新文章
- 模型(modle)
- 配置opencv
- 认识copy关键
- hdu 3357 水题
- Cocos2d-x win7 + vs2010 配置图文详解(亲测)
- ArrayList和LinkedList和Vector源码分析
- Python学习-使用opencv-python提取手掌和手心及部分掌纹
- mac pro换屏指南
- Java多线程循环打印ABC的5种实现方法
- JS方法转字符串
- (转)C#串口SerialPort常用属性方法
- Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用
- BZOJ.1076.[SCOI2008]奖励关(概率DP 倒推)
- 数据流-------C#文件和byte[]互换问题
- day41 mysql 学习 练习题 重要*****
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明
- plsql developer 64位 注册码
- windows下端口占用解决方法-查看和杀死占用端口进程
- ubuntu安装mysql-python
- bzoj 3246 [Ioi2013]Dreaming 贪心