Forks



fork: a system call that creates a new process under the UNIX operating system

Crucial warning: For CS330 Labs, you ABSOLUTELY MUST use a linux machine instead of hercules for any program containing a fork() system call. This requirement is due to the possibility of a fork bomb , which is a runaway process that creates too many other processes either directly or indirectly.
At present, the CC compiler does not run on linux. Use g++ instead. The g++ on linux is good.

For example, if a program contains a call to fork( ), the execution of the program results in the execution of two processes. One process is created to start executing the program. When the fork( ) system call is executed, another process is created. The original process is called the parent process and the second process is called the child process. The child process is an almost exact copy of the parent process. Both processes continue executing from the point where the fork( ) calls returns execution to the main program. Since UNIX is a time-shared operating system, the two processes can execute concurrently.

Click here to see a sample program testing some system calls

Click here to see a sample program without fork
Click here to see the output

Click here to see a sample program with fork that runs two processes forever
Click here to see the output

Click here to see a sample program with fork that prints 5 lines each from two processes
Click here to see the output

Some differences between the child and parent process are:

fork returns:

wait:

Suppose we want to have the parent wait for a signal from the child process:
Click here to see a sample program
Click here to see the output

execl:

Suppose we want the new process to do something quite different from the parent process, namely run a different program. The execl system call loads a new executable into memory and associates it with the current process. In other words, it changes things so that this process starts executing executable code from a different file.

Click here to see a sample program with fork/exec and wait that executes the system program named ls, using the executable file called /bin/ls and using one parameter "-l". The overall result is equivalent to doing: ls -l
Click here to see a sample program with fork/exec and wait
Click here to see the output

process control block (pcb): data structure in OS that represents one process

Fork Bombs

Click here to see a sample forkbomb program, restricted to n <= 4

Click here to see the output

To help understand the forkbomb, consider this series of simple programs, which create 2^0 = 1, 2^1 = 2, 2^2 = 4, and 2^3 = 8 because they have 0, 1, 2, and 3 fork system calls, respectively.

simplefork0.cpp - no fork system call

simplefork1.cpp - one fork system call

simplefork2.cpp - two fork system calls

simplefork3.cpp - three fork system calls

Click here to see the output for all these programs

Implementation of the fork() system call:




Table of Contents