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,并且不能调用 不可重入函数
 

Linux System Programming 学习笔记(十) 信号的更多相关文章

  1. Linux System Programming 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

  2. Linux System Programming 学习笔记(一) 介绍

    1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...

  3. Linux System Programming 学习笔记(十一) 时间

    1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...

  4. Linux System Programming 学习笔记(七) 线程

    1. Threading is the creation and management of multiple units of execution within a single process 二 ...

  5. Linux System Programming 学习笔记(六) 进程调度

    1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...

  6. Linux System Programming 学习笔记(五) 进程管理

    1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...

  7. Linux System Programming 学习笔记(四) 高级I/O

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

  8. Linux System Programming 学习笔记(九) 内存管理

    1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...

  9. Linux System Programming 学习笔记(八) 文件和目录管理

    1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...

随机推荐

  1. 字符串的驻留(String Interning)

    http://www.cnblogs.com/artech/archive/2007/03/04/663728.html 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...

  2. 完整的.h宏定义

    ##封装中的要点 __declspec(dllexport) 表明函数为导出函数 * `extern "C"` 导出的函数名与声明一致,否则导出函数名无法正常使用 当然,也可以通过 ...

  3. OC和C++的混用1

    //Objective-C类 /*在混用之前需要做一步非常重要的事:不是代码而是编译器选项,在做混合编译之前一定要把编译器的Compile Sources As选项改为Objective C++. 修 ...

  4. C# 调用腾讯地图WebService API获取距离(一对多)

    官方文档地址:https://lbs.qq.com/webservice_v1/guide-distance.html 代码: /// <summary> /// 获取距离最近的点的经纬度 ...

  5. centos启动流程

    centos6启动流程 1.主板,post加电自检,检查硬件环境 2.主板选择一个硬盘进行引导,执行mbr446 grub stage1 3.grub stage1.5 加载/boot分区文件系统驱动 ...

  6. CNCF 有哪些具体的项目内容?

    前言:CNCF(Cloud Native Computing Foundation)于 2015 年 7 月成立,隶属于 Linux 基金会,初衷围绕“云原生”服务云计算,致力于维护和集成开源技术,支 ...

  7. MySQL如何复制一个表

    MySQL如何复制一个表 1 复制 employee 表 => employee2 () create table employee2 like employee () insert into ...

  8. 【mac】【nginx】开机重启

    homebrew.mxcl.nginx.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  9. COMP9021——6.3

    有关yield的用法简介以及图灵机 第一节课大体没有太大变化,前半节课为了给图灵机的讲解做铺垫引入了yield.数组.字符串和文件等都是一个可迭代的对象,但由于它们的所有数据都存储与内存中,对内存的消 ...

  10. vue创建路由,axios前后台交互,element-ui配置使用,django contentType组件

    vue中创建路由 每一个vue组件都有三部分组成 template:放html代码 script:放js相关 style:放css相关 vue中创建路由 1.先创建组件 Course.vue 2.ro ...