Perl Process Management - Perl Scripting

What is the process management? How to create and manage Perl processes?

The user can mould Perl in different ways to create new processes as per the user requirements. This tutorial will enumerate few important and most frequently used methods to create and manage Perl processes.
  • You can use special variables $$ or $PROCESS_ID to get current process ID.
  • Every process that is created by Perl using any of the above mentioned methods, maintains its own virtual environment with-in %ENV variable.
  • The exit() function always exits just the child process which executes this function and the main process will not exit unless all running child-processes have come to exit point.
  • All open handles are dup()-ed in child-processes, so that if any handles are need to close in one process does not affect the others.

Backstick Operator

This best way of executing any Unix command in Perl is by using backstick operator. You simply need to insert your command inside the backstick operator, which will result in execution of the command and returns its output which can be stored as follows −
When the above code is executed, it lists down all the files and directories available in the current directory −

The system() Function

The user can also use the system() function to execute any Unix command, whose output will return to the output of the perl script. By default, it is the screen, i.e., STDOUT, but you can redirect it to any file by using redirection operator > −
When above code is executed, it displays all the files and directories available in the current directory −
START##
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
drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy
Be careful when your command contains shell environmental variables like $PATH or $HOME. Try following three scenarios −
When above code is executed, it produces the following result depending on what is set in shell variable $PATH.

The fork() Function

Perl provides a fork() function that relates to the Unix system call of the same name. On most of the Unix-like platforms where the fork() system call is available, Perl's fork() simply calls it. On some platforms such as Windows where the fork() system call is not available, then Perl can built to emulate fork() at the interpreter level.
The fork() function is used to clone a current process. This call creates a new process running the same program at the same point. It returns the child pid to the parent process, 0 to the child process. If the fork is meant to unsuccessful then it undefines the function. You can use exec() function within a process to launch the requested executable, which will be executed in a separate process area and exec() will wait for it to complete the process before exiting with the same exit status.
When above code is executed, it produces the following result −
The wait() and waitpid() are the two calls that can be passed as a pseudo-process ID returned by fork(). These calls will probably wait until the termination of the pseudo-process comes and return its status. If you fork without ever waiting on your children using waitpid() function, you will get accumulated on zombies. On Unix systems, you can avoid this by setting $SIG{CHLD} to "IGNORE" as follows −
When above code is executed, it produces the following result −

The kill() Function

Perl kill('KILL', (Process List)) function can be used to terminate or kill a pseudo-process by passing it the ID returned by fork().
Note that using kill('KILL', (Process List)) on a pseudo-process() may lead to cause of memory leaks, because the thread that implements the pseudo-process does not get a chance to clean up its resources.
You can use kill() function to send any other signal to target processes, for example following will send SIGINT to a process IDs 104 and 102 −

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Perl Scripting Topics