Perl

The Perl Fork Function

The fork() is a built-in function of Perl that is used to create a child process of any running process and it works on the Linux operating systems only. It creates a separate copy of the executing process and runs both processes in parallel. So, if the variable of the parent process is updated, it will not have any effect on the same variable of the child process or vice versa. The uses of the fork() function in Perl are shown in this tutorial.

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.

id or undef fork();

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.

#!/usr/bin/perl

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:

p1

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.

#!/usr/bin/perl

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:

p2

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.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.