Syntax:
The syntax of the fork() function is given in the following. It returns a numeric value or “undef”. If the fork() function works successfully, a particular process ID is returned or 0 is returned for the child process. It returns “undef” if the fork() function is unsuccessful.
Example 1: Simple Use of the Fork() Function
Create a Perl file with the following script that creates a child process from the current process using the fork() function. The process ID is printed after executing the fork() function. The variables of the parent and the child processes are stored in different memory locations. So, if the value of the variable is changed in the parent process, it will not make any change for the same variable of the child process or vice versa. In this script, the variable of the parent process is changed. Then, the variables of both parent and child processes are printed.
use strict;
use warnings;
use 5.34.0;
#Declare a variable
my $var = "Test Fork";
#Fork the current process
my $process_id = fork();
#Check the process id
if($process_id < 0)
{
say "Unable to fork the process.";
}
elsif ($process_id == 0)
{
say "Child Process:";
#Print the value of the variable of the child process
say "The value of the variable: $var";
}
else
{
say "Parent Process: $process_id";
#Update the value of the variable
$var = "Test Fork 2";
#Print the value of the variable of the parent process
say "The value of the variable: $var";
}
Output:
The following output appears after executing the script. The variable of the parent process is modified with the “Test Fork 2” value but the variable of the child process remains unchanged. So, the value of the variable of the child process is “Test Fork”. That was the value after the fork:
Example 2: Using the Fork() and Wait() Functions
Create a Perl file with the following script that shows the use of the fork() function with the wait() function. Three variables are declared in the beginning of the script. The sleep() function is used inside the block of the parent process for 1 second and inside the child process for 2 seconds. The sum of the two variables is calculated and stored on another variable inside the block of the parent process. The values of the two variables are multiplied and stored on another variable inside the child process. The wait() function is used at the end of the script to wait for completing the task of one process before starting the task of another process. If the fork() function is used multiple times in the script and many processes are running at the same time, a deadlock situation may arise. The problem can be solved using the wait() function.
use strict;
use warnings;
use 5.34.0;
#Initialize the variables
my $n1 = 10;
my $n2 = 25;
my $result = 0;
#Fork the current process
my $process_id = fork();
#Check the process id
if ($process_id == 0)
{
#Calculate the multiplication of the numbers
say "Child Process: $$";
$result = $n1 * $n2;
say "Wait for 2 seconds...\n";
sleep 2;
say "The multiplication of $n1 and $n2 is $result.";
}
elsif ($process_id > 0)
{
#Calculate the sum of the numbers
say "Parent Process: $$";
$result = $n1 + $n2;
say "Wait for 1 second...\n";
sleep 1;
say "The sum of $n1 and $n2 is $result.";
}
else
{
say "Unable to fork the process.";
}
#Wait to complete the process task
wait();
Output:
The following output appears after executing the script. According to the output, the child process is created successfully, the process ID of the parent process is printed, and the script is paused for 1 second to complete the task. Next, the process ID of the child process is printed and the script is paused for 2 seconds to complete the task. The output of both parent and child processes is printed later:
Conclusion
The purpose of using the Perl fork() function is shown in this tutorial by creating a child process and performing different types of tasks inside the block of the parent and the child processes.