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

Signal Handling--ref

发布时间:2021-01-24 20:17:15 所属栏目: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

The return value and error conditions are the same as forpause.

sigsuspend,you can replace thepauseorsleeploop in the previous section with something completely reliable:

sigset_t mask,oldmask;

...

/ Set up the mask of signals to temporarily block. /
sigemptyset (&mask);
sigaddset (&mask,SIGUSR1);

...

/ Wait for a signal to arrive. /
sigprocmask (SIG_BLOCK,&mask,&oldmask);
while (!usr_interrupt)
sigsuspend (&oldmask);
sigprocmask (SIG_UNBLOCK,NULL);

sigsuspendreturns,it resets the process's signal mask to the original value,the value from before the call tosigsuspend---in this case,theSIGUSR1signal is once again blocked. The second call tosigprocmaskis necessary to explicitly unblock this signal.

whileloop is necessary at all,since the program is apparently only waiting for oneSIGUSR1signal. The answer is that the mask passed tosigsuspendpermits the process to be woken up by the delivery of other kinds of signals,as well--for example,job control signals. If the process is woken up by a signal that doesn't setusr_interrupt,it just suspends itself again until the "right" kind of signal eventually arrives.

SIGSTKSZis defined to a canonical size for signal stacks. You can usemallocto allocate the space for the stack. Then callsigaltstackorsigstackto tell the system to use that space for the signal stack.

sigstackis the older interface,which comes from 4.2 BSD.sigaltstackis the newer interface,and comes from 4.4 BSD. Thesigaltstackinterface has the advantage that it does not require your program to know which direction the stack grows,which depends on the specific machine and operating system.

struct sigaltstack
This structure describes a signal stack. It contains the following members:
void *ss_sp
This points to the base of the signal stack.
size_t ss_size
This is the size (in bytes) of the signal stack which`ss_sp'points to. You should set this to however much space you allocated for the stack. There are two macros defined in`signal.h'that you should use in calculating this size:
SIGSTKSZ
This is the canonical size for a signal stack. It is judged to be sufficient for normal uses.
MINSIGSTKSZ
This is the amount of signal stack space the operating system needs just to implement signal delivery. The size of a signal stackmustbe greater than this. For most cases,just usingSIGSTKSZforss_sizeis sufficient. But if you know how much stack space your program's signal handlers will need,you may want to use a different size. In this case,you should allocateMINSIGSTKSZadditional bytes for the signal stack and increasess_sizeaccordinly.
int ss_flags
This field contains the bitwise OR of these flags:
SA_DISABLE
This tells the system that it should not use the signal stack.
SA_ONSTACK
This is set by the system,and indicates that the signal stack is currently in use. If this bit is not set,then signals will be delivered on the normal user stack.
intsigaltstack(const struct sigaltstack *stack,struct sigaltstack *oldstack)
Thesigaltstackfunction specifies an alternate stack for use during signal handling. When a signal is received by the process and its action indicates that the signal stack is used,the system arranges a switch to the currently installed signal stack while the handler for that signal is executed.

(编辑:青岛站长网)

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

热点阅读