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

📄 library_23.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!-- This HTML file has been created by texi2html 1.27     from library.texinfo on 3 March 1994 --><TITLE>The GNU C Library - Child Processes</TITLE><P>Go to the <A HREF="library_22.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_22.html">previous</A>, <A HREF="library_24.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html">next</A> section.<P><H1><A NAME="SEC401" HREF="library_toc.html#SEC401" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC401">Child Processes</A></H1><A NAME="IDX1680"></A><P><DFN>Processes</DFN> are the primitive units for allocation of systemresources.  Each process has its own address space and (usually) onethread of control.  A process executes a program; you can have multipleprocesses executing the same program, but each process has its own copyof the program within its own address space and executes itindependently of the other copies.<A NAME="IDX1681"></A><A NAME="IDX1682"></A><P>Processes are organized hierarchically.  Each process has a <DFN>parentprocess</DFN> which explicitly arranged to create it.  The processes createdby a given parent are called its <DFN>child processes</DFN>.  A childinherits many of its attributes from the parent process.<P>This chapter describes how a program can create, terminate, and controlchild processes.  Actually, there are three distinct operationsinvolved: creating a new child process, causing the new process toexecute a program, and coordinating the completion of the child processwith the original program.<P>The <CODE>system</CODE> function provides a simple, portable mechanism forrunning another program; it does all three steps automatically.  If youneed more control over the details of how this is done, you can use theprimitive functions to do each step individually instead.<P><A NAME="IDX1683"></A><H2><A NAME="SEC402" HREF="library_toc.html#SEC402" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC402">Running a Command</A></H2><P>The easy way to run another program is to use the <CODE>system</CODE>function.  This function does all the work of running a subprogram, butit doesn't give you much control over the details: you have to waituntil the subprogram terminates before you can do anything else.<P><A NAME="IDX1684"></A><A NAME="IDX1685"></A><U>Function:</U> int <B>system</B> <I>(const char *<VAR>command</VAR>)</I><P>This function executes <VAR>command</VAR> as a shell command.  In the GNU Clibrary, it always uses the default shell <CODE>sh</CODE> to run the command.In particular, it searches the directories in <CODE>PATH</CODE> to findprograms to execute.  The return value is <CODE>-1</CODE> if it wasn'tpossible to create the shell process, and otherwise is the status of theshell process.  See section <A HREF="library_23.html#SEC407" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC407">Process Completion</A>, for details on how thisstatus code can be interpreted.<A NAME="IDX1686"></A><P>The <CODE>system</CODE> function is declared in the header file<TT>`stdlib.h'</TT>.<P><STRONG>Portability Note:</STRONG> Some C implementations may not have anynotion of a command processor that can execute other programs.  You candetermine whether a command processor exists by executing<CODE>system (NULL)</CODE>; if the return value is nonzero, a commandprocessor is available.<P>The <CODE>popen</CODE> and <CODE>pclose</CODE> functions (see section <A HREF="library_14.html#SEC213" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_14.html#SEC213">Pipe to a Subprocess</A>) are closely related to the <CODE>system</CODE> function.  Theyallow the parent process to communicate with the standard input andoutput channels of the command being executed.<P><H2><A NAME="SEC403" HREF="library_toc.html#SEC403" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC403">Process Creation Concepts</A></H2><P>This section gives an overview of processes and of the steps involved increating a process and making it run another program.<A NAME="IDX1687"></A><A NAME="IDX1688"></A><P>Each process is named by a <DFN>process ID</DFN> number.  A unique process IDis allocated to each process when it is created.  The <DFN>lifetime</DFN> ofa process ends when its termination is reported to its parent process;at that time, all of the process resources, including its process ID,are freed.<A NAME="IDX1689"></A><A NAME="IDX1690"></A><A NAME="IDX1691"></A><A NAME="IDX1692"></A><P>Processes are created with the <CODE>fork</CODE> system call (so the operationof creating a new process is sometimes called <DFN>forking</DFN> a process).The <DFN>child process</DFN> created by <CODE>fork</CODE> is an exact clone of theoriginal <DFN>parent process</DFN>, except that it has its own process ID.<P>After forking a child process, both the parent and child processescontinue to execute normally.  If you want your program to wait for achild process to finish executing before continuing, you must do thisexplicitly after the fork operation, by calling <CODE>wait</CODE> or<CODE>waitpid</CODE> (see section <A HREF="library_23.html#SEC407" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC407">Process Completion</A>).  These functions give youlimited information about why the child terminated--for example, itsexit status code.<P>A newly forked child process continues to execute the same program asits parent process, at the point where the <CODE>fork</CODE> call returns.You can use the return value from <CODE>fork</CODE> to tell whether the programis running in the parent process or the child.<A NAME="IDX1693"></A><P>Having several processes run the same program is only occasionallyuseful.  But the child can execute another program using one of the<CODE>exec</CODE> functions; see section <A HREF="library_23.html#SEC406" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC406">Executing a File</A>.  The program that theprocess is executing is called its <DFN>process image</DFN>.  Startingexecution of a new program causes the process to forget all about itsprevious process image; when the new program exits, the process exitstoo, instead of returning to the previous process image.<P><H2><A NAME="SEC404" HREF="library_toc.html#SEC404" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC404">Process Identification</A></H2><P>The <CODE>pid_t</CODE> data type represents process IDs.  You can get theprocess ID of a process by calling <CODE>getpid</CODE>.  The function<CODE>getppid</CODE> returns the process ID of the parent of the currentprocess (this is also known as the <DFN>parent process ID</DFN>).  Yourprogram should include the header files <TT>`unistd.h'</TT> and<TT>`sys/types.h'</TT> to use these functions.<A NAME="IDX1695"></A><A NAME="IDX1694"></A><P><A NAME="IDX1696"></A><U>Data Type:</U> <B>pid_t</B><P>The <CODE>pid_t</CODE> data type is a signed integer type which is capableof representing a process ID.  In the GNU library, this is an <CODE>int</CODE>.<P><A NAME="IDX1697"></A><U>Function:</U> pid_t <B>getpid</B> <I>(void)</I><P>The <CODE>getpid</CODE> function returns the process ID of the current process.<P><A NAME="IDX1698"></A><U>Function:</U> pid_t <B>getppid</B> <I>(void)</I><P>The <CODE>getppid</CODE> function returns the process ID of the parent of thecurrent process.<P><H2><A NAME="SEC405" HREF="library_toc.html#SEC405" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC405">Creating a Process</A></H2><P>The <CODE>fork</CODE> function is the primitive for creating a process.It is declared in the header file <TT>`unistd.h'</TT>.<A NAME="IDX1699"></A><P><A NAME="IDX1700"></A><U>Function:</U> pid_t <B>fork</B> <I>(void)</I><P>The <CODE>fork</CODE> function creates a new process.<P>If the operation is successful, there are then both parent and childprocesses and both see <CODE>fork</CODE> return, but with different values: itreturns a value of <CODE>0</CODE> in the child process and returns the child'sprocess ID in the parent process.<P>If process creation failed, <CODE>fork</CODE> returns a value of <CODE>-1</CODE> inthe parent process.  The following <CODE>errno</CODE> error conditions aredefined for <CODE>fork</CODE>:<P><DL COMPACT><DT><CODE>EAGAIN</CODE><DD>There aren't enough system resources to create another process, or theuser already has too many processes running.<P><DT><CODE>ENOMEM</CODE><DD>The process requires more space than the system can supply.</DL><P>The specific attributes of the child process that differ from theparent process are:<P><UL><LI>The child process has its own unique process ID.<P><LI>The parent process ID of the child process is the process ID of itsparent process.<P><LI>The child process gets its own copies of the parent process's open filedescriptors.  Subsequently changing attributes of the file descriptorsin the parent process won't affect the file descriptors in the child,and vice versa.  See section <A HREF="library_12.html#SEC181" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC181">Control Operations on Files</A>.<P><LI>The elapsed processor times for the child process are set to zero;see section <A HREF="library_19.html#SEC310" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html#SEC310">Processor Time</A>.<P><LI>The child doesn't inherit file locks set by the parent process.See section <A HREF="library_12.html#SEC181" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC181">Control Operations on Files</A>.<P><LI>The child doesn't inherit alarms set by the parent process.See section <A HREF="library_19.html#SEC321" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html#SEC321">Setting an Alarm</A>.<P><LI>The set of pending signals (see section <A HREF="library_21.html#SEC334" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC334">How Signals Are Delivered</A>) for the childprocess is cleared.  (The child process inherits its mask of blockedsignals and signal actions from the parent process.)</UL><P><A NAME="IDX1701"></A><U>Function:</U> pid_t <B>vfork</B> <I>(void)</I><P>The <CODE>vfork</CODE> function is similar to <CODE>fork</CODE> but more efficient;however, there are restrictions you must follow to use it safely.<P>While <CODE>fork</CODE> makes a complete copy of the calling process's addressspace and allows both the parent and child to execute independently,<CODE>vfork</CODE> does not make this copy.  Instead, the child processcreated with <CODE>vfork</CODE> shares its parent's address space until it callsone of the <CODE>exec</CODE> functions.  In the meantime, the parent processsuspends execution.<P>You must be very careful not to allow the child process created with<CODE>vfork</CODE> to modify any global data or even local variables sharedwith the parent.  Furthermore, the child process cannot return from (ordo a long jump out of) the function that called <CODE>vfork</CODE>!  Thiswould leave the parent process's control information very confused.  Ifin doubt, use <CODE>fork</CODE> instead.<P>Some operating systems don't really implement <CODE>vfork</CODE>.  The GNU Clibrary permits you to use <CODE>vfork</CODE> on all systems, but actuallyexecutes <CODE>fork</CODE> if <CODE>vfork</CODE> isn't available.  If you followthe proper precautions for using <CODE>vfork</CODE>, your program will stillwork even if the system uses <CODE>fork</CODE> instead.<P><A NAME="IDX1702"></A><A NAME="IDX1703"></A><H2><A NAME="SEC406" HREF="library_toc.html#SEC406" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC406">Executing a File</A></H2>

⌨️ 快捷键说明

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