fork |
Prototype: |
#include <unistd.h>
int fork(void);
|
General Description: |
Create a new process (independent task) at this call. This call creates a child process to run with the parent. You must be careful that you capture the child and direct it to its assigned task; otherwise, the child runs each statement the parent does (run together). |
Return Value: |
0 - The task that gets this is the child. >0 - The task that gets this is the parent. <0 - The parent failed to create a new child check errno. |
Parameters |
(none)
|
|
Possible Errors |
EAGAIN |
The fork() cannot allocate sufficient memory to copy the parent's page tables and allocate a task structure for the child. |
ENOMEM |
The fork() failed to allocate the necessary kernel structures because memory is tight. |
Examples |
int PID;
if ( (PID = fork()) == 0 )
{ /*--- CHILD ---*/
/**** Run the child's assignment ***/
exit();
}
else if ( PID > 0 )
{ /*--- PARENT ---*/
int status;
/**** Do parent's work ****/
wait(status); /* may be done in SIGCHLD signal handler */
}
else /*--- ERROR ---*/
perror("fork() failed");
|