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

Signal Handling--ref

发布时间:2021-01-24 19:36:20 所属栏目:Linux 来源:网络整理
导读:副标题#e# A program error such as dividing by zero or issuing an address outside the valid range. A user request to interrupt or terminate the program. Most environments are set up to let a user suspend the program by typing C-z ,or termi

Ifsignalcan't honor the request,it returnsSIG_ERRinstead. The followingerrnoerror conditions are defined for this function:

EINVAL
You specified an invalidsignum; or you tried to ignore or provide a handler forSIGKILLorSIGSTOP.

#include 

void
termination_handler (int signum)
{
struct temp_file *p;

for (p = temp_file_list; p; p = p->next)
unlink (p->name);
}

int
main (void)
{
...
if (signal (SIGINT,termination_handler) == SIG_IGN)
signal (SIGINT,SIG_IGN);
if (signal (SIGHUP,termination_handler) == SIG_IGN)
signal (SIGHUP,SIG_IGN);
if (signal (SIGTERM,termination_handler) == SIG_IGN)
signal (SIGTERM,SIG_IGN);
...
}

SIGQUITor the program error signals in this example because these are designed to provide information for debugging (a core dump),and the temporary files may give useful information.

sighandler_tssignal(intsignum,sighandler_taction)
Thessignalfunction does the same thing assignal; it is provided only for compatibility with SVID.
sighandler_tSIG_ERR
The value of this macro is used as the return value fromsignalto indicate an error.

sigactionfunction has the same basic effect assignal: to specify how a signal should be handled by the process. However,sigactionoffers more control,at the expense of more complexity. In particular,sigactionallows you to specify additional flags to control when the signal is generated and how the handler is invoked.

sigactionfunction is declared in`signal.h'.

struct sigaction
Structures of typestruct sigactionare used in thesigactionfunction to specify all the information about how to handle a particular signal. This structure contains at least the following members:
sighandler_t sa_handler
This is used in the same way as theactionargument to thesignalfunction. The value can beSIG_DFL,SIG_IGN,or a function pointer. See section.
sigset_t sa_mask
This specifies a set of signals to be blocked while the handler runs. Blocking is explained in section. Note that the signal that was delivered is automatically blocked by default before its handler is started; this is true regardless of the value insa_mask. If you want that signal not to be blocked within its handler,you must write code in the handler to unblock it.
int sa_flags
This specifies various flags which can affect the behavior of the signal. These are described in more detail in sectionsigaction.
intsigaction(intsignum,const struct sigaction *action,struct sigaction *old-action)
Theactionargument is used to set up a new action for the signalsignum,while theold-actionargument is used to return information about the action previously associated with this symbol. (In other words,old-actionhas the same purpose as thesignalfunction's return value--you can check to see what the old action in effect for the signal was,and restore it later if you want.)

(编辑:成都站长网)

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

热点阅读