YouTip LogoYouTip

Perl Process Management

Perl Process Management

In Perl, you can create processes in different ways.

This tutorial will discuss some process management methods.

  • You can use the special variable $$ or $PROCESS_ID to get the process ID.
  • The %ENV hash stores the environment variables of the parent process, i.e., the shell. These variables can be modified in Perl.
  • exit() is typically used to exit a child process. The main process exits after all child processes have exited.
  • All open handles are duplicated in the child process by the dup() function. Closing all handles in a process does not affect other processes.

Backtick Operator

Using the backtick operator makes it easy to execute Unix commands. You can insert simple commands within backticks. After the command executes, it returns the result:

#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files) {
    print $file;
}
1;

Executing the above program yields the following output:

drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
……

system() Function

You can also use the system() function to execute Unix commands. Executing the command directly outputs the result. By default, the output goes to the current STDOUT of Perl, which is usually the screen. You can also use the redirection operator > to output to a specified file:

Executing the above program yields the following output:

drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
……

You need to be aware of the output of commands that contain environment variables like $PATH or $HOME, as shown below:

Example

#!/usr/bin/perl
$PATH = "I am a Perl variable";
system('echo $PATH');
system("echo $PATH");
system("echo $PATH");
1;

Executing the above program yields the following output:

/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
I am a Perl variable
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin

fork() Function

The Perl fork() function is used to create a new process.

In the parent process, it returns the child process's PID. In the child process, it returns 0. If an error occurs (e.g., insufficient memory), it returns undef and sets $! to the corresponding error message.

fork can be used in conjunction with exec. The exec function ends the process after executing the command in the quotes.

Example

#!/usr/bin/perl
if(!defined($pid = fork())){
    die "Cannot create child process: $!";
}elsif($pid == 0){
    print "Output from child processn";
    exec("date") || die "Cannot output date: $!";
}else{
    print "Output from parent processn";
    $ret = waitpid($pid, 0);
    print "Completed process ID: $retn";
}
1;

Executing the above program yields the following output:

Output from parent process
Output from child process
Sun Jun 19 22:21:14 CST 2016
Completed process ID: 47117

When a process exits, it sends a CHLD signal to the parent process and then becomes a zombie process. The parent process needs to use wait and waitpid to terminate it. Of course, you can also set $SIG{CHLD} to IGNORG:

Example

#!/usr/bin/perl
local $SIG{CHLD} = "IGNORE";
if(!defined($pid = fork())){
    die "Cannot create child process: $!";
}elsif($pid == 0){
    print "Output from child processn";
    exec("date") || die "Cannot output date: $!";
}else{
    print "Output from parent processn";
    $ret = waitpid($pid, 0);
    print "Completed process ID: $retn";
}
1;

Executing the above program yields the following output:

Output from parent process
Output from child process
Sun Jun 19 22:30:56 CST 2016
Completed process ID: -1

Kill Function

Perl's kill('signal', (Process List)) sends a signal to a group of processes. 'signal' is the numeric signal to send; 9 is for killing a process.

First, let's look at common signals in Linux, as shown in the following list:

Signal Name Value Standard Description
HUP 1 A Hangup detected
INT 2 A Interrupt from keyboard
QUIT 3 A Quit from keyboard
ILL 4 A Illegal instruction
ABRT 6 C Abort signal
FPE 8 C Floating point exception
KILL 9 AF Terminate signal
USR1 10 A User-defined signal 1
SEGV 11 C Invalid memory reference
USR2 12 A User-defined signal 2
PIPE 13 A Broken pipe: write to pipe with no readers
ALRM 14 A Timer signal from alarm
TERM 15 A Termination signal
CHLD 17 B Child stopped or terminated
CONT 18 E Continue if stopped
STOP 19 DF Stop process
TSTP 20 D Stop typed at tty
TTIN 21 D tty input for background process
TTOU 22 D tty output for background process

The following example sends a SIGINT signal to processes 104 and 102:

Example

#!/usr/bin/perl
kill('INT', 104, 102);
1;
← Docker Run CommandPerl Cgi Programming β†’