HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Perl you can in different ways to create a process.
This tutorial will discuss the management of some processes.
You can use the special variable$$ or $ PROCESS_IDto get the process ID.
% ENV hash holds the parent process, which is the shell environment variables in Perl can modify these variables.
exit () is usually used to exit a child process, the child process exits the main process exits after all.
All open handles are dup in the subroutine () function to copy all close all handles processes will not affect other processes.
Using the backquote operator can easily execute Unix commands. You can insert some simple commands in the anti-quotes. Command execution will return results:
#!/usr/bin/perl @files = `ls -l`; foreach $file (@files){ print $file; } 1;
The above program, the output results are 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 can also use thesystem () function executes the Unix command, the command is executed directly output.By default, the current will be sent to Perl's STDOUT point where, typically screen. You can also use the redirection operator> output to the specified file:
The above program, the output results are 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 ……
Note that the command you need to include environment variables such as output or $ HOME $ PATH, as follows:
#!/usr/bin/perl $PATH = "我是 Perl 的变量"; system('echo $PATH'); # $PATH 作为 shell 环境变量 system("echo $PATH"); # $PATH 作为 Perl 的变量 system("echo \$PATH"); # 转义 $ 1;
The above program, the output results are 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
Perl fork () function is used to create a new process.
Returns the child in the parent process PID, in the child process returns 0. If an error (for example, out of memory) occurred returns undef, and $! Is set to the corresponding error message.
It can be used in conjunction with fork and exec. exec function execution process that is completed after the end of command within quotation marks.
#!/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;
The above program, the output results are as follows:
通过父进程输出 通过子进程输出 2016年 6月19日 星期日 22时21分14秒 CST 完成的进程ID: 47117
If the process exits, it sends a signal to the parent process after CHLD, it will become a dead process that requires a parent process wait and waitpid terminated. 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;
The above program, the output results are as follows:
通过父进程输出 通过子进程输出 2016年 6月19日 星期日 22时30分56秒 CST 完成的进程ID: -1
Perl kill ( 'signal', ( Process List)) sends a signal to a group of processes.signal is a digital signal sent 9 to kill the process.
First look at linux commonly used signal, see the list below:
信号名 值 标注 解释 ———————————————————————————————————————————————————————————— 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 processes 104 and 102 to send SIGINT signal:
#!/usr/bin/perl kill('INT', 104, 102); 1;