You can create processes in different ways in Perl.
This tutorial will discuss some methods of managing processes.
You can get the process ID using the special variable $$ or $PROCESS_ID .
The %ENV hash stores the environment variables in the parent process, which is the shell. These variables can be modified in Perl.
exit() is usually used to exit a child process. The main process exits after all child processes have exited.
All open handles will be copied by the dup() function in the subroutine, and all handles closed by the process will not affect other processes.
Unix commands can be easily executed using the backtick operator. You can insert some simple commands in backticks. After the command is executed, the results will be returned:
#!/usr/bin/perl@files = `ls -l`;foreach $file (@files){ print $file;}1;
Executing the above program, the output results are as follows:
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14drwxr-xr-x 4 root root 4096 Sep 13 07:54 android-rw-r--r-- 1 root root 574 Sep 17 15:16 index .htmdrwxr-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 the system() function to execute Unix commands. Executing this command will output the results directly. By default, it will be sent to the place where Perl's STDOUT currently points, which is usually the screen. You can also use the redirection operator > to output to a specific file:
Executing the above program, the output results are as follows:
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14drwxr-xr-x 4 root root 4096 Sep 13 07:54 android-rw-r--r-- 1 root root 574 Sep 17 15:16 index .htmdrwxr-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 the command including environment variables such as $PATH or $HOME, as shown below:
Executing the above program, the output results are as follows:
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbinI am the Perl variable /usr/local/bin:/bin:/usr/bin:/ usr/local/sbin:/usr/sbin:/sbin
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, insufficient memory), undef is returned and $! is set to the corresponding error message.
fork can be used in conjunction with exec. The process ends after the exec function executes the command in quotes.
Executing the above program, the output results are as follows:
Output via parent process Output via child process Process ID completed on Sunday 19 June 2016 22:21:14 CST Process ID: 47117
If the process exits, it will send a CHLD signal to the parent process and it will become a zombie process. The parent process needs to use wait and waitpid to terminate it. Of course, you can also set $SIG{CHLD} to IGNORG:
Executing the above program, the output results are as follows:
Output via parent process Output via child process Process ID completed on Sunday, June 19, 2016 22:30:56 CST Process ID: -1
Perl kill('signal', (Process List)) sends a signal to a group of processes. signal is the digital signal sent, 9 is to kill the process.
First, let’s take a look at the commonly used signals in Linux, see the following list:
Explanation of signal name and value annotation———————————————————————————————— ——————————————HUP 1 A Hang detected INT 2 A Interrupt from keyboard QUIT 3 A Stop from keyboard ILL 4 A Illegal instruction ABRT 6 C Failure FPE 8 C Floating point Abnormal KILL 9 AF terminal signal USR1 10 A User-defined signal 1SEGV 11 C Illegal memory access USR2 12 A User-defined signal 2PIPE 13 A Write to pipe ALRM with no reader 14 A Timer signal TERM from alarm clock 15 A Terminal signal CHLD 17 B Child process termination CONT 18 E Continue STOP if stopped 19 DF Stop the process TSTP 20 D Type the stop command TTIN on tty 21 D Type TTOU on the tty of the background process 22 D tty output to background process
The following example sends SIGINT signals to processes 104 and 102: