Ifsignal can't honor the request,it returnsSIG_ERR instead. The followingerrno error conditions are defined for this function:
EINVAL
- You specified an invalidsignum; or you tried to ignore or provide a handler for
SIGKILL orSIGSTOP .
#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)
-
The
ssignal function 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 from
signal to indicate an error.
sigactionfunction has the same basic effect assignal : to specify how a signal should be handled by the process. However,sigaction offers more control,at the expense of more complexity. In particular,sigaction allows 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 type
struct sigaction are used in thesigaction function 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 the
signal function. 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 in
sa_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 section
sigaction .
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 the
signal function'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.) (编辑:威海站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|