Penguin
Annotated edit history of fork(2) version 5, including all changes. View license author blame.
Rev Author # Line
1 perry 1 !!NAME
2 WikiAdmin 2 fork - create a child process
1 perry 3
4 !!SYNOPSIS
2 WikiAdmin 5 __#include <sys/types.h>__ /* for pid_t */
6 __#include <unistd.h>__ /* for fork(2) prototype */
7 __pid_t fork(void);__
1 perry 8
9 !!DESCRIPTION
2 WikiAdmin 10 fork(2) creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited.
1 perry 11
2 WikiAdmin 12 Under Linux, and in fact any modern implementation of Unix, fork(2) is implemented using copy-on-write pages, so the only penalty incurred by fork is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.
1 perry 13
14 !!RETURN VALUE
2 WikiAdmin 15 On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and ''errno'' will be set appropriately.
1 perry 16
17 !!ERRORS
2 WikiAdmin 18 ;[EAGAIN]: fork(2) cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child, or the number of processors would exceed the limit of the number of processes either system wide, or for a particular users. (CHILD_MAX)
5 PerryLorier 19 ;:__Note__: The Linux kernel appears to only return this error if the fork(2) would exceed the maximum allowed number of processes from one limit or another.
2 WikiAdmin 20 ;[ENOMEM]: fork(2) failed to allocate the necessary kernel structures because memory is tight.
1 perry 21
2 WikiAdmin 22 !!CONFORMING TO
3 JohnMcPherson 23 The fork(2) call conforms to SVr4, SVID, [POSIX], X/OPEN, BSD 4.3.
1 perry 24
2 WikiAdmin 25 !!EXAMPLE
26 /*
27 * This program demonstrates fork(2).
28 */
1 perry 29
2 WikiAdmin 30 #include <sys/types.h> /* for pid_t */
31 #include <unistd.h> /* for fork(2) */
32 #include <stdio.h> /* for printf(3), fprintf(3) */
33 #include <string.h> /* for strerror(3) */
34 #include <errno.h> /* for errno */
1 perry 35
2 WikiAdmin 36 int main(int argc,char **argv)
37 {
38 pid_t pid;
1 perry 39
2 WikiAdmin 40 pid = fork();
1 perry 41
2 WikiAdmin 42 if (pid == -1) {
43 fprintf(
44 stderr,
45 "fork(): %s (%i)\n",
46 strerror(errno),
47 errno);
48 return 1;
49 }
1 perry 50
2 WikiAdmin 51 if (pid == 0) {
52 printf("I am the child!\n");
1 perry 53
2 WikiAdmin 54 for(;;) {
55 printf("a");
56 fflush(stdout);
57 }
58 }
59 else {
60 printf("I am the parent! The child's PID is %i\n",pid);
1 perry 61
2 WikiAdmin 62 for(;;) {
63 printf("b"); /* Delete previous char */
64 fflush(stdout);
65 }
66 }
67 return 0;
68 }
4 JohnMcPherson 69
70 Note that on a machine with a single [CPU], this will output large groups of "aaaaa...." followed by large groups of "bbbb..." as each process gets a timeslice. On a machine with more than one CPU, you might be more likely to see "a" and "b" interspersed in smaller units as both processes can run at the same time.
1 perry 71
72 !!SEE ALSO
2 WikiAdmin 73 clone(2), execve(2), vfork(2), wait(2)
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 6 times)