fork():
The fork() system call will spawn a new child process which is an identical process to the parent except that has a new system process ID. The process is copied in memory from the parent and a new process structure is assigned by the kernel. The return value of the function is which discriminates the two threads of execution. A zero is returned by the fork function in the child's process.
The environment, resource limits, umask, controlling terminal, current working directory, root directory, signal masks and other process resources are also duplicated from the parent in the forked child process.
Example:
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<stdlib.h>
4 int globalVariable = 2;
5 int main(){
6 char *sIdentifier;
7 int iStackVariable = 20;
8
9 pid_t pID = fork();
10
11 if( pID == 0 ) {
12 //Code only executed by child process
13 sIdentifier = "Child Process: ";
14 globalVariable++;
15 iStackVariable++;
16 } else if ( pID < 0 ) {
17 printf( "Failed to fork!/n" );
18 exit(1);
19 //Throw exception
20 } else {
21 //Code only executed by parent process
22 sIdentifier = "Parent Process: ";
23 }
24
25 //Code executed by both parent and child.
26 printf( " %s/n", sIdentifier );
27 printf( " Global variable: %d/n", globalVariable );
28 printf( " Stack variable: %d/n", iStackVariable );
29 }
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ gcc fork.c
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ ./a.out
Parent Process:
Global variable: 2
Stack variable: 20
Child Process:
Global variable: 3
Stack variable: 21
[Potential Pitfall] : Some memory duplicated by a forked process such as file pointers, will cause intermixed output from both processes. Use the wait() function so that the processes do not access the file at the same time or open unique file descriptors. Some like stdout or stderr will be shared unless synchronized using wait() or some other mechanism. The file close on exit is another gotcha. A terminating process will close files before exiting. File locks set by the parent process are not inherited by the child process.
[Potential Pitfall] : Race conditions can be created due to the unpredictability of when the kernel scheduler runs portions or time slices of the process. One can use wait() . the use of sleep() does not guarentee reliability of execution on a heavily loaded system as the scheduler behavior is not predictable by the application.
Note on exit() vs _exit() : The C library function exit() calls the kernel system call _exit() internally. The kernel system call _exit() will cause the kernel to close descriptors, free memory, and perform the kernel terminating process clean-up. The C library function exit() call will flush I/O buffers and perform aditional clean-up before calling _exit() internally. The function exit(status ) causes the executable to return "status" as the return code for main() . When exit(status ) is called by a child process, it allows the parent process to examine the terminating status of the child (if it terminates first). Without this call (or a call from main() to return() ) and specifying the status argument, the process will not return a value.
-
#include <stdlib.h> void exit(int status );
#include <unistd.h> void _exit(int status );
Man Pages:
- fork - create a child process