5.46. Perl process management

发布时间 : 2025-10-25 13:33:03 UTC      

Page Views: 9 views

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_ID to get the process ID.

  • The %ENV hash stores the parent process, that is, shell environmentvariables, 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 $PATH or $HOME , as shown below

Example #

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

Execute the above procedure, and the output is as follows:

/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.

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; 

Execute the above procedure, and the output is as follows:

Output through parent process Output through child processes Sunday, June 19, 2016 22:21:14 CST Completed Process ID: 47117 

If the process exits, a 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; 

Execute the above procedure, and the output is as follows:

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; 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。