You can create processes in different ways in Perl.
This tutorial will discuss the management of some processes.
You can use special variables.
$$or$PROCESS_IDto get the process ID.The
%ENVhash stores the parent process, that is,shellenvironmentvariables, which can be modified in Perl.exit()is usually used to exit a child process, and the main process exits after all the child processes exit.All open handles will be used in the subroutine
dup()function copy, all closed processes all handles will not affect other processes.
5.46.1. Backquote operator #
It can be easily executed using the backquote operator
Unix
orders. Youcan insert some simple commands in backquotes. The result is returned afterthe command is executed:
#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files){
print $file;
}
1;
Execute the above procedure, and 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
……
5.46.2. System () function #
You can also use it.
system()
function executes the Unix command, whichoutputs the result directly. By default, it will be sent to the current Perl
STDOUT
. The place you point to is usually the screen. You can also use the redirect operator
>
export to the specified file:
Execute the above procedure, and 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 note that the command contains environment variables such as Execute the above procedure, and the output is as follows:
$PATH
or
$HOME
, as shown belowExample #
#/ Usr/bin/perl $PATH="I am a variable of Perl"; System ('echo $PATH '); #$ PATH
As a shell environment variable system ("echo $PATH"); #$ PATH as Perl
The variable system ("echo $PATH") of# Escape $1;
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
I am a variable of Perl
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
5.46.3. Fork () function #
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. Return if an error occurs (for example, out of memory)
undef
and set the
$!
set to the corresponding error message.
Execute the above procedure, and the output is as follows: If the process exits, a Execute the above procedure, and the output is as follows:
fork
can and
exec
use it together. The process ends afterthe
exec
function executes the command in quotation marks.Example #
#/ Usr/bin/perlif (! Defined ($pid=fork())) {# fork error returned
Undefie "Unable to create child process: $!";} Elsif ($pid==0) {print "Output through
child processes n"; exec ("date") | | die "Unable to output date: $!";}else{#
Print 'output through parent process' in the parent process$
Ret=waitpid ($pid, 0); Print "Completed process ID: $ret n";} 1;
Output through parent process
Output through child processes
Sunday, June 19, 2016 22:21:14 CST
Completed Process ID: 47117
CHLD
after the signal, it becomes a dead process and needs to be used by the parent process
wait
and
waitpid
to stop. Of course, you can also set the
$SIG{CHLD}
is
IGNORG
:Example #
#/ Usr/bin/perlocal $SIG {CHLD}="IGNORE"; If (! Defined ($pid=fork()) {# fork
Error returned
Undefie "Unable to create child process: $!";} Elsif ($pid==0)
{print "Output through child processes n"; exec ("date") || die
"Unable to output date: $!";}else{#
Print 'output through parent process' in the parent process$
Ret=waitpid ($pid, 0); Print "Completed process ID: $ret n";} 1;
Output through parent process
Output through child processes
Sunday, June 19, 2016 22:30:56 CST
Completed process ID: -1
5.46.4. Kill function #
Perl
kill('signal',
(Process
List))
send a signal to a group of processes.
signal
is the digital signal sent, 9 to kill the process.
First, take a look at the common signals in linux, as shown in the following list:
Signal name Value Mark Explain
————————————————————————————————————————————————————————————
HUP 1 A Pending detected
INT 2 A Interrupt from keyboard
QUIT 3 A Stop from keyboard
ILL 4 A Illegal instructions
ABRT 6 C fail
FPE 8 C Floating-point exception
KILL 9 AF Terminal signal
USR1 10 A User defined signal 1
SEGV 11 C Illegal memory access
USR2 12 A User Defined Signal 2
PIPE 13 A Write to a pipeline without readers
ALRM 14 A Timer signal from alarm clock
TERM 15 A Terminal signal
CHLD 17 B Child process termination
CONT 18 E If stopped, continue
STOP 19 DF Stop process
TSTP 20 D Tty typed stop command
TTIN 21 D Tty input to backend processes
TTOU 22 D Tty output to backend processes
The following example is sent to processes 104 and 102
SIGINT
signal:Example #
#!/usr/bin/perlkill('INT',104,102);1;