Penguin

Differences between version 2 and predecessor to the previous major change of fork(2).

Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History

Newer page: version 2 Last edited on Sunday, November 3, 2002 10:34:52 pm by WikiAdmin Revert
Older page: version 1 Last edited on Tuesday, June 4, 2002 12:23:40 am by perry Revert
@@ -1,72 +1,70 @@
-FORK  
-!!!FORK  
-NAME  
-SYNOPSIS  
-DESCRIPTION  
-RETURN VALUE  
-ERRORS  
-CONFORMING TO  
-SEE ALSO  
-----  
 !!NAME 
+fork - create a child process  
  
-  
-fork - create a child process  
 !!SYNOPSIS 
+ __#include <sys/types.h>__ /* for pid_t */  
+ __#include <unistd.h>__ /* for fork(2) prototype */  
+ __pid_t fork(void);__  
  
-  
-__#include __  
-#include __  
-  
-  
-__pid_t fork(void);__  
 !!DESCRIPTION 
+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.  
  
+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.  
  
-__fork__ 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.  
-  
-  
-Under Linux, __fork__ 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.  
 !!RETURN VALUE 
+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.  
  
-  
-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.  
 !!ERRORS 
+;[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)  
+;[ENOMEM]: fork(2) failed to allocate the necessary kernel structures because memory is tight.  
  
+!!CONFORMING TO  
+The fork(2) call conforms to SVr4, SVID, POSIX, X/OPEN, BSD 4.3.  
  
-__EAGAIN__  
+!!EXAMPLE  
+ /*  
+ * This program demonstrates fork(2).  
+ */  
  
+ #include <sys/types.h> /* for pid_t */  
+ #include <unistd.h> /* for fork(2) */  
+ #include <stdio.h> /* for printf(3), fprintf(3) */  
+ #include <string.h> /* for strerror(3) */  
+ #include <errno.h> /* for errno */  
  
-__fork__ cannot allocate sufficient memory to copy the  
-parent's page tables and allocate a task structure for the  
-child.  
+ int main(int argc,char **argv)  
+ {  
+ pid_t pid;  
  
+ pid = fork();  
  
-__ENOMEM__  
+ if (pid == -1) {  
+ fprintf(  
+ stderr,  
+ "fork(): %s (%i)\n",  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
  
+ if (pid == 0) {  
+ printf("I am the child!\n");  
  
-__fork__ failed to allocate the necessary kernel  
-structures because memory is tight.  
-!!CONFORMING TO  
+ for(;;) {  
+ printf("a");  
+ fflush(stdout);  
+ }  
+ }  
+ else {  
+ printf("I am the parent ! The child's PID is %i\n",pid);  
  
+ for(;;) {  
+ printf("b"); /* Delete previous char */  
+ fflush(stdout);  
+ }  
+ }  
+ return 0;  
+ }  
  
-The __fork__ call conforms to SVr4, SVID, POSIX, X/OPEN,  
-BSD 4.3.  
 !!SEE ALSO 
-  
-  
- clone(2), execve(2), vfork(2),  
- wait(2)  
-----  
+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.