0% found this document useful (0 votes)
198 views4 pages

Understanding fork() in Linux Processes

The fork() system call in Linux and Unix creates a new child process that runs concurrently with the parent process, returning different values based on the success of the operation. The wait() system call blocks the parent process until a child process exits, while the execv system call replaces the current process image with a new one. The document includes examples in C demonstrating the use of fork() and the resulting process creation hierarchy.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views4 pages

Understanding fork() in Linux Processes

The fork() system call in Linux and Unix creates a new child process that runs concurrently with the parent process, returning different values based on the success of the operation. The wait() system call blocks the parent process until a child process exits, while the execv system call replaces the current process image with a new one. The document includes examples in C demonstrating the use of fork() and the resulting process creation hierarchy.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

fork() system call



The Fork system call is used for creating a new process in Linux, and
Unix systems, which is called the child process, which runs
concurrently with the process that makes the fork() call (parent
process). After a new child process is created, both processes will
execute the next instruction following the fork() system call.
The child process uses the same pc(program counter), same CPU
registers, and same open files which use in the parent process. It
takes no parameters and returns an integer value.
Below are different values returned by fork().
 Negative Value: The creation of a child process was
unsuccessful.
 Zero: Returned to the newly created child process.
 Positive value: Returned to parent or caller. The value
contains the process ID of the newly created child process.

Note: fork() is threading based function, to get the correct output


run the program on a local system.
Please note that the above programs don’t compile in a
Windows environment.
Example of fork() in C
 C

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{

// make two process which run same


// program after this instruction
pid_t p = fork();
if(p<0){
perror("fork fail");
exit(1);
}
printf("Hello world!, process_id(pid) = %d \
n",getpid());
return 0;
}

Output
Hello world!, process_id(pid) = 31
Hello world!, process_id(pid) = 32

Example 2: Calculate the number of times hello is


printed.
 C

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}

Output
hello
hello
hello
hello
hello
hello
hello
hello
Explanation
The number of times ‘hello’ is printed is equal to the number of
processes created. Total Number of Processes = 2 n, where n is the
number of fork system calls. So here n = 3, 23 = 8 Let us put some
label names for the three lines:
fork (); // Line 1
fork (); // Line 2
fork (); // Line 3
L1 // There will be 1 child process
/ \ // created by line 1.
L2 L2 // There will be 2 child processes
/ \ / \ // created by line 2
L3 L3 L3 L3 // There will be 4 child processes
// created by line 3
So there is a total of eight processes (new child processes and one
original process). If we want to represent the relationship between
the processes as a tree hierarchy it would be the following: The main
process: P0 Processes created by the 1st fork: P1 Processes created
by the 2nd fork: P2, P3 Processes created by the 3rd fork: P4, P5, P6,
P7
P0
/ | \
P1 P4 P2
/ \ \
P3 P6 P5
/
P7

Wait System Call



A call to wait() blocks the calling process until one of its child
processes exits or a signal is received. After child process
terminates, parent continues its execution after wait system call
instruction.
Child process may terminate due to any of these:
 It calls exit();
 It returns (an int) from main
 It receives a signal (from the OS or another process) whose
default action is to terminate.

execv system call

The execv system call is a function in the C programming language that replaces the current
process image with a new process image. The new process image is loaded into the current
process space and begins execution from the entry point of the new program.
The execv system call is part of the exec family of functions, which are used to run a new
program in the current process space. The exec family of functions can be used to run any
program files, including binary executables or shell scripts 1

Common questions

Powered by AI

The primary difference between execv and fork() lies in their functions: fork() creates a new child process duplicating the current process context, whereas execv replaces the current process image with a new one, effectively loading a new program. fork() maintains the execution flow of both parent and child processes simultaneously, while execv stops the current execution by starting the new program .

After a fork() call, the child process proceeds to execute the instruction following the fork call independently of the parent process. Both processes share the same program counter, CPU registers, and open files at the moment of the fork. However, any changes in the process context after the fork will not be shared due to the isolation provided by separate process spaces .

In the fork() system call, process IDs are utilized to differentiate between the child and the parent processes. The returned positive integer to the parent process is the process ID of the new child process, allowing the parent to identify and track the child's lifecycle. The child process receives a zero, thus recognizing itself as the child .

In the provided example, three fork() calls are made. Each fork() call doubles the number of processes, starting from the original one. The formula for the total number of processes created is 2^n, where n is the number of fork calls. Thus, 2^3 = 8 processes are created. Each process prints 'hello', resulting in eight lines of output .

A child process can terminate under several conditions such as: calling exit(), returning an integer from the main function, or receiving a signal from the operating system or another process whose default action is to terminate the process .

A fork() call is considered unsuccessful when it returns a negative value. This indicates that the system was unable to create a new child process, potentially due to resource limitations such as exceeding process number limits. It has significant implications for process management as it necessitates handling these errors to prevent invalid process operations and manage resources efficiently .

Running fork() examples on a local system, specifically Linux or Unix-based systems, is crucial as fork is inherently threading and process management based, which these systems support. In contrast, Windows does not natively support the fork() system call, leading to incompatibility and potential failure in execution if attempted in such an environment .

The fork() system call can return three distinct outcomes: a negative value, zero, and a positive value. A negative value indicates that the creation of a child process was unsuccessful, zero is returned to the newly created child process, and a positive value, which contains the process ID of the newly created child process, is returned to the parent process .

The total number of processes generated by n fork() calls is calculated as 2^n, where each fork() call duplicates all current processes. This exponential growth occurs because each existing process creates an additional child process for each fork call, effectively doubling the current number of processes at every step .

The wait() system call plays a critical role in process management by blocking the calling process until one of its child processes exits or a signal is received. This ensures that resources used by the child process are released and that the parent can accurately monitor the lifecycle of its child processes .

You might also like