Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Process Management

Perl Process Management

In Perl you can create processes in different ways.

This tutorial discusses some methods of process management.


The backtick operator

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



#!/usr/bin/perl



@files = `ls -l`;



foreach $file (@files){

   print $file;

}



1;

Running the above program produces 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

……


The system() function

You can also use the system() The function executes a Unix command, and executing the command outputs the result directly. By default it goes to wherever Perl's STDOUT currently points, usually the screen. You can also use the redirection operator > to output to a specified file:

Running the above program produces 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 pay attention to the output of commands that 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

"

)

;

Escaping $

1

;

Running the above program produces the following output:



/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


The fork() function

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

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

fork can be used together with exec. After the exec function executes the command in the quotes, the process ends.

Example

#!/usr/bin/perl

if

(

!

defined

(

$pid

=

fork

(

)

)

)

{

If fork fails, it returns undef

die

"

Unable to create child process:

$!

"

;

}

elsif

(

$pid

==

0

)

{

print

"

Output from the child process

\n

"

;

exec

(

"

date

"

)

||

die

"

Unable to output the date:

$!

"

;

}

else

{

In the parent process

print

"

Output from the parent process

\n

"

;

$ret

=

waitpid

(

$pid

,

0

)

;

print

"

Completed process ID:

$ret

\n

"

;

}

1

;

Running the above program produces the following output:



通过父进程输出

通过子进程输出

2016年 6月19日 星期日 22时21分14秒 CST

完成的进程ID: 47117

When a process exits, it sends a CHLD signal to its parent and becomes a zombie process, which the parent must terminate using wait or waitpid. Of course, you can also set $SIG{CHLD} to IGNORG:

Example

#!/usr/bin/perl

local

$SIG

{

CHLD

}

=

"

IGNORE

"

;

if

(

!

defined

(

$pid

=

fork

(

)

)

)

{

If fork fails, it returns undef

die

"

Unable to create child process:

$!

"

;

}

elsif

(

$pid

==

0

)

{

print

"

Output from the child process

\n

"

;

exec

(

"

date

"

)

||

die

"

Unable to output the date:

$!

"

;

}

else

{

In the parent process

print

"

Output from the parent process

\n

"

;

$ret

=

waitpid

(

$pid

,

0

)

;

print

"

Completed process ID:

$ret

\n

"

;

}

1

;

Running the above program produces the following output:



通过父进程输出

通过子进程输出

2016年 6月19日 星期日 22时30分56秒 CST

完成的进程ID: -1


The kill Function

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

First, let's look at the common signals in Linux, as listed 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 sends a SIGINT signal to processes 104 and 102:

Example

#!/usr/bin/perl

kill

(

'

INT

'

,

104

,

102

)

;

1

;