📄 library_21.html
字号:
to <CODE>SIG_DFL</CODE> and the default action is to ignore that signal, thenany pending signals of that type are discarded (even if they areblocked). Discarding the pending signals means that they will never bedelivered, not even if you subsequently specify another action andunblock this kind of signal.<P>The <CODE>signal</CODE> function returns the action that was previously ineffect for the specified <VAR>signum</VAR>. You can save this value andrestore it later by calling <CODE>signal</CODE> again.<P>If <CODE>signal</CODE> can't honor the request, it returns <CODE>SIG_ERR</CODE>instead. The following <CODE>errno</CODE> error conditions are defined forthis function:<P><DL COMPACT><DT><CODE>EINVAL</CODE><DD>You specified an invalid <VAR>signum</VAR>; or you tried to ignore or providea handler for <CODE>SIGKILL</CODE> or <CODE>SIGSTOP</CODE>.</DL><P>Here is a simple example of setting up a handler to delete temporaryfiles when certain fatal signals happen:<P><PRE>#include <signal.h>voidtermination_handler (int signum){ struct temp_file *p; for (p = temp_file_list; p; p = p->next) unlink (p->name);}intmain (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); ...}</PRE><P>Note how if a given signal was previously set to be ignored, this codeavoids altering that setting. This is because non-job-control shellsoften ignore certain signals when starting children, and it is importantfor the children to respect this.<P>We do not handle <CODE>SIGQUIT</CODE> or the program error signals in thisexample because these are designed to provide information for debugging(a core dump), and the temporary files may give useful information.<P><A NAME="IDX1545"></A><U>Function:</U> sighandler_t <B>ssignal</B> <I>(int <VAR>signum</VAR>, sighandler_t <VAR>action</VAR>)</I><P>The <CODE>ssignal</CODE> function does the same thing as <CODE>signal</CODE>; it isprovided only for compatibility with SVID.<P><A NAME="IDX1546"></A><U>Macro:</U> sighandler_t <B>SIG_ERR</B><P>The value of this macro is used as the return value from <CODE>signal</CODE>to indicate an error.<P><A NAME="IDX1547"></A><H3><A NAME="SEC346" HREF="library_toc.html#SEC346" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC346">Advanced Signal Handling</A></H3><P>The <CODE>sigaction</CODE> function has the same basic effect as<CODE>signal</CODE>: to specify how a signal should be handled by the process.However, <CODE>sigaction</CODE> offers more control, at the expense of morecomplexity. In particular, <CODE>sigaction</CODE> allows you to specifyadditional flags to control when the signal is generated and how thehandler is invoked.<P>The <CODE>sigaction</CODE> function is declared in <TT>`signal.h'</TT>.<A NAME="IDX1548"></A><P><A NAME="IDX1549"></A><U>Data Type:</U> <B>struct sigaction</B><P>Structures of type <CODE>struct sigaction</CODE> are used in the<CODE>sigaction</CODE> function to specify all the information about how tohandle a particular signal. This structure contains at least thefollowing members:<P><DL COMPACT><DT><CODE>sighandler_t sa_handler</CODE><DD>This is used in the same way as the <VAR>action</VAR> argument to the<CODE>signal</CODE> function. The value can be <CODE>SIG_DFL</CODE>,<CODE>SIG_IGN</CODE>, or a function pointer. See section <A HREF="library_21.html#SEC345" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC345">Basic Signal Handling</A>.<P><DT><CODE>sigset_t sa_mask</CODE><DD>This specifies a set of signals to be blocked while the handler runs.Blocking is explained in section <A HREF="library_21.html#SEC373" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC373">Blocking Signals for a Handler</A>. Note that thesignal that was delivered is automatically blocked by default before itshandler is started; this is true regardless of the value in<CODE>sa_mask</CODE>. If you want that signal not to be blocked within itshandler, you must write code in the handler to unblock it.<P><DT><CODE>int sa_flags</CODE><DD>This specifies various flags which can affect the behavior of the signal. These are described in more detail in section <A HREF="library_21.html#SEC349" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC349">Flags for <CODE>sigaction</CODE></A>.</DL><P><A NAME="IDX1550"></A><U>Function:</U> int <B>sigaction</B> <I>(int <VAR>signum</VAR>, const struct sigaction *<VAR>action</VAR>, struct sigaction *<VAR>old_action</VAR>)</I><P>The <VAR>action</VAR> argument is used to set up a new action for the signal<VAR>signum</VAR>, while the <VAR>old_action</VAR> argument is used to returninformation about the action previously associated with this symbol.(In other words, <VAR>old_action</VAR> has the same purpose as the<CODE>signal</CODE> function's return value--you can check to see what theold action in effect for the signal was, and restore it later if youwant.)<P>Either <VAR>action</VAR> or <VAR>old_action</VAR> can be a null pointer. If<VAR>old_action</VAR> is a null pointer, this simply suppresses the returnof information about the old action. If <VAR>action</VAR> is a null pointer,the action associated with the signal <VAR>signum</VAR> is unchanged; thisallows you to inquire about how a signal is being handled without changingthat handling.<P>The return value from <CODE>sigaction</CODE> is zero if it succeeds, and<CODE>-1</CODE> on failure. The following <CODE>errno</CODE> error conditions aredefined for this function:<P><DL COMPACT><DT><CODE>EINVAL</CODE><DD>The <VAR>signum</VAR> argument is not valid, or you are trying totrap or ignore <CODE>SIGKILL</CODE> or <CODE>SIGSTOP</CODE>.</DL><P><H3><A NAME="SEC347" HREF="library_toc.html#SEC347" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC347">Interaction of <CODE>signal</CODE> and <CODE>sigaction</CODE></A></H3><P>It's possible to use both the <CODE>signal</CODE> and <CODE>sigaction</CODE>functions within a single program, but you have to be careful becausethey can interact in slightly strange ways.<P>The <CODE>sigaction</CODE> function specifies more information than the<CODE>signal</CODE> function, so the return value from <CODE>signal</CODE> cannotexpress the full range of <CODE>sigaction</CODE> possibilities. Therefore, ifyou use <CODE>signal</CODE> to save and later reestablish an action, it maynot be able to reestablish properly a handler that was established with<CODE>sigaction</CODE>.<P>To avoid having problems as a result, always use <CODE>sigaction</CODE> tosave and restore a handler if your program uses <CODE>sigaction</CODE> at all.Since <CODE>sigaction</CODE> is more general, it can properly save andreestablish any action, regardless of whether it was establishedoriginally with <CODE>signal</CODE> or <CODE>sigaction</CODE>.<P>If you establish an action with <CODE>signal</CODE> and then examine it with<CODE>sigaction</CODE>, the handler address that you get may not be the sameas what you specified with <CODE>signal</CODE>. It may not even be suitablefor use as an action argument with <CODE>signal</CODE>. But you can rely onusing it as an argument to <CODE>sigaction</CODE>.<P>So, you're better off using one or the other of the mechanismsconsistently within a single program. <P><STRONG>Portability Note:</STRONG> The basic <CODE>signal</CODE> function is a featureof ANSI C, while <CODE>sigaction</CODE> is part of the POSIX.1 standard. Ifyou are concerned about portability to non-POSIX systems, then youshould use the <CODE>signal</CODE> function instead.<P><H3><A NAME="SEC348" HREF="library_toc.html#SEC348" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC348"><CODE>sigaction</CODE> Function Example</A></H3><P>In section <A HREF="library_21.html#SEC345" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC345">Basic Signal Handling</A>, we gave an example of establishing asimple handler for termination signals using <CODE>signal</CODE>. Here is anequivalent example using <CODE>sigaction</CODE>:<P><PRE>#include <signal.h>voidtermination_handler (int signum){ struct temp_file *p; for (p = temp_file_list; p; p = p->next) unlink (p->name);}intmain (void){ ... struct sigaction new_action, old_action; /* Set up the structure to specify the new action. */ new_action.sa_handler = termination_handler; sigemptyset (&new_action.sa_mask); new_action.sa_flags = 0; sigaction (SIGINT, NULL, &old_action); if (old_action.sa_handler != SIG_IGN) sigaction (SIGINT, &new_action, NULL); sigaction (SIGHUP, NULL, &old_action); if (old_action.sa_handler != SIG_IGN) sigaction (SIGHUP, &new_action, NULL); sigaction (SIGTERM, NULL, &old_action); if (old_action.sa_handler != SIG_IGN) sigaction (SIGTERM, &new_action, NULL); ...}</PRE><P>The program just loads the <CODE>new_action</CODE> structure with the desiredparameters and passes it in the <CODE>sigaction</CODE> call. The usage of<CODE>sigemptyset</CODE> is described later; see section <A HREF="library_21.html#SEC368" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC368">Blocking Signals</A>.<P>As in the example using <CODE>signal</CODE>, we avoid handling signalspreviously set to be ignored. Here we can avoid altering the signalhandler even momentarily, by using the feature of <CODE>sigaction</CODE> thatlets us examine the current action without specifying a new one.<P>Here is another example. It retrieves information about the currentaction for <CODE>SIGINT</CODE> without changing that action.<P><PRE>struct sigaction query_action;if (sigaction (SIGINT, NULL, &query_action) < 0) /* <CODE>sigaction</CODE> returns -1 in case of error. */ else if (query_action.sa_handler == SIG_DFL) /* <CODE>SIGINT</CODE> is handled in the default, fatal manner. */else if (query_action.sa_handler == SIG_IGN) /* <CODE>SIGINT</CODE> is ignored. */else /* A programmer-defined signal handler is in effect. */</PRE><P><A NAME="IDX1551"></A><A NAME="IDX1552"></A><A NAME="IDX1553"></A><H3><A NAME="SEC349" HREF="library_toc.html#SEC349" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC349">Flags for <CODE>sigaction</CODE></A></H3><P>The <CODE>sa_flags</CODE> member of the <CODE>sigaction</CODE> structure is acatch-all for special features. Most of the time, <CODE>SA_RESTART</CODE> isa good value to use for this field.<P>The value of <CODE>sa_flags</CODE> is interpreted as a bit mask. Thus, youshould choose the flags you want to set, OR those flags together,and store the result in the <CODE>sa_flags</CODE> member of your<CODE>sigaction</CODE> structure.<P>Each signal number has its own set of flags. Each call to<CODE>sigaction</CODE> affects one particular signal number, and the flagsthat you specify apply only to that particular signal.<P>In the GNU C library, establishing a handler with <CODE>signal</CODE> sets allthe flags to zero except for <CODE>SA_RESTART</CODE>, whose value depends onthe settings you have made with <CODE>siginterrupt</CODE>. See section <A HREF="library_21.html#SEC362" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC362">Primitives Interrupted by Signals</A>, to see what this is about.<A NAME="IDX1554"></A><P>These macros are defined in the header file <TT>`signal.h'</TT>.<P><A NAME="IDX1555"></A><U>Macro:</U> int <B>SA_NOCLDSTOP</B><P>This flag is meaningful only for the <CODE>SIGCHLD</CODE> signal. When theflag is set, the system delivers the signal for a terminated childprocess but not for one that is stopped. By default, <CODE>SIGCHLD</CODE> isdelivered for both terminated children and stopped children.<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -