加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Signal Handling--ref

发布时间:2021-01-25 08:01:09 所属栏目:Linux 来源:网络整理
导读:signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asy

Setting this flag for a signal other thanSIGCHLDhas no effect.

intSA_ONSTACK
If this flag is set for a particular signal number,the system uses the signal stack when delivering that kind of signal. See section. If a signal with this flag arrives and you have not set a signal stack,the system terminates the program withSIGILL.
intSA_RESTART
This flag controls what happens when a signal is delivered during certain primitives (such asopen,readorwrite),and the signal handler returns normally. There are two alternatives: the library function can resume,or it can return failure with error codeEINTR.

The choice is controlled by theSA_RESTARTflag for the particular kind of signal that was delivered. If the flag is set,returning from a handler resumes the library function. If the flag is clear,returning from a handler makes the function fail. See section.

execfunction (see section),any signals that you've defined your own handlers for revert to theirSIG_DFLhandling. (If you think about it a little,this makes sense; the handler functions from the old program are specific to that program,and aren't even present in the address space of the new program image.) Of course,the new program can establish its own handlers.

SIG_DFLorSIG_IGN,as appropriate. It's a good idea to check to make sure that the shell has not set up an initial action ofSIG_IGNbefore you establish your own signal handlers.

SIGHUP,but not ifSIGHUPis currently ignored:

...
struct sigaction temp;

sigaction (SIGHUP,&temp);

if (temp.sa_handler != SIG_IGN)
{
temp.sa_handler = handle_sighup;
sigemptyset (&temp.sa_mask);
sigaction (SIGHUP,&temp,NULL);
}

signalorsigactionfunctions.

signalorsigactionto tell the operating system to call it when a signal arrives. This is known asestablishingthe handler. See section.

    You can have the handler function note that the signal arrived by tweaking some global data structures,and then return normally.
  • You can have the handler function terminate the program or transfer control to a point where it can recover from the situation that caused the signal.

SIGALRMand the I/O and interprocess communication signals. But a handler forSIGINTmight also return normally after setting a flag that tells the program to exit at a convenient time.

sig_atomic_tfor reasons described in section.

SIGALRMsignal has arrived. This technique is useful because it allows the iteration in progress when the signal arrives to complete before the loop exits.

#include 
#include 
#include 

/ This flag controls termination of the main loop. /
volatile sig_atomic_t keep_going = 1;

/ The signal handler just clears the flag and re-enables itself. /
void
catch_alarm (int sig)
{
keep_going = 0;
signal (sig,catch_alarm);
}

void
do_stuff (void)
{
puts ("Doing stuff while waiting for alarm....");
}

int
main (void)
{
/ Establish a handler for SIGALRM signals. /
signal (SIGALRM,catch_alarm);

/ Set an alarm to go off in a little while. /
alarm (2);

/ Check the flag once in a while to see when to quit. /
while (keep_going)
do_stuff ();

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读