fork()

Create a new process

Synopsis:

#include <sys/types.h>
#include <unistd.h>

pid_t fork( void );

This function is declared in <process.h>, which <unistd.h> includes.

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:

Note that:

In order to successfully call this function, your process must have the PROCMGR_AID_FORK ability enabled. For more information, see procmgr_ability().

You can use pthread_atfork() to register fork handler functions to be called before or after the fork occurs.

Note: If your program uses mutexes, you need to register a pthread_atfork() handler that locks all the mutexes before you fork.

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.
EPERM
One of the following occurred:
  • The calling process doesn't have the required permission; see procmgr_ability().
  • (QNX Neutrino 7.0.1 or later) The calling process's priority is above the permitted range.

Examples:

This program demonstrates forking. You run it like this:

fork_example program [arguments]

The child runs program (passing it the arguments, if any), and the parent waits for the child to exit.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main( int argc, char **argv )
{
    pid_t pid;
    pid_t wpid;
    int   status;

    if (argc == 1) {
        printf ("Usage: %s <program> [<arguments>]\n", argv[0]);
        return EXIT_FAILURE;
    }

    if( ( pid = fork() ) == -1 ) {
        perror( "fork" );
        return EXIT_FAILURE;
    }

    if( pid == 0 ) {
        printf ("Child: My pid is %d.\n", getpid() );

        /* Use exec to become the specified program. */
        execvp( argv[1], argv+1 );

        /* This can happen only if execvp() fails; print a message, and then exit. */
        perror( argv[1] );
        return EXIT_FAILURE;

    } else {
        printf ("Parent: My pid is %d.\n", getpid() );

        /* Wait for the child to finish. */
        do {
            wpid = waitpid( pid, &status, 0 );
        } while( WIFEXITED( status ) == 0 );

        printf ("Parent: Child %d has exited with status %d; we're done!\n",
                wpid, WEXITSTATUS( status ));
        return WEXITSTATUS( status );
    }
}

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes