Linux信号,signal和sigaction(一)[通俗易懂]

Linux信号,signal和sigaction(一)[通俗易懂]
信号是Unix和Linux系统响应某些条件而产生的一个事件。接收到该信号的进程会相应地采取一些操作。
每个信号都有一个数字编码。
 
         实例:Ctrl-C的工作原理
        1.用户输入ctrl-c<

大家好,又见面了,我是你们的朋友全栈君。

信号是Unix和Linux系统响应某些条件而产生的一个事件。接收到该信号的进程会相应地采取一些操作。

每个信号都有一个数字编码。

 

          实例:Ctrl-C的工作原理

         1.用户输入ctrl-c

          2.驱动程序受到字符

            3.匹配VINTR和ISIG的字符被开启

              4.驱动程序调用信号系统

                 5.信号系统发送SIGINT到进程

                    6.进程受到SIGINT

                       7.进程消亡

 

信号来自于内核,生成信号的请求可以来源于用户,内核,进程

用户:比如Ctrl-C,Ctrl-/.当然这些也可以改变(使用stty改变VINTR控制字符)

内核:非法段存取,浮点溢出等进程运行出错。

进程:一个进程可以通过系统调用kill给另一个进程发送信号;进程间也可以通过信号通信。

 

注:同步信号-–由进程的某个操作产生的信号,比如被零除;

      异步信号-–由进程外部的事件引起的信号,比如用户的击键;

 

信号的名称在signal.h中定义(一般/usr/include/signal.h)。

 

man 7 signal 可以查看signal的相关知识和用法等。

常见的有:

caoli@caoli-laptop:~$ man 7 signal

 First the signals described in the original POSIX.1-1990 standard.

       Signal     Value     Action   Comment
       ──────────────────────────────────────────────────────────────────────
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard  
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no
                                     readers
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
   SIGCONT   19,18,25    Cont    Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at tty
       SIGTTIN   21,21,26    Stop    tty input for background process
       SIGTTOU   22,22,27    Stop    tty output for background process

       The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

       Next  the  signals  not  in  the POSIX.1-1990 standard but described in
       SUSv2 and POSIX.1-2001.

       Signal       Value     Action   Comment
       ────────────────────────────────────────────────────────────────────
       SIGBUS      10,7,10     Core    Bus error (bad memory access)
       SIGPOLL                 Term    Pollable event (Sys V).
                                       Synonym for SIGIO
       SIGPROF     27,27,29    Term    Profiling timer expired
       SIGSYS      12,-,12     Core    Bad argument to routine (SVr4)
       SIGTRAP        5        Core    Trace/breakpoint trap
       SIGURG      16,23,21    Ign     Urgent condition on socket (4.2BSD)
       SIGVTALRM   26,26,28    Term    Virtual alarm clock (4.2BSD)
       SIGXCPU     24,24,30    Core    CPU time limit exceeded (4.2BSD)
 SIGXFSZ     25,25,31    Core    File size limit exceeded (4.2BSD)

       Up to and including Linux 2.2, the default behavior for  SIGSYS,  SIGX‐
       CPU,  SIGXFSZ,  and (on architectures other than SPARC and MIPS) SIGBUS
       was to terminate the process (without a core  dump).   (On  some  other
       Unix systems the default action for SIGXCPU and SIGXFSZ is to terminate
       the  process  without  a  core  dump.)   Linux  2.4  conforms  to   the
       POSIX.1-2001  requirements  for  these signals, terminating the process
       with a core dump.

       Next various other signals.

       Signal       Value     Action   Comment

       ────────────────────────────────────────────────────────────────────
       SIGIOT         6        Core    IOT trap. A synonym for SIGABRT
       SIGEMT       7,-,7      Term
       SIGSTKFLT    -,16,-     Term    Stack fault on coprocessor (unused)
       SIGIO       23,29,22    Term    I/O now possible (4.2BSD)
       SIGCLD       -,-,18     Ign     A synonym for SIGCHLD
       SIGPWR      29,30,19    Term    Power failure (System V)
       SIGINFO      29,-,-             A synonym for SIGPWR
   SIGLOST      -,-,-      Term    File lock lost
       SIGWINCH    28,28,20    Ign     Window resize signal (4.3BSD, Sun)
       SIGUNUSED    -,31,-     Term    Unused signal (will be SIGSYS)

       (Signal 29 is SIGINFO / SIGPWR on an alpha but SIGLOST on a sparc.)

       SIGEMT is not specified in POSIX.1-2001, but  nevertheless  appears  on
       most  other Unix systems, where its default action is typically to ter‐
       minate the process with a core dump.

       SIGPWR (which is not specified in POSIX.1-2001) is typically ignored by
       default on those other Unix systems where it appears.

       SIGIO (which is not specified in POSIX.1-2001) is ignored by default on
       several other Unix systems.
进程处理信号的方式有三种:默认,忽略,调用一个函数。

1.默认:(通常是消亡)

针对SIGINT来说,它的默认处理是消亡,可以用以下调用来恢复默认值

signal(SIGINT,SIG_DFL);

 

2.忽略信号

程序可以通过以下调用来告诉内核他忽略SIGINT信号。

signal(SIGINT,SIG_IGN);

 

3.调用函数

程序告诉内核,当信号来时,应该调用哪个函数。那个函数称为信号处理函数

signal(signum,functionname);

 

关于signal库函数

#include <signal.h>

 

void (*signal(int sig,void (*func)(int))) (int);

遇到错误返回-1

执行成功返回prevcation

 

func为信号处理函数,也可以用SIG_DFL和SIG_IGN来替代。

 

 

signal(SIGINT,SIG_DFL)里面可以用SIG_IGN或者f替代。

 

关于发送信号和sigaction见下节

 

 

我好累阿,回去睡觉。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/136740.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3

    Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3

    2022年1月7日
    47
  • C#编程入门_ToArray和CopyTo的区别_22「建议收藏」

    C#编程入门_ToArray和CopyTo的区别_22「建议收藏」ToArray是将一个集合转化成一个数组,该数组是进行ToArray操作的一个产物CopyTo(T[]array)是一个将集合中的数据复制到一个一个已经存在的数组中,只不过是对数组中的元素进行修改usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.T

    2022年5月15日
    37
  • 数据中心的变化需要更多有效冷却方式[通俗易懂]

    数据中心的变化需要更多有效冷却方式[通俗易懂]数据中心的变化需要更多有效冷却方式

    2022年4月20日
    39
  • 常用进程调度算法_进程调度算法例题

    常用进程调度算法_进程调度算法例题写在前面:我是【程序员宝藏】的宝藏派发员,致力于创作原创干货。我热爱技术、热爱开源与分享,创作的【计算机基础面试问题】系列文章和【计算机基础主干知识】系列文章广受好评!后期会创作更多优质原创系列文章!如果您对计算机基础知识、编程等感兴趣,可以关注我,我们一起成长!本人力荐:如果觉得CSDN排版不够美观,欢迎来我的个人原创公zong号【程序员宝藏】(号如其名,诚不欺你!)查看有红色重点标记和排版美观的全系列文章(不细你来找我要红包)参考链接:TCP三次握手四次挥手好多同学问我要pdf版,我干脆.

    2022年9月29日
    3
  • 如何将一个数组转成集合数组_java数组转list集合

    如何将一个数组转成集合数组_java数组转list集合如何将一个数组转成集合?java.util.Arrays类为我们提供了一个方法Arrays.asList(T…a)此方法可以将数组转换成一个arrayList集合使用方法: publicstaticvoidmain(String[]args){ String[]array={“张三”,”李四”,”王五”}; List<String>asList=Arrays.asList(array); System.out.println(asList.toStr

    2025年12月10日
    5
  • Linux系统安装RabbitMQ详细步骤

    Linux系统安装RabbitMQ详细步骤Erlang官方下载地址:https://www.erlang.org/downloadsRabbitMQ官方下载地址:https://www.rabbitmq.com/download.html一安装依赖包安装RabbitMQ之前必须要先安装所需要的依赖包可以使用下面的一次性安装命令yuminstallgccglibc-develmakencurses-developenssl-develxmlto-y二安装Erlang(1

    2022年4月9日
    106

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号