“嘿,父进程! 你创建的子进程已经终止了!”
此时父进程将暂时放下手头的工作,处理子进程终止的相关事宜 。为了实现这个方法,引入了信号处理机制 。此处的“信号”是在特定事件发生时由操作系统向进程发送的消息 。
1、信号与函数
#include /* Type of a signal handler.*/typedef void (*__sighandler_t) (int);/* Set the handler for the signal SIG to HANDLER, returning the oldhandler, or SIG_ERR on error.By default `signal' has the BSD semantic.*/extern __sighandler_t signal (int __sig, __sighandler_t __handler)
sig:特殊情况消息
:特殊情况下将要调用的函数的地址值
特效情况消息有很多中
/* We define here all the signal names listed in POSIX (1003.1-2008);as of 1003.1-2013, no additional signals have been added by POSIX.We also define here signal names that historically exist in everyreal-world POSIX variant (e.g. SIGWINCH).Signals in the 1-15 range are defined with their historical numbers.For other signals, we use the BSD numbers.There are two unallocated signal numbers in the 1-31 range: 7 and 29.Signal number 0 is reserved for use as kill(pid, 0), to test whethera process exists without sending it a signal.*//* ISO C99 signals.*/#defineSIGINT2/* Interactive attention signal.*/#defineSIGILL4/* Illegal instruction.*/#defineSIGABRT6/* Abnormal termination.*/#defineSIGFPE8/* Erroneous arithmetic operation.*/#defineSIGSEGV11/* Invalid access to storage.*/#defineSIGTERM15/* Termination request.*//* Historical signals specified by POSIX. */#defineSIGHUP1/* Hangup.*/#defineSIGQUIT3/* Quit.*/#defineSIGTRAP5/* Trace/breakpoint trap.*/#defineSIGKILL9/* Killed.*/#define SIGBUS10/* Bus error.*/#defineSIGSYS12/* Bad system call.*/#defineSIGPIPE13/* Broken pipe.*/#defineSIGALRM14/* Alarm clock.*//* New(er) POSIX signals (1003.1-2008, 1003.1-2013).*/#defineSIGURG16/* Urgent data is available at a socket.*/#defineSIGSTOP17/* Stop, unblockable.*/#defineSIGTSTP18/* Keyboard stop.*/#defineSIGCONT19/* Continue.*/#defineSIGCHLD20/* Child terminated or stopped.*/#defineSIGTTIN21/* Background read from control terminal.*/#defineSIGTTOU22/* Background write to control terminal.*/#defineSIGPOLL23/* Pollable event occurred (System V).*/#defineSIGXCPU24/* CPU time limit exceeded.*/#defineSIGXFSZ25/* File size limit exceeded.*/#defineSIGVTALRM26/* Virtual timer expired.*/#defineSIGPROF27/* Profiling timer expired.*/#defineSIGUSR130/* User-defined signal 1.*/#defineSIGUSR231/* User-defined signal 2.*//* Nonstandard signals found in all modern POSIX systems(including both BSD and Linux).*/#defineSIGWINCH28/* Window size change (4.3 BSD, Sun).*/
signo
:已到通过调用alarm函数注册的时间 。
:输出CTRL+C
:子进程终止
下面两种就是信号注册过程,注册好信号后,发送信号时,操作系统将调用该信号对应的函数 。
(, );
(, );
使用alarm函数验证,
#include
返回0或者以秒为单位的距信号发送所剩事件 。
文章插图
在第一个等待中我按了CTRL+C,而且在等待过程中实际没有睡眠100s,在产生信号时,为了调用信号处理器,将唤醒由于调用sleep函数而进入阻塞状态的进程 。而进程一旦被唤醒,就不会再进入睡眠状态了 。
2、利用函数进行信号处理
- 如何更换win10电脑开机头像
- 笔记本屏幕省电怎么设置
- 电脑任务栏怎么换颜色
- 苹果手机如何屏蔽陌生号码
- 「专题速递」数字人直播带货、传统行业数字化升级、远程协作中的低延时视频、地产物业
- 基于SSH的企业信息查询系统
- 金字塔有几个面 金字塔是四个面还是八个面
- web自动化之iframe页面切换
- 评论时间、用户ID、评论内容 用python爬取影评及影片信息
- WhatsApp 私有群营销方案