⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 signal.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@code{SIGCONT} always makes the process continue regardless.Most programs have no reason to handle @code{SIGCONT}; they simplyresume execution without realizing they were ever stopped.  You can usea handler for @code{SIGCONT} to make a program do something special whenit is stopped and continued---for example, to reprint a prompt when itis suspended while waiting for input.@end deftypevr@comment signal.h@comment POSIX.1@deftypevr Macro int SIGSTOPThe @code{SIGSTOP} signal stops the process.  It cannot be handled,ignored, or blocked.@end deftypevr@cindex stop signal@comment signal.h@comment POSIX.1@deftypevr Macro int SIGTSTPThe @code{SIGTSTP} signal is an interactive stop signal.  Unlike@code{SIGSTOP}, this signal can be handled and ignored.  Your program should handle this signal if you have a special need toleave files or system tables in a secure state when a process isstopped.  For example, programs that turn off echoing should handle@code{SIGTSTP} so they can turn echoing back on before stopping.This signal is generated when the user types the SUSP character(normally @kbd{C-z}).  For more information about terminal driversupport, see @ref{Special Characters}.@end deftypevr@cindex interactive stop signal@comment signal.h@comment POSIX.1@deftypevr Macro int SIGTTINA process cannot read from the the user's terminal while it is running as a background job.  When any process in a background job tries toread from the terminal, all of the processes in the job are sent a@code{SIGTTIN} signal.  The default action for this signal is tostop the process.  For more information about how this interacts withthe terminal driver, see @ref{Access to the Terminal}.@end deftypevr@cindex terminal input signal@comment signal.h@comment POSIX.1@deftypevr Macro int SIGTTOUThis is similar to @code{SIGTTIN}, but is generated when a process in abackground job attempts to write to the terminal or set its modes.Again, the default action is to stop the process.  @code{SIGTTOU} isonly generated for an attempt to write to the terminal if the@code{TOSTOP} output mode is set; @pxref{Output Modes}.@end deftypevr@cindex terminal output signalWhile a process is stopped, no more signals can be delivered to it untilit is continued, except @code{SIGKILL} signals and (obviously)@code{SIGCONT} signals.  The signals are marked as pending, but notdelivered until the process is continued.  The @code{SIGKILL} signalalways causes termination of the process and can't be blocked, handledor ignored.  You can ignore @code{SIGCONT}, but it always causes theprocess to be continued anyway if it is stopped.  Sending a@code{SIGCONT} signal to a process causes any pending stop signals forthat process to be discarded.  Likewise, any pending @code{SIGCONT}signals for a process are discarded when it receives a stop signal.When a process in an orphaned process group (@pxref{Orphaned ProcessGroups}) receives a @code{SIGTSTP}, @code{SIGTTIN}, or @code{SIGTTOU}signal and does not handle it, the process does not stop.  Stopping theprocess would probably not be very useful, since there is no shellprogram that will notice it stop and allow the user to continue it.What happens instead depends on the operating system you are using.Some systems may do nothing; others may deliver another signal instead,such as @code{SIGKILL} or @code{SIGHUP}.  In the GNU system, the processdies with @code{SIGKILL}; this avoids the problem of many stopped,orphaned processes lying around the system.@ignoreOn the GNU system, it is possible to reattach to the orphaned processgroup and continue it, so stop signals do stop the process as usual ona GNU system unless you have requested POSIX compatibility ``till ithurts.''@end ignore@node Operation Error Signals@subsection Operation Error SignalsThese signals are used to report various errors generated by anoperation done by the program.  They do not necessarily indicate aprogramming error in the program, but an error that prevents anoperating system call from completing.  The default action for all ofthem is to cause the process to terminate.@comment signal.h@comment POSIX.1@deftypevr Macro int SIGPIPE@cindex pipe signal@cindex broken pipe signalBroken pipe.  If you use pipes or FIFOs, you have to design yourapplication so that one process opens the pipe for reading beforeanother starts writing.  If the reading process never starts, orterminates unexpectedly, writing to the pipe or FIFO raises a@code{SIGPIPE} signal.  If @code{SIGPIPE} is blocked, handled orignored, the offending call fails with @code{EPIPE} instead.Pipes and FIFO special files are discussed in more detail in @ref{Pipesand FIFOs}.Another cause of @code{SIGPIPE} is when you try to output to a socketthat isn't connected.  @xref{Sending Data}.@end deftypevr@comment signal.h@comment GNU@deftypevr Macro int SIGLOST@cindex lost resource signalResource lost.  This signal is generated when you have an advisory lockon an NFS file, and the NFS server reboots and forgets about your lock.In the GNU system, @code{SIGLOST} is generated when any server programdies unexpectedly.  It is usually fine to ignore the signal; whatevercall was made to the server that died just returns an error.@end deftypevr@comment signal.h@comment BSD@deftypevr Macro int SIGXCPUCPU time limit exceeded.  This signal is generated when the processexceeds its soft resource limit on CPU time.  @xref{Limits on Resources}.@end deftypevr@comment signal.h@comment BSD@deftypevr Macro int SIGXFSZFile size limit exceeded.  This signal is generated when the processattempts to extend a file so it exceeds the process's soft resourcelimit on file size.  @xref{Limits on Resources}.@end deftypevr@node Miscellaneous Signals@subsection Miscellaneous SignalsThese signals are used for various other purposes.  In general, theywill not affect your program unless it explicitly uses them for something.@comment signal.h@comment POSIX.1@deftypevr Macro int SIGUSR1@end deftypevr@comment signal.h@comment POSIX.1@deftypevr Macro int SIGUSR2@cindex user signalsThe @code{SIGUSR1} and @code{SIGUSR2} signals are set aside for you touse any way you want.  They're useful for simple interprocesscommunication, if you write a signal handler for them in the programthat receives the signal.There is an example showing the use of @code{SIGUSR1} and @code{SIGUSR2}in @ref{Signaling Another Process}.The default action is to terminate the process.@end deftypevr@comment signal.h@comment BSD@deftypevr Macro int SIGWINCHWindow size change.  This is generated on some systems (including GNU)when the terminal driver's record of the number of rows and columns onthe screen is changed.  The default action is to ignore it.If a program does full-screen display, it should handle @code{SIGWINCH}.When the signal arrives, it should fetch the new screen size andreformat its display accordingly.@end deftypevr@comment signal.h@comment BSD@deftypevr Macro int SIGINFOInformation request.  In 4.4 BSD and the GNU system, this signal is sentto all the processes in the foreground process group of the controllingterminal when the user types the STATUS character in canonical mode;@pxref{Signal Characters}.If the process is the leader of the process group, the default action isto print some status information about the system and what the processis doing.  Otherwise the default is to do nothing.@end deftypevr@node Signal Messages@subsection Signal Messages@cindex signal messagesWe mentioned above that the shell prints a message describing the signalthat terminated a child process.  The clean way to print a messagedescribing a signal is to use the functions @code{strsignal} and@code{psignal}.  These functions use a signal number to specify whichkind of signal to describe.  The signal number may come from thetermination status of a child process (@pxref{Process Completion}) or itmay come from a signal handler in the same process.@comment string.h@comment GNU@deftypefun {char *} strsignal (int @var{signum})This function returns a pointer to a statically-allocated stringcontaining a message describing the signal @var{signum}.  Youshould not modify the contents of this string; and, since it can berewritten on subsequent calls, you should save a copy of it if you needto reference it later.@pindex string.hThis function is a GNU extension, declared in the header file@file{string.h}.@end deftypefun@comment signal.h@comment BSD@deftypefun void psignal (int @var{signum}, const char *@var{message})This function prints a message describing the signal @var{signum} to thestandard error output stream @code{stderr}; see @ref{Standard Streams}.If you call @code{psignal} with a @var{message} that is either a nullpointer or an empty string, @code{psignal} just prints the message corresponding to @var{signum}, adding a trailing newline.If you supply a non-null @var{message} argument, then @code{psignal}prefixes its output with this string.  It adds a colon and a space character to separate the @var{message} from the string correspondingto @var{signum}.@pindex stdio.hThis function is a BSD feature, declared in the header file @file{signal.h}.@end deftypefun@vindex sys_siglistThere is also an array @code{sys_siglist} which contains the messagesfor the various signal codes.  This array exists on BSD systems, unlike@code{strsignal}.@node Signal Actions@section Specifying Signal Actions@cindex signal actions@cindex establishing a handlerThe simplest way to change the action for a signal is to use the@code{signal} function.  You can specify a built-in action (such as toignore the signal), or you can @dfn{establish a handler}.The GNU library also implements the more versatile @code{sigaction}facility.  This section describes both facilities and gives suggestionson which to use when.@menu* Basic Signal Handling::       The simple @code{signal} function.* Advanced Signal Handling::    The more powerful @code{sigaction} function.* Signal and Sigaction::        How those two functions interact.* Sigaction Function Example::  An example of using the sigaction function.* Flags for Sigaction::         Specifying options for signal handling.* Initial Signal Actions::      How programs inherit signal actions.@end menu@node Basic Signal Handling@subsection Basic Signal Handling@cindex @code{signal} functionThe @code{signal} function provides a simple interface for establishingan action for a particular signal.  The function and associated macrosare declared in the header file @file{signal.h}.@pindex signal.h@comment signal.h@comment GNU@deftp {Data Type} sighandler_tThis is the type of signal handler functions.  Signal handlers take oneinteger argument specifying the signal number, and have return type@code{void}.  So, you should define handler functions like this:@smallexamplevoid @var{handler} (int @code{signum}) @{ @dots{} @}@end smallexampleThe name @code{sighandler_t} for this data type is a GNU extension.@end deftp@comment signal.h@comment ANSI@deftypefun sighandler_t signal (int @var{signum}, sighandler_t @var{action})The @code{signal} function establishes @var{action} as the action forthe signal @var{signum}.The first argument, @var{signum}, identifies the signal whose behavioryou want to control, and should be a signal number.  The proper way tospecify a signal number is with one of the symbolic signal namesdescribed in @ref{Standard Signals}---don't use an explicit number, becausethe numerical code for a given kind of signal may vary from operatingsystem to operating system.The second argument, @var{action}, specifies the action to use for thesignal @var{signum}.  This can be one of the following:@table @code@item SIG_DFL@vindex SIG_DFL@cindex default action for a signal@code{SIG_DFL} specifies the default action for the particular signal.The default actions for various kinds of signals are stated in@ref{Standard Signals}.@item SIG_IGN@vindex SIG_IGN@cindex ignore action for a signal@code{SIG_IGN} specifies that the signal should be ignored.Your program generally should not ignore signals that represent seriousevents or that are normally used to request termination.  You cannotignore the @code{SIGKILL} or @code{SIGSTOP} signals at all.  You canignore program error signals like @code{SIGSEGV}, but ignoring the errorwon't enable the program to continue executing meaningfully.  Ignoringuser requests such as @code{SIGINT}, @code{SIGQUIT}, and @code{SIGTSTP}is unfriendly.When you do not wish signals to be delivered during a certain part ofthe program, the thing to do is to block them, not ignore them.@xref{Blocking Signals}.@item @var{handler}Supply the address of a handler function in your program, to specifyrunning this handler as the way to deliver the signal.For more information about defining signal handler functions,see @ref{Defining Handlers}.@end tableIf you set the action for a signal to @code{SIG_IGN}, or if you set itto @code{SIG_DFL} 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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -