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:

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.

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:

POSIX 1003.1

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.

See also:

errno, execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), spawn(), spawnl(), spawnle(), spawnlp(), spawnlpe(), spawnp(), spawnv(), spawnve(), spawnvp(), spawnvpe(), wait()

Processes and Threads chapter of Getting Started with QNX Neutrino