alaffin | 7ce3a79 | 2000-07-27 16:59:03 +0000 | [diff] [blame^] | 1 | .\" |
| 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 |
| 36 | forker \- fork desired number of copies of the current process |
| 37 | .br |
| 38 | background \- fork a process and return control to caller |
| 39 | |
| 40 | .SH SYNOPSIS |
| 41 | int |
| 42 | .br |
| 43 | \fBbackground\fR(\fIprefix\fR) |
| 44 | .br |
| 45 | char *\fIprefix\fR; |
| 46 | |
| 47 | int |
| 48 | .br |
| 49 | \fBforker\fR(\fIncopies, mode, prefix\fR) |
| 50 | .br |
| 51 | int \fIncopies\fR; |
| 52 | .br |
| 53 | int \fImode\fR; |
| 54 | .br |
| 55 | char *\fIprefix\fR; |
| 56 | |
| 57 | extern int \fBForker_pids\fR[]; |
| 58 | .br |
| 59 | extern int \fBForker_npids\fR; |
| 60 | |
| 61 | .SH DESCRIPTION |
| 62 | The \fBbackground\fR function will do a fork of the current process. |
| 63 | The parent process will then exit, thus orphaning the |
| 64 | child process. Doing this will not nice the child process |
| 65 | like executing a cmd in the background using "&" from the shell. |
| 66 | If the fork fails and prefix is not NULL, a error message is printed |
| 67 | to stderr and the process will exit with a value of errno. |
| 68 | |
| 69 | The \fBforker\fR function will fork \fIncopies\fR minus one copies |
| 70 | of the current process. There are two modes in how the forks |
| 71 | will be done. Mode 0 (default) will have all new processes |
| 72 | be children of the parent process. Using Mode 1, |
| 73 | the parent process will have one child and that child will |
| 74 | fork the next process, if necessary, and on and on. |
| 75 | The \fBforker\fR function will return the number of successful |
| 76 | forks. This value will be different for the parent and each child. |
| 77 | Using mode 0, the parent will get the total number of successful |
| 78 | forks. Using mode 1, the newest child will get the total number |
| 79 | of forks. The parent will get a return value of 1. |
| 80 | |
| 81 | The \fBforker \fRfunction also updates the global variables |
| 82 | \fIForker_pids[\fR] and Forker_npids. The \fIForker_pids\fR array will |
| 83 | be updated to contain the pid of each new process. The |
| 84 | \fIForker_npids\fR variable contains the number of entries |
| 85 | in \fIForker_pids.\fR Note, not all processes will have |
| 86 | access to all pids via \fIForker_pids.\fR If using mode 0, only the |
| 87 | parent process will have all information. If using mode 1, |
| 88 | only the last child process will have all information. |
| 89 | |
| 90 | If the \fIprefix\fR parameter is not NULL and the fork system call fails, |
| 91 | a error message will be printed to stderr. The error message |
| 92 | will be preceded with \fIprefix\fR string. If \fIprefix\fR is NULL, |
| 93 | no 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 | |
| 106 | main(argc, argv) |
| 107 | int argc; |
| 108 | char **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" |
| 145 | fork(2). |
| 146 | |
| 147 | .SH BUGS |
| 148 | The child pids are stored in the fixed array, \fIForker_pids\fR. |
| 149 | The array only has space for 4098 pids. Only the first |
| 150 | 4098 pids will be stored in the array. |
| 151 | |