Procesos Man

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

FORK(2) Linux Programmer’s Manual FORK(2)

NAME
fork − create a child process

SYNOPSIS
#include <unistd.h>

pid_t fork(void);

DESCRIPTION
fork creates a child process that differs from the parent
process only in its PID and PPID, and in the fact that
resource utilizations are set to 0. File locks and pend-
ing signals are not inherited.

Under Linux, fork is implemented using copy−on−write


pages, so the only penalty incurred by fork is the time
and memory required to duplicate the parent’s page tables,
and to create a unique task structure for the child.

RETURN VALUE
On success, the PID of the child process is returned in
the parent’s thread of execution, and a 0 is returned in
the child’s thread of execution. On failure, a −1 will be
returned in the parent’s context, no child process will be
created, and errno will be set appropriately.

ERRORS
EAGAIN fork cannot allocate sufficient memory to copy the
parent’s page tables and allocate a task structure
for the child.

ENOMEM fork failed to allocate the necessary kernel struc-


tures because memory is tight.

CONFORMING TO
The fork call conforms to SVr4, SVID, POSIX, X/OPEN, BSD
4.3.

SEE ALSO
clone(2), execve(2), vfork(2), wait(2)

Linux 1.2.9 10 June 1995 1


EXIT(3) Linux Programmer’s Manual EXIT(3)

NAME
exit − cause normal program termination

SYNOPSIS
#include <stdlib.h>

void exit(int status);

DESCRIPTION
The exit() function causes normal program termination and
the value of status is returned to the parent. All func-
tions registered with atexit() and on_exit() are called in
the reverse order of their registration, and all open
streams are flushed and closed.

RETURN VALUE
The exit() function does not return.

CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO
_exit(2), atexit(3), on_exit(3)

GNU April 2, 1993 1


WAIT(2) Linux Programmer’s Manual WAIT(2)

NAME
wait, waitpid − wait for process termination

SYNOPSIS
#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *status)


pid_t waitpid(pid_t pid, int *status, int options);

DESCRIPTION
The wait function suspends execution of the current pro-
cess until a child has exited, or until a signal is deliv-
ered whose action is to terminate the current process or
to call a signal handling function. If a child has
already exited by the time of the call (a so−called "zom-
bie" process), the function returns immediately. Any sys-
tem resources used by the child are freed.

The waitpid function suspends execution of the current


process until a child as specified by the pid argument has
exited, or until a signal is delivered whose action is to
terminate the current process or to call a signal handling
function. If a child as requested by pid has already
exited by the time of the call (a so−called "zombie" pro-
cess), the function returns immediately. Any system
resources used by the child are freed.

The value of pid can be one of:

< −1 which means to wait for any child process whose


process group ID is equal to the absolute value of
pid.

−1 which means to wait for any child process; this is


the same behaviour which wait exhibits.

0 which means to wait for any child process whose


process group ID is equal to that of the calling
process.

> 0 which means to wait for the child whose process ID


is equal to the value of pid.

The value of options is an OR of zero or more of the fol-


lowing constants:

WNOHANG
which means to return immediately if no child has
exited.

WUNTRACED
which means to also return for children which are
stopped, and whose status has not been reported.

Linux 23 June 1997 1


WAIT(2) Linux Programmer’s Manual WAIT(2)

If status is not NULL, wait or waitpid store status infor-


mation in the location pointed to by status.

This status can be evaluated with the following macros


(these macros take the stat buffer (an int) as an argument
−− not a pointer to the buffer!):

WIFEXITED(status)
is non−zero if the child exited normally.

WEXITSTATUS(status)
evaluates to the least significant eight bits of
the return code of the child which terminated,
which may have been set as the argument to a call
to exit() or as the argument for a return statement
in the main program. This macro can only be evalu-
ated if WIFEXITED returned non−zero.

WIFSIGNALED(status)
returns true if the child process exited because of
a signal which was not caught.

WTERMSIG(status)
returns the number of the signal that caused the
child process to terminate. This macro can only be
evaluated if WIFSIGNALED returned non−zero.

WIFSTOPPED(status)
returns true if the child process which caused the
return is currently stopped; this is only possible
if the call was done using WUNTRACED.

WSTOPSIG(status)
returns the number of the signal which caused the
child to stop. This macro can only be evaluated if
WIFSTOPPED returned non−zero.

RETURN VALUE
The process ID of the child which exited, −1 on error or
zero if WNOHANG was used and no child was available (in
which case, errno is set to an appropriate value).

ERRORS
ECHILD if the process specified in pid does not exist or
is not a child of the calling process. (This can
happen for one’s own child if the action for
SIGCHLD is set to SIG_IGN.)

EINVAL if the options argument was invalid.

ERESTARTSYS
if WNOHANG was not set and an unblocked signal or a
SIGCHLD was caught. This error is returned by the
system call. The library interface is not allowed

Linux 23 June 1997 2


WAIT(2) Linux Programmer’s Manual WAIT(2)

to return ERESTARTSYS, but will return EINTR.

NOTES
The Single Unix Specification describes a flag SA_NOCLD-
WAIT (not present under Linux) such that if either this
flag is set, or the action for SIGCHLD is set to SIG_IGN
(which, by the way, is not allowed by POSIX), then chil-
dren that exit do not become zombies and a call to wait()
or waitpid() will block until all children have exited,
and then fail with errno set to ECHILD.

CONFORMING TO
SVr4, POSIX.1

SEE ALSO
signal(2), wait4(2), signal(7)

Linux 23 June 1997 3


EXEC(3) Linux Programmer’s Manual EXEC(3)

NAME
execl, execlp, execle, execv, execvp − execute a file

SYNOPSIS
#include <unistd.h>

extern char **environ;

int execl( const char *path, const char *arg, ...);


int execlp( const char *file, const char *arg, ...);
int execle( const char *path, const char *arg , ..., char
* const envp[]);
int execv( const char *path, char *const argv[]);
int execvp( const char *file, char *const argv[]);

DESCRIPTION
The exec family of functions replaces the current process
image with a new process image. The functions described
in this manual page are front−ends for the function
execve(2). (See the manual page for execve for detailed
information about the replacement of the current process.)

The initial argument for these functions is the pathname


of a file which is to be executed.

The const char *arg and subsequent ellipses in the execl,


execlp, and execle functions can be thought of as arg0,
arg1, ..., argn. Together they describe a list of one or
more pointers to null−terminated strings that represent
the argument list available to the executed program. The
first argument, by convention, should point to the file
name associated with the file being executed. The list of
arguments must be terminated by a NULL pointer.

The execv and execvp functions provide an array of point-


ers to null−terminated strings that represent the argument
list available to the new program. The first argument, by
convention, should point to the file name associated with
the file being executed. The array of pointers must be
terminated by a NULL pointer.

The execle function also specifies the environment of the


executed process by following the NULL pointer that termi-
nates the list of arguments in the parameter list or the
pointer to the argv array with an additional parameter.
This additional parameter is an array of pointers to null−
terminated strings and must be terminated by a NULL
pointer. The other functions take the environment for the
new process image from the external variable environ in
the current process.

Some of these functions have special semantics.

The functions execlp and execvp will duplicate the actions

BSD MANPAGE 29 November 1993 1


EXEC(3) Linux Programmer’s Manual EXEC(3)

of the shell in searching for an executable file if the


specified file name does not contain a slash (/) charac-
ter. The search path is the path specified in the envi-
ronment by the PATH variable. If this variable isn’t
specified, the default path ‘‘:/bin:/usr/bin’’ is used.
In addition, certain errors are treated specially.

If permission is denied for a file (the attempted execve


returned EACCES), these functions will continue searching
the rest of the search path. If no other file is found,
however, they will return with the global variable errno
set to EACCES.

If the header of a file isn’t recognized (the attempted


execve returned ENOEXEC), these functions will execute the
shell with the path of the file as its first argument.
(If this attempt fails, no further searching is done.)

RETURN VALUES
If any of the exec functions returns, an error will have
occurred. The return value is −1, and the global variable
errno will be set to indicate the error.

FILES
/bin/sh

ERRORS
All of these functions may fail and set errno for any of
the errors specified for the library function execve(2).

SEE ALSO
sh(1), execve(2), fork(2), environ(5), ptrace(2)

COMPATIBILITY
On some other systems the default PATH has the current
working directory listed after /bin and /usr/bin, as an
anti−Trojan−horse measure. As of libc 5.4.7, Linux still
uses the traditional "current directory first" default
PATH.

The behavior of execlp and execvp when errors occur while


attempting to execute the file is historic practice, but
has not traditionally been documented and is not specified
by the POSIX standard. BSD (and possibly other systems) do
an automatic sleep and retry if ETXTBSY is encountered.
Linux treats it as a hard error and returns immediately.

Traditionally, the functions execlp and execvp ignored all


errors except for the ones described above and ENOMEM and
E2BIG, upon which they returned. They now return if any
error other than the ones described above occurs.

STANDARDS
Execl, execv, execle, execlp and execvp conform to IEEE

BSD MANPAGE 29 November 1993 2


EXEC(3) Linux Programmer’s Manual EXEC(3)

Std1003.1−88 (‘‘POSIX.1’’).

BSD MANPAGE 29 November 1993 3

You might also like