blob: b06c1e50eaeb38febac857c6c33cf6bc0b919008 [file] [log] [blame]
alaffin7ce3a792000-07-27 16:59:03 +00001.\"
2.\" $Id: forker.3,v 1.1 2000/07/27 16:59:03 alaffin Exp $
3.\"
4.\" Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
5.\"
6.\" This program is free software; you can redistribute it and/or modify it
7.\" under the terms of version 2 of the GNU General Public License as
8.\" published by the Free Software Foundation.
9.\"
10.\" This program is distributed in the hope that it would be useful, but
11.\" WITHOUT ANY WARRANTY; without even the implied warranty of
12.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13.\"
14.\" Further, this software is distributed without any warranty that it is
15.\" free of the rightful claim of any third person regarding infringement
16.\" or the like. Any license provided herein, whether implied or
17.\" otherwise, applies only to this software file. Patent licenses, if
18.\" any, provided herein do not apply to combinations of this program with
19.\" other software, or any other product whatsoever.
20.\"
21.\" You should have received a copy of the GNU General Public License along
22.\" with this program; if not, write the Free Software Foundation, Inc., 59
23.\" Temple Place - Suite 330, Boston MA 02111-1307, USA.
24.\"
25.\" Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
26.\" Mountain View, CA 94043, or:
27.\"
28.\" http://www.sgi.com
29.\"
30.\" For further information regarding this notice, see:
31.\"
32.\" http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33.\"
34.TH FORKER 3 07/25/2000 "Linux Test Project"
35.SH NAME
36forker \- fork desired number of copies of the current process
37.br
38background \- fork a process and return control to caller
39
40.SH SYNOPSIS
41int
42.br
43\fBbackground\fR(\fIprefix\fR)
44.br
45char *\fIprefix\fR;
46
47int
48.br
49\fBforker\fR(\fIncopies, mode, prefix\fR)
50.br
51int \fIncopies\fR;
52.br
53int \fImode\fR;
54.br
55char *\fIprefix\fR;
56
57extern int \fBForker_pids\fR[];
58.br
59extern int \fBForker_npids\fR;
60
61.SH DESCRIPTION
62The \fBbackground\fR function will do a fork of the current process.
63The parent process will then exit, thus orphaning the
64child process. Doing this will not nice the child process
65like executing a cmd in the background using "&" from the shell.
66If the fork fails and prefix is not NULL, a error message is printed
67to stderr and the process will exit with a value of errno.
68
69The \fBforker\fR function will fork \fIncopies\fR minus one copies
70of the current process. There are two modes in how the forks
71will be done. Mode 0 (default) will have all new processes
72be children of the parent process. Using Mode 1,
73the parent process will have one child and that child will
74fork the next process, if necessary, and on and on.
75The \fBforker\fR function will return the number of successful
76forks. This value will be different for the parent and each child.
77Using mode 0, the parent will get the total number of successful
78forks. Using mode 1, the newest child will get the total number
79of forks. The parent will get a return value of 1.
80
81The \fBforker \fRfunction also updates the global variables
82\fIForker_pids[\fR] and Forker_npids. The \fIForker_pids\fR array will
83be updated to contain the pid of each new process. The
84\fIForker_npids\fR variable contains the number of entries
85in \fIForker_pids.\fR Note, not all processes will have
86access to all pids via \fIForker_pids.\fR If using mode 0, only the
87parent process will have all information. If using mode 1,
88only the last child process will have all information.
89
90If the \fIprefix\fR parameter is not NULL and the fork system call fails,
91a error message will be printed to stderr. The error message
92will be preceded with \fIprefix\fR string. If \fIprefix\fR is NULL,
93no error message is printed.
94
95.SH EXAMPLES
96
97.nf
98.in +3
99/*
100 * The following is a unit test main for the background and forker
101 * functions.
102 */
103
104#include <stdio.h>
105
106main(argc, argv)
107int argc;
108char **argv;
109{
110 int ncopies=1;
111 int mode=0;
112 int ret;
113
114 if ( argc == 1 ) {
115 printf("Usage: %s ncopies [mode]\n", argv[0]);
116 exit(1);
117 }
118
119 if ( sscanf(argv[1], "%i", &ncopies) != 1 ) {
120 printf("%s: ncopies argument must be integer\n", argv[0]);
121 exit(1);
122 }
123
124 if ( argc == 3 )
125 if ( sscanf(argv[2], "%i", &mode) != 1 ) {
126 printf("%s: mode argument must be integer\n", argv[0]);
127 exit(1);
128 }
129
130 printf("Starting Pid = %d\n", getpid());
131 ret=background(argv[0]);
132 printf("After background() ret:%d, pid = %d\n", ret, getpid());
133
134 ret=forker(ncopies, mode, argv[0]);
135
136 printf("forker(%d, %d, %s) ret:%d, pid = %d, sleeping 30 seconds.\n",
137 ncopies, mode, argv[0], ret, getpid());
138 sleep(30);
139 exit(0);
140}
141.in -3
142.fi
143
144.SH "SEE ALSO"
145fork(2).
146
147.SH BUGS
148The child pids are stored in the fixed array, \fIForker_pids\fR.
149The array only has space for 4098 pids. Only the first
1504098 pids will be stored in the array.
151