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_IDto get the process ID.

  • The %ENV hash stores the environment variables of the parent process, that is, the environment variables in the shell. These variables can be modified in Perl.

  • exit()Usually used to exit child processes; the main process exits after all child processes have exited.

  • All open handles are duplicated via the dup() function in the child process, and closing any handles in one process does not affect other processes.


Backtick Operator

You can easily execute Unix commands using the backtick operator. You can insert some simple commands within backticks. After the command executes, the result is returned:

#!/usr/bin/perl

@files = `ls -l`;

foreach $file (@files){
   print $file;
}

1;

Execute the above program, the output is as follows:

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 thesystem()function to execute Unix commands. Executing the command will directly output the result. By default, it is sent to wherever Perl's STDOUT currently points, usually the screen. You can also use the redirection operator > to output to a specified file:

Execute the above program, the output is as follows:

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 pay attention to the output results when commands contain environment variables such as $PATH or $HOME, as shown below:

Example

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

Execute the above program, the output is as follows:

/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
我是 Perl 的变量
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin

fork() Function

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

In the parent process it returns the PID of the child process; 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 together with exec. The exec function terminates the process after executing the command in the quotes.

Example

#!/usr/bin/perl if(!defined($pid = fork())) { # fork returns undef on error die "Unable to create child process:$!"; }elsif ($pid == 0) { print "Output from child process\n"; exec("date") || die "Unable to output date:$!"; } else { # In the parent process print "Output from parent process\n"; $ret = waitpid($pid, 0); print "Completed process ID:$ret\n"; } 1;

Execute the above program, the output is as follows:

通过父进程输出
通过子进程输出
2016年 6月19日 星期日 22时21分14秒 CST
完成的进程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 IGNORE:

Example

#!/usr/bin/perl local $SIG{CHLD} = "IGNORE"; if(!defined($pid = fork())) { # fork returns undef on error die "Unable to create child process:$!"; }elsif ($pid == 0) { print "Output from child process\n"; exec("date") || die "Unable to output date:$!"; } else { # In the parent process print "Output from parent process\n"; $ret = waitpid($pid, 0); print "Completed process ID:$ret\n"; } 1;

Execute the above program, the output is as follows:

通过父进程输出
通过子进程输出
2016年 6月19日 星期日 22时30分56秒 CST
完成的进程ID: -1

Kill Function

Perl kill('signal', (Process List))Sends a signal to a group of processes. signal is the numeric signal to send; 9 kills the process.

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

信号名          值          标注          解释
————————————————————————————————————————————————————————————
HUP             1           A             检测到挂起
INT               2           A             来自键盘的中断
QUIT            3           A             来自键盘的停止
ILL               4           A             非法指令
ABRT          6           C             失败
FPE             8           C             浮点异常
KILL             9           AF            终端信号
USR1          10          A             用户定义的信号1
SEGV          11          C             非法内存访问
USR2          12          A             用户定义的信号2
PIPE           13          A             写往没有读取者的管道
ALRM         14          A             来自闹钟的定时器信号
TERM         15          A             终端信号
CHLD          17          B             子进程终止
CONT         18          E             如果被停止则继续
STOP         19          DF            停止进程
TSTP          20          D             tty键入的停止命令
TTIN            21          D             对后台进程的tty输入
TTOU          22          D             对后台进程的tty输出

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

Example

#!/usr/bin/perl kill('INT', 104, 102); 1;
Other extensions