blob: 6eab68eaf27653ca90a04f557669cfdc598f9904 [file] [log] [blame]
<html lang="en">
<head>
<title>Process Creation Example - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Processes.html#Processes" title="Processes">
<link rel="prev" href="BSD-Wait-Functions.html#BSD-Wait-Functions" title="BSD Wait Functions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Process-Creation-Example"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="BSD-Wait-Functions.html#BSD-Wait-Functions">BSD Wait Functions</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Processes.html#Processes">Processes</a>
<hr>
</div>
<h3 class="section">26.9 Process Creation Example</h3>
<p>Here is an example program showing how you might write a function
similar to the built-in <code>system</code>. It executes its <var>command</var>
argument using the equivalent of &lsquo;<samp><span class="samp">sh -c </span><var>command</var></samp>&rsquo;.
<pre class="smallexample"> #include &lt;stddef.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;
/* <span class="roman">Execute the command using this shell program.</span> */
#define SHELL "/bin/sh"
int
my_system (const char *command)
{
int status;
pid_t pid;
pid = fork ();
if (pid == 0)
{
/* <span class="roman">This is the child process. Execute the shell command.</span> */
execl (SHELL, SHELL, "-c", command, NULL);
_exit (EXIT_FAILURE);
}
else if (pid &lt; 0)
/* <span class="roman">The fork failed. Report failure.</span> */
status = -1;
else
/* <span class="roman">This is the parent process. Wait for the child to complete.</span> */
if (waitpid (pid, &amp;status, 0) != pid)
status = -1;
return status;
}
</pre>
<!-- Yes, this example has been tested. -->
<p>There are a couple of things you should pay attention to in this
example.
<p>Remember that the first <code>argv</code> argument supplied to the program
represents the name of the program being executed. That is why, in the
call to <code>execl</code>, <code>SHELL</code> is supplied once to name the program
to execute and a second time to supply a value for <code>argv[0]</code>.
<p>The <code>execl</code> call in the child process doesn't return if it is
successful. If it fails, you must do something to make the child
process terminate. Just returning a bad status code with <code>return</code>
would leave two processes running the original program. Instead, the
right behavior is for the child process to report failure to its parent
process.
<p>Call <code>_exit</code> to accomplish this. The reason for using <code>_exit</code>
instead of <code>exit</code> is to avoid flushing fully buffered streams such
as <code>stdout</code>. The buffers of these streams probably contain data
that was copied from the parent process by the <code>fork</code>, data that
will be output eventually by the parent process. Calling <code>exit</code> in
the child would output the data twice. See <a href="Termination-Internals.html#Termination-Internals">Termination Internals</a>.
</body></html>