Cyberithub

Quick Tutorial: Process Management in Perl(v5)

Advertisements

In this article, I will go through the concept of Process Management in Perl.

Process Management in Perl

Process management in Perl is basically a way for the programs to ‘talk’ to each other, to keep track of what another process is doing and manage resources efficiently.

What is Perl

Abbreviated as Practical Extraction and Report Language, Perl is a programming language developed by Larry Wall, especially designed for processing strings. Because of its strong text processing abilities, Perl has become one of the most popular languages for writing CGI scripts. Perl is an interpretive language, which makes it easy to build and test simple programs.

Also Read: How to install Perl on RedHat/CentOS 7

What is Process

A process is basically a program in execution. The execution of a process must progress in a certain manner.
It is defined as an entity which represents the basic unit of work to be implemented in the system. To put it in simple terms, we write our programs in a text file and when we execute this program, it becomes a process which performs all the tasks mentioned in the program. These processes needs to talk to each other for utilization of the resources hence we require process management in Perl.

Quick Tutorial: Process Management in Perl(v5) 1

System Function

The easiest way to launch a child process in Perl to run a program is the system function. This function creates a new process, runs a program in it and waits for its completion. It returns the completion Status of the program.

For example, to invoke the Linux date command within Perl, it should be done like this:

system "date";

The child process runs the date command, which inherits Perl’s standard input, standard output, and the standard error.

Syntax

Following is the simple syntax for this function −

system PROGRAM, LIST
system PROGRAM

Example

[root@localhost ~]# vi example.pl
#!/usr/bin/perl -w

system("uname -a");

Output

[root@localhost ~]# perl example.pl
Linux localhost 3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Fork() Function

fork() system call to create a new process running the same program at the same point. It returns the child pid to the parent process, 0 to the child process, or undef if the fork is unsuccessful. File descriptors (and sometimes locks on those descriptors) are shared, while everything else is copied. $$ will have the current script process Id.

Example

[root@localhost ~]# vi example.pl
#/usr/bin/perl -w
use strict;
use warnings;
print "Current Process ID is:$$\n";
my $proc = fork();
if( $proc == 0)
{
print "Child Process ID is $proc\n";
}
else
{
print "Parent Process ID is $proc\n";
}

Output

[root@localhost ~]# perl example.pl
Current Process ID is:13623
Parent Process ID is 13624
Child Process ID is 0

Sleep() function

sleep() function in Perl is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds or forever if parameter is not specified. The sleep( ) function accepts seconds as a parameter and returns the same on success.

Syntax: sleep(seconds)

Returns: seconds passed to it as parameter, on success

Example

[root@localhost ~]# vi example.pl
#/usr/bin/perl -w
use strict;
use warnings;
print scalar localtime();
print "\n";
sleep(7);
print "Process sleeping for 7 seconds\n";
print scalar localtime();
print "\n";

Output

You can see from the output that process has slept for 7 seconds and then came back up after 7 seconds.

[root@localhost perl]# perl example.pl
Mon Dec 16 11:40:16 2019
Process sleeping for 7 seconds
Mon Dec 16 11:40:23 2019

Terminating Running Processes

As we can execute commands and start programs from Perl scripts, we can also terminate them on need. If you are a UNIX/Linux guy, you must be certainly aware of the the kill command. Perl has a built-in function having the same name and job as well, which is killing a process.

Syntax

kill(SIGNAL,@PEOCESSLIST);
kill(SIGNAL,PROCESSID1,PROCESSID2,..,PROCESSIDn);

Top Command 

Verfiy the running httpd process through top command as mentioned below.

[root@localhost ~]# ps -ef | grep -i httpd | grep -v grep
root 2537 1 0 12:35 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2538 2537 0 12:35 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2539 2537 0 12:35 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2540 2537 0 12:35 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2541 2537 0 12:35 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 2542 2537 0 12:35 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND

Total number of Processes

You can get the total number of running httpd process from below command.

[root@localhost ~]# ps -ef | grep -i httpd | grep -v grep | wc -l
6

Example

#!/usr/bin/perl
@PID_list = ();
@processes = `ps -ef`;
foreach $line (@processes)
{
if($line =~ /httpd/)
{
@list = split(/\s+/,$line);
push(@PID_list, $list[1]);
}
}
kill(9,@PID_list) if(scalar(@PID_list) > 0);
print (scalar(@PID_list), " Total processes killed\n");

Output

[root@localhost ~]# perl example.pl
6 processes killed
[root@localhost ~]# ps -ef | grep -i httpd | grep -v grep
[root@localhost ~]# ps -ef | grep -i httpd | grep -v grep | wc -l
0

Reference: Perl Documentation

Leave a Comment