/ After a while,check to see whether any signals are pending. /
sigpending (&waiting_mask);
if (sigismember (&waiting_mask,SIGINT)) {
/ User has tried to kill the process. /
}
else if (sigismember (&waiting_mask,SIGTSTP)) {
/ User has tried to stop the process. /
}
SIGINTsignal is pending when anotherSIGINT signal arrives,your program will probably only see one of them when you unblock this signal.
sigpendingfunction is new in POSIX.1. Older systems have no equivalent facility.
/* If this flag is nonzero,don't handle the signal right away. */
volatile sig_atomic_t signal_pending;
/ This is nonzero if a signal arrived and was not handled. /
volatile sig_atomic_t defer_signal;
void
handler (int signum)
{
if (defer_signal)
signal_pending = signum;
else
... / "Really" handle the signal. /
}
...
void
update_mumble (int frob)
{
/ Prevent signals from having immediate effect. /
defer_signal++;
/ Now update mumble ,without worrying about interruption. /
mumble.a = 1;
mumble.b = hack ();
mumble.c = frob;
/ We have updated mumble . Handle any signal that came in. /
defer_signal--;
if (defer_signal == 0 && signal_pending != 0)
raise (signal_pending);
}
signal_pending. That way,we can handle several types of inconvenient signals with the same mechanism.
defer_signalso that nested critical sections will work properly; thus,ifupdate_mumble were called withsignal_pending already nonzero,signals would be deferred not only withinupdate_mumble ,but also within the caller. This is also why we do not checksignal_pending ifdefer_signal is still nonzero.
defer_signalrequire more than one instruction; it is possible for a signal to happen in the middle. But that does not cause any problem. If the signal happens early enough to see the value from before the increment or decrement,that is equivalent to a signal which came before the beginning of the increment or decrement,which is a case that works properly.
defer_signalbefore testingsignal_pending ,because this avoids a subtle bug. If we did these things in the other order,like this,
if (defer_signal == 1 && signal_pending != 0)
raise (signal_pending);
defer_signal--;
ifstatement and the decrement would be effetively "lost" for an indefinite amount of time. The handler would merely setdefer_signal ,but the program having already tested this variable,it would not test the variable again.
defer_signalas a counter which must be tested along withsignal_pending . After all,testing for zero is cleaner than testing for one. But if you did not usedefer_signal as a counter,and gave it values of zero and one only,then either order might seem equally simple. This is a further advantage of using a counter fordefer_signal : it will reduce the chance you will write the code in the wrong order and create a subtle bug.)
pause
pause. Please read about its disadvantages,in the following section,before you use it.
intpause()
-
The
pause function suspends program execution until a signal arrives whose action is either to execute a handler function,or to terminate the process. (编辑:应用网_丽江站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|