Perl process management

In Perl you can create processes in different ways.

This tutorial discusses how to manage some processes.

  • You can use the $$ $PROCESS to get the process ID.

  • The %ENV hash holds the parent process, the environment variables in the shell, which can be modified in Perl.

  • Exit() is typically used to exit a sub-process, and the main process exits after all the sub-processes have exited.

  • All open handles are copied by the dup() function in the subprother, and all handles to all closed processes do not affect other processes.


The back quote operator

Unix commands can be easily executed using the back quote operator. Y ou can insert some simple commands in back quotes. When the command is executed, the results are returned:

#!/usr/bin/perl

@files = `ls -l`;

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

1;

To execute the above procedure, 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 the system() function to execute the Unix command, which outputs the results directly. B y default, it is sent to where the current Perl STDOUT points, usually the screen. You can also use the redirect operator to output to the specified file:

To execute the above procedure, 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 be aware that the command contains the output $PATH environment variables $HOME or the data, as follows:

#!/usr/bin/perl

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

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

1;

To execute the above procedure, 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

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

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

Fork can be used with exec. The exec function executes the command in quotation marks and the process ends.

#!/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;

To execute the above procedure, the output is as follows:

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

If a process exits, a CHLD signal is sent to the parent process, which becomes a deadlocked process that requires the parent process to terminate with wait and waitpid. 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;

To execute the above procedure, 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 set of processes. Signal is the digital signal sent, and 9 is the kill process.

First look at the common 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 instances send SIGINT signals to processes 104 and 102:

#!/usr/bin/perl

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