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

📄 process.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@itemPending alarms.  @xref{Setting an Alarm}.@itemCurrent working directory and root directory.  @xref{WorkingDirectory}.  In the GNU system, the root directory is not copied whenexecuting a setuid program; instead the system default root directoryis used for the new program.@itemFile mode creation mask.  @xref{Setting Permissions}.@itemProcess signal mask; see @ref{Process Signal Mask}.@itemPending signals; see @ref{Blocking Signals}.@itemElapsed processor time associated with the process; see @ref{Processor Time}.@end itemizeIf the set-user-ID and set-group-ID mode bits of the process image fileare set, this affects the effective user ID and effective group ID(respectively) of the process.  These concepts are discussed in detailin @ref{Process Persona}.Signals that are set to be ignored in the existing process image arealso set to be ignored in the new process image.  All other signals areset to the default action in the new process image.  For moreinformation about signals, see @ref{Signal Handling}.File descriptors open in the existing process image remain open in thenew process image, unless they have the @code{FD_CLOEXEC}(close-on-exec) flag set.  The files that remain open inherit allattributes of the open file description from the existing process image,including file locks.  File descriptors are discussed in @ref{Low-Level I/O}.Streams, by contrast, cannot survive through @code{exec} functions,because they are located in the memory of the process itself.  The newprocess image has no streams except those it creates afresh.  Each ofthe streams in the pre-@code{exec} process image has a descriptor insideit, and these descriptors do survive through @code{exec} (provided thatthey do not have @code{FD_CLOEXEC} set).  The new process image canreconnect these to new streams using @code{fdopen} (@pxref{Descriptorsand Streams}).@node Process Completion@section Process Completion@cindex process completion@cindex waiting for completion of child process@cindex testing exit status of child processThe functions described in this section are used to wait for a childprocess to terminate or stop, and determine its status.  These functionsare declared in the header file @file{sys/wait.h}.@pindex sys/wait.h@comment sys/wait.h@comment POSIX.1@deftypefun pid_t waitpid (pid_t @var{pid}, int *@var{status-ptr}, int @var{options})The @code{waitpid} function is used to request status information from achild process whose process ID is @var{pid}.  Normally, the callingprocess is suspended until the child process makes status informationavailable by terminating.Other values for the @var{pid} argument have special interpretations.  Avalue of @code{-1} or @code{WAIT_ANY} requests status information forany child process; a value of @code{0} or @code{WAIT_MYPGRP} requestsinformation for any child process in the same process group as thecalling process; and any other negative value @minus{} @var{pgid}requests information for any child process whose process group ID is@var{pgid}.If status information for a child process is available immediately, thisfunction returns immediately without waiting.  If more than one eligiblechild process has status information available, one of them is chosenrandomly, and its status is returned immediately.  To get the statusfrom the other eligible child processes, you need to call @code{waitpid}again.The @var{options} argument is a bit mask.  Its value should be thebitwise OR (that is, the @samp{|} operator) of zero or more of the@code{WNOHANG} and @code{WUNTRACED} flags.  You can use the@code{WNOHANG} flag to indicate that the parent process shouldn't wait;and the @code{WUNTRACED} flag to request status information from stoppedprocesses as well as processes that have terminated.The status information from the child process is stored in the objectthat @var{status-ptr} points to, unless @var{status-ptr} is a null pointer.The return value is normally the process ID of the child process whosestatus is reported.  If the @code{WNOHANG} option was specified and nochild process is waiting to be noticed, the value is zero.  A value of@code{-1} is returned in case of error.  The following @code{errno}error conditions are defined for this function:@table @code@item EINTRThe function was interrupted by delivery of a signal to the callingprocess.  @xref{Interrupted Primitives}.@item ECHILDThere are no child processes to wait for, or the specified @var{pid}is not a child of the calling process.@item EINVALAn invalid value was provided for the @var{options} argument.@end table@end deftypefunThese symbolic constants are defined as values for the @var{pid} argumentto the @code{waitpid} function.@comment Extra blank lines make it look better.@table @code@item WAIT_ANYThis constant macro (whose value is @code{-1}) specifies that@code{waitpid} should return status information about any child process.@item WAIT_MYPGRPThis constant (with value @code{0}) specifies that @code{waitpid} shouldreturn status information about any child process in the same processgroup as the calling process.@end tableThese symbolic constants are defined as flags for the @var{options}argument to the @code{waitpid} function.  You can bitwise-OR the flagstogether to obtain a value to use as the argument.@table @code@item WNOHANGThis flag specifies that @code{waitpid} should return immediatelyinstead of waiting, if there is no child process ready to be noticed.@item WUNTRACEDThis flag specifies that @code{waitpid} should report the status of anychild processes that have been stopped as well as those that haveterminated.@end table@comment sys/wait.h@comment POSIX.1@deftypefun pid_t wait (int *@var{status-ptr})This is a simplified version of @code{waitpid}, and is used to waituntil any one child process terminates.  The call:@smallexamplewait (&status)@end smallexample@noindentis exactly equivalent to:@smallexamplewaitpid (-1, &status, 0)@end smallexample@end deftypefun@comment sys/wait.h@comment BSD@deftypefun pid_t wait4 (pid_t @var{pid}, int *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})If @var{usage} is a null pointer, @code{wait4} is equivalent to@code{waitpid (@var{pid}, @var{status-ptr}, @var{options})}.If @var{usage} is not null, @code{wait4} stores usage figures for thechild process in @code{*@var{rusage}} (but only if the child hasterminated, not if it has stopped).  @xref{Resource Usage}.This function is a BSD extension.@end deftypefunHere's an example of how to use @code{waitpid} to get the status fromall child processes that have terminated, without ever waiting.  Thisfunction is designed to be a handler for @code{SIGCHLD}, the signal thatindicates that at least one child process has terminated.@smallexample@groupvoidsigchld_handler (int signum)@{  int pid;  int status;  while (1)    @{      pid = waitpid (WAIT_ANY, &status, WNOHANG);      if (pid < 0)        @{          perror ("waitpid");          break;        @}      if (pid == 0)        break;      notice_termination (pid, status);    @}@}@end group@end smallexample@node Process Completion Status@section Process Completion StatusIf the exit status value (@pxref{Program Termination}) of the childprocess is zero, then the status value reported by @code{waitpid} or@code{wait} is also zero.  You can test for other kinds of informationencoded in the returned status value using the following macros.These macros are defined in the header file @file{sys/wait.h}.@pindex sys/wait.h@comment sys/wait.h@comment POSIX.1@deftypefn Macro int WIFEXITED (int @var{status})This macro returns a nonzero value if the child process terminatednormally with @code{exit} or @code{_exit}.@end deftypefn@comment sys/wait.h@comment POSIX.1@deftypefn Macro int WEXITSTATUS (int @var{status})If @code{WIFEXITED} is true of @var{status}, this macro returns thelow-order 8 bits of the exit status value from the child process.@xref{Exit Status}.@end deftypefn@comment sys/wait.h@comment POSIX.1@deftypefn Macro int WIFSIGNALED (int @var{status})This macro returns a nonzero value if the child process terminatedbecause it received a signal that was not handled.@xref{Signal Handling}.@end deftypefn@comment sys/wait.h@comment POSIX.1@deftypefn Macro int WTERMSIG (int @var{status})If @code{WIFSIGNALED} is true of @var{status}, this macro returns thesignal number of the signal that terminated the child process.@end deftypefn@comment sys/wait.h@comment BSD@deftypefn Macro int WCOREDUMP (int @var{status})This macro returns a nonzero value if the child process terminatedand produced a core dump.@end deftypefn@comment sys/wait.h@comment POSIX.1@deftypefn Macro int WIFSTOPPED (int @var{status})This macro returns a nonzero value if the child process is stopped.@end deftypefn@comment sys/wait.h@comment POSIX.1@deftypefn Macro int WSTOPSIG (int @var{status})If @code{WIFSTOPPED} is true of @var{status}, this macro returns thesignal number of the signal that caused the child process to stop.@end deftypefn@node BSD Wait Functions@section BSD Process Wait FunctionsThe GNU library also provides these related facilities for compatibilitywith BSD Unix.  BSD uses the @code{union wait} data type to representstatus values rather than an @code{int}.  The two representations areactually interchangeable; they describe the same bit patterns.  The GNUC Library defines macros such as @code{WEXITSTATUS} so that they willwork on either kind of object, and the @code{wait} function is definedto accept either type of pointer as its @var{status-ptr} argument.These functions are declared in @file{sys/wait.h}.@pindex sys/wait.h@comment sys/wait.h@comment BSD@deftp {Data Type} {union wait}This data type represents program termination status values.  It hasthe following members:@table @code@item int w_termsigThe value of this member is the same as the result of the@code{WTERMSIG} macro.@item int w_coredumpThe value of this member is the same as the result of the@code{WCOREDUMP} macro.@item int w_retcodeThe value of this member is the same as the result of the@code{WEXITSTATUS} macro.@item int w_stopsigThe value of this member is the same as the result of the@code{WSTOPSIG} macro.@end tableInstead of accessing these members directly, you should use theequivalent macros.@end deftpThe @code{wait3} function is the predecessor to @code{wait4}, which ismore flexible.  @code{wait3} is now obsolete.@comment sys/wait.h@comment BSD@deftypefun pid_t wait3 (union wait *@var{status-ptr}, int @var{options}, struct rusage *@var{usage})If @var{usage} is a null pointer, @code{wait3} is equivalent to@code{waitpid (-1, @var{status-ptr}, @var{options})}.If @var{usage} is not null, @code{wait3} stores usage figures for thechild process in @code{*@var{rusage}} (but only if the child hasterminated, not if it has stopped).  @xref{Resource Usage}.@end deftypefun@node Process Creation Example@section Process Creation ExampleHere is an example program showing how you might write a functionsimilar to the built-in @code{system}.  It executes its @var{command}argument using the equivalent of @samp{sh -c @var{command}}.@smallexample#include <stddef.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>/* @r{Execute the command using this shell program.}  */#define SHELL "/bin/sh"@groupint my_system (const char *command)@{  int status;  pid_t pid;@end group  pid = fork ();  if (pid == 0)    @{      /* @r{This is the child process.  Execute the shell command.} */      execl (SHELL, SHELL, "-c", command, NULL);      _exit (EXIT_FAILURE);    @}  else if (pid < 0)    /* @r{The fork failed.  Report failure.}  */    status = -1;  else    /* @r{This is the parent process.  Wait for the child to complete.}  */    if (waitpid (pid, &status, 0) != pid)      status = -1;  return status;@}@end smallexample@comment Yes, this example has been tested.There are a couple of things you should pay attention to in thisexample.Remember that the first @code{argv} argument supplied to the programrepresents the name of the program being executed.  That is why, in thecall to @code{execl}, @code{SHELL} is supplied once to name the programto execute and a second time to supply a value for @code{argv[0]}.  The @code{execl} call in the child process doesn't return if it issuccessful.  If it fails, you must do something to make the childprocess terminate.  Just returning a bad status code with @code{return}would leave two processes running the original program.  Instead, theright behavior is for the child process to report failure to its parentprocess.Call @code{_exit} to accomplish this.  The reason for using @code{_exit}instead of @code{exit} is to avoid flushing fully buffered streams suchas @code{stdout}.  The buffers of these streams probably contain datathat was copied from the parent process by the @code{fork}, data thatwill be output eventually by the parent process.  Calling @code{exit} inthe child would output the data twice.  @xref{Termination Internals}.

⌨️ 快捷键说明

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