fork()

Create a new process

Synopsis:

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

pid_t fork( void );

Since:

BlackBerry 10.0.0

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.
  • The child process has a single thread, regardless of the number that the parent process has.
  • 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.

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

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
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:

POSIX 1003.1

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

Last modified: 2014-06-24



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus