The return value and error conditions are the same as forpause .
sigsuspend,you can replace thepause orsleep loop 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,theSIGUSR1 signal is once again blocked. The second call tosigprocmask is necessary to explicitly unblock this signal.
whileloop is necessary at all,since the program is apparently only waiting for oneSIGUSR1 signal. The answer is that the mask passed tosigsuspend permits 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 usemalloc to allocate the space for the stack. Then callsigaltstack orsigstack to tell the system to use that space for the signal stack.
sigstackis the older interface,which comes from 4.2 BSD.sigaltstack is the newer interface,and comes from 4.4 BSD. Thesigaltstack interface 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 using
SIGSTKSZ forss_size is 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 allocateMINSIGSTKSZ additional bytes for the signal stack and increasess_size accordinly.
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)
-
The
sigaltstack function 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. (编辑:青岛站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|