四、基于多任务的并发服务器( 三 )


“嘿,父进程! 你创建的子进程已经终止了!”
此时父进程将暂时放下手头的工作,处理子进程终止的相关事宜 。为了实现这个方法,引入了信号处理机制 。此处的“信号”是在特定事件发生时由操作系统向进程发送的消息 。
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 /* Schedule an alarm.In SECONDS seconds, the process will get a SIGALRM.If SECONDS is zero, any currently scheduled alarm will be cancelled.The function returns the number of seconds remaining until the lastalarm scheduled would have signaled, or zero if there wasn't one.There is no return value to indicate an error, but you can set `errno'to 0 and check its value after calling `alarm', and this might tell you.The signal may come late due to processor scheduling.*/extern unsigned int alarm (unsigned int __seconds) __THROW;
返回0或者以秒为单位的距信号发送所剩事件 。

四、基于多任务的并发服务器

文章插图
在第一个等待中我按了CTRL+C,而且在等待过程中实际没有睡眠100s,在产生信号时,为了调用信号处理器,将唤醒由于调用sleep函数而进入阻塞状态的进程 。而进程一旦被唤醒,就不会再进入睡眠状态了 。
2、利用函数进行信号处理