fork()
Create a new process
Synopsis:
#include <sys/types.h> #include <process.h> pid_t fork( void );
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The fork() function creates a new process. The new process (child process) is an exact copy of the calling process (parent process), except for the following:
- The child process has a unique process ID.
- The child process has a different parent process ID (which is the process ID of the calling process).
- The child process has its own copy of the parent's file descriptors. Each of the child's file descriptors refers to the same open file description with the corresponding file descriptor of the parent.
- The child process has its own copy of the parent's open directory streams.
- The child process's values of tms_utime, tms_stime, tms_cutime, and tms_cstime are set to zero.
- File locks previously set by the parent aren't inherited by the child.
- Pending alarms are cleared for the child process.
- The set of signals pending for the child process is initialized to the empty set.
- The child process doesn't inherit any memory locks that the parent set. For more information, see Locking memory in the Process Manager chapter of the System Architecture guide.
In order to successfully call this function, your process must have the PROCMGR_AID_FORK ability enabled. For more information, see procmgr_ability() .
The parent's ability configuration is copied to the child verbatim, regardless of the PROCMGR_AOP_INHERIT_NO status of each of the abilities.
Returns:
A value of zero to the child process; and the process ID of the child process to the parent process. Both processes continue to execute from the fork() function. If an error occurs, fork() returns -1 to the parent and sets errno .
Errors:
- EAGAIN
- Insufficient resources are available to create the child process. For example, you might have exceeded the maximum number of processes permitted; see the RLIMIT_NPROC resource for getrlimit() .
- ENOMEM
- The process requires more memory than the system is able to supply.
- ENOSYS
- The fork() function isn't implemented for this memory protection model. See also Caveats, below.
- EPERM
- The calling process doesn't have the required permission; see procmgr_ability() .
Examples:
/* * This program executes the program and arguments * specified by argv[1..argc]. The standard input * of the executed program is converted to upper * case. */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <ctype.h> #include <process.h> #include <sys/wait.h> int main( int argc, char **argv ) { pid_t pid; pid_t wpid; int fd[2]; char buffer[80]; int i, len; int status; if( pipe( fd ) == -1 ) { perror( "pipe" ); return EXIT_FAILURE; } if( ( pid = fork() ) == -1 ) { perror( "fork" ); return EXIT_FAILURE; } if( pid == 0 ) { /* This is the child process. * Move read end of the pipe to stdin ( 0 ), * close any extraneous file descriptors, * then use exec to 'become' the command. */ dup2( fd[0], 0 ); close( fd[1] ); execvp( argv[1], argv+1 ); /* This can only happen if exec fails; print message * and exit. */ perror( argv[1] ); return EXIT_FAILURE; } else { /* This is the parent process. * Remove extraneous file descriptors, * read descriptor 0, write into pipe, * close pipe, and wait for child to die. */ close( fd[0] ); while( ( len = read( 0, buffer, sizeof( buffer ) ) ) > 0 ) { for( i = 0; i < len; i++ ) { if( isupper( buffer[i] ) ) buffer[i] = tolower( buffer[i] ); } write( fd[1], buffer, len ); } close( fd[1] ); do { wpid = waitpid( pid, &status, 0 ); } while( WIFEXITED( status ) == 0 ); return WEXITSTATUS( status ); } }
Classification:
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
Caveats:
Currently, fork() is supported only in single-threaded applications. If you create a thread and then call fork(), the function returns -1 and sets errno to ENOSYS.