Poster of Linux kernelThe best gift for a Linux geek
FORKER

FORKER

Section: C Library Functions (3) Updated: 07/25/2000
Local index Up
 

NAME

forker - fork desired number of copies of the current process
background - fork a process and return control to caller

 

SYNOPSIS

int
background(prefix)
char *prefix;

int
forker(ncopies, mode, prefix)
int ncopies;
int mode;
char *prefix;

extern int Forker_pids[];
extern int Forker_npids;

 

DESCRIPTION

The background function will do a fork of the current process. The parent process will then exit, thus orphaning the child process. Doing this will not nice the child process like executing a cmd in the background using "&" from the shell. If the fork fails and prefix is not NULL, a error message is printed to stderr and the process will exit with a value of errno.

The forker function will fork ncopies minus one copies of the current process. There are two modes in how the forks will be done. Mode 0 (default) will have all new processes be children of the parent process. Using Mode 1, the parent process will have one child and that child will fork the next process, if necessary, and on and on. The forker function will return the number of successful forks. This value will be different for the parent and each child. Using mode 0, the parent will get the total number of successful forks. Using mode 1, the newest child will get the total number of forks. The parent will get a return value of 1.

The forker function also updates the global variables Forker_pids[] and Forker_npids. The Forker_pids array will be updated to contain the pid of each new process. The Forker_npids variable contains the number of entries in Forker_pids. Note, not all processes will have access to all pids via Forker_pids. If using mode 0, only the parent process will have all information. If using mode 1, only the last child process will have all information.

If the prefix parameter is not NULL and the fork system call fails, a error message will be printed to stderr. The error message will be preceded with prefix string. If prefix is NULL, no error message is printed.

 

EXAMPLES

/*
 * The following is a unit test main for the background and forker
 * functions.
 */

#include <stdio.h>

main(argc, argv)
int argc;
char **argv;
{
    int ncopies=1;
    int mode=0;
    int ret;

    if ( argc == 1 ) {
        printf("Usage: %s ncopies [mode], argv[0]);
        exit(1);
    }

    if ( sscanf(argv[1], "%i", &ncopies) != 1 ) {
        printf("%s: ncopies argument must be integer, argv[0]);
        exit(1);
    }

    if ( argc == 3 )
        if ( sscanf(argv[2], "%i", &mode) != 1 ) {
        printf("%s: mode argument must be integer, argv[0]);
        exit(1);
    }

    printf("Starting Pid = %d, getpid());
    ret=background(argv[0]);
    printf("After background() ret:%d, pid = %d, ret, getpid());

    ret=forker(ncopies, mode, argv[0]);

    printf("forker(%d, %d, %s) ret:%d, pid = %d, sleeping 30 seconds.,
        ncopies, mode, argv[0], ret, getpid());
    sleep(30);
    exit(0);
}

 

SEE ALSO

fork(2).

 

BUGS

The child pids are stored in the fixed array, Forker_pids. The array only has space for 4098 pids. Only the first 4098 pids will be stored in the array.


 

Index

NAME
SYNOPSIS
DESCRIPTION
EXAMPLES
SEE ALSO
BUGS

This document was created by man2html, using the manual pages.
Time: 21:46:19 GMT, April 16, 2011