Perl process management


You can create processes in different ways in Perl.

This tutorial will discuss some process management methods.

  • You can use the special variable $$ or $PROCESS_ID to get the process ID.

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

  • exit() is usually used to exit a child process. The main process exits after all child processes have exited.

  • All open handles will be copied by the dup() function in the subroutine, and closing all handles of the process will not affect other processes.


Backtick operator

Using the backtick operator makes it easy to execute Unix commands. You can insert some simple commands in backticks. After the command is executed, the result will be returned:

#!/usr/bin/perl

@files = `ls -l`;

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

1;

Execute the above program, the output result 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 system () The function executes a Unix command, and executing the command will directly output the result. By default, it will be sent to the place where Perl's STDOUT currently points, which is usually the screen. You can also use the redirection operator> to output to a specified file:

Execute the above program, the output result 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 note that the command contains environment variables such as $PATH or $HOME The output result is as follows:

#!/usr/bin/perl

$PATH = "我是 Perl 的变量";

system('echo $PATH');  # $PATH 作为 shell 环境变量
system("echo $PATH");  # $PATH 作为 Perl 的变量
system("echo $PATH"); # 转义 $

1;

Execute the above program and the output result 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 fork() function is used to create a new process.

Returns the PID of the child process in the parent process, and returns 0 in the child process. If an error occurs (for example, insufficient memory), undef is returned and $! is set to the corresponding error message.

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

#!/usr/bin/perl

if(!defined($pid = fork())) {
   # fork 发生错误返回 undef
   die "无法创建子进程: $!";
}elsif ($pid == 0) {
   print "通过子进程输出\n";
   exec("date") || die "无法输出日期: $!";
  
} else {
   # 在父进程中
   print "通过父进程输出\n";
   $ret = waitpid($pid, 0);
   print "完成的进程ID: $ret\n";

}

1;

Execute the above program, the output result is as follows:

通过父进程输出
通过子进程输出
2016年 6月19日 星期日 22时21分14秒 CST
完成的进程ID: 47117

If the process exits, it will send a CHLD signal to the parent process, and it will become a zombie process, which needs to be used by the parent process. wait and waitpid to terminate. Of course, you can also set $SIG{CHLD} to IGNORG:

#!/usr/bin/perl

local $SIG{CHLD} = "IGNORE";
 
if(!defined($pid = fork())) {
   # fork 发生错误返回 undef
   die "无法创建子进程: $!";
}elsif ($pid == 0) {
   print "通过子进程输出\n";
   exec("date") || die "无法输出日期: $!";
  
} else {
   # 在父进程中
   print "通过父进程输出\n";
   $ret = waitpid($pid, 0);
   print "完成的进程ID: $ret\n";

}

1;

Execute the above program, the output result is as follows:

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

Kill function

Perl kill('signal', (Process List))Send a signal to a group of processes. signal is the digital signal sent, 9 is to kill the process.

First take a look at the commonly used signals in Linux, see 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 examples send SIGINT signals to processes 104 and 102:

#!/usr/bin/perl

kill('INT', 104, 102);
 
1;