home PYTHONJAVA
 

Perl Process Management

In Perl you can create processes in different ways.

This tutorial will discuss some of the process management methods.

  • You can use the special variable $$ or $PROCESS_ID to get the process ID.

  • %ENV The hash stores the parent process, which is the environment variable in the shell. These variables can be modified in Perl.

  • exit() is usually used to exit a child process, and the main process exits after the child process has all exited.

  • All open handles are copied by the dup() function in the subroutine, and all handles to the close process do not affect other processes.



Backquote operator

Using the backquote operator makes it easy to execute Unix commands. You can insert some simple commands in backticks. The result will be returned after the command is executed:

#!/usr/canister/perl 

@documents = 'ls - l'; 

foreach $report (@files) 

print $file; 

 

1;

Execute the above program, the yield 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 three 544 401 4096 Jul 6 sixteen:forty nine MIME-Lite-3.01
-rw-r--r-- 1 root root seventy one Sep 17 15:16  take a look at.pl
...

machine() characteristic

You also can use the system() characteristic to execute Unix instructions, so as to output the effects immediately. By default it will likely be despatched to the modern factor of Perl's STDOUT, generally the display screen. You also can use the redirection operator > to output to the specified record:

Execute the above program, the output is as follows:

drwxr-xr-x three root Root 4096 Sep 14 06:46 nine-14
Drwxr-xr-x 4 root root 4096 Sep 13 07:fifty four android
-rw-r--r-- 1 root root 574 Sep 17 15:16  index.htm
Drwxr-xr-x 3 544 401 4096 Jul 6 16:forty nine MIME-Lite-3.01
-rw-r--r-- 1 root root seventy one Sep 17 15:sixteen  check.pl
...

You need to be aware that the command consists of output consequences for surroundings variables along with $PATH or $HOME as follows:

Instance

#!/usr/container/perl $PATH= " I am a variable in Perl"; machine('echo $PATH'); # $PATH as a shell domain variable gadget("echo $PATH"); # $PATH as a variable for Perl machine("echo $PATH"); #Escape$ 1;

Execute the above programming, the yield is as follows:

/usr/local/bin:/container:/usr/bin:/usr/neighborhood/sbin:/usr/sbin:/sbin 

I am Perl variables 

/usr/nearby/bin:/receptacle:/usr/bin :/usr/local/sbin:/usr/sbin:/sbin

fork() characteristic

The Perl fork() include is utilized to make another strategy.

Returns the PID of the child procedure inside the decide framework, and returns zero in the youngster methodology. On the off chance that a mix-ups occurs (for instance, lacking memory), return undef and set $! To the relating mistakes message.

fork can be utilized with executive. The way closes while the executive capacity executes the order in rates.

Instance

#!/usr/bin/perl if(!described($pid = fork( ))) # fork Error returning undef die "Unable to create toddler process: $!"; elsif ($pid == zero) print "Output through toddler process else # in the parent manner print "Output through determine techniquen"; $ret= waitpid ($pid, 0); print "Completed Process ID: $retn"; 1;

Execute the above program, the yield is as follows:

Output through parent system 

Output by means of child procedure 

2016year  6month19day Sunday 22Time21Minute 14seconds CST 

Completed ProcessID: 47117

If the technique leaves, it will send a CHLD flag to the decide strategy, it will end up being a zombie framework, the decide system wants to apply pause and waitpid to end. Of way, you could also set $SIGCHLD to IGNORG:

Instance

#!/usr/bin/perl local $SIGCHLD = "IGNORE" ; if(!defined($pid = fork( ))) # fork Error returning undef die "Unable to create child technique: $!"; elsif ($pid == zero) print "Output via baby techniquen"; exec("date") else # inside the figure manner print "Output through discern proceduren"; $ret= waitpid ($pid, zero); print "Completed Process ID: $retn"; 1;

Execute the above application, the output is as follows:

Output via figure system
Output thru baby technique
201612 months  6month19day Sunday 22Time30Minute fifty sixseconds CST
Completed ProcessID: -1

Kill function

Perl kill('sign', (Process List)) sends a signal to a collection of approaches. Signal is the transmitted digital signal, 9 is the kill method.

First observe the common signals in Linux, see the following list:

sign call cost label interpretation
———————————————————————————————————— ——————————————————————
HUP 1 A detected hangs
INT 2 A interruption from the keyboard
QUIT 3 A stop from the keyboard
ILL four A Illegal Instructions
ABRT 6 C failed
FPE eight C floating point exception
KILL 9 AF terminal signal
USR1 10 A consumer-defined signal1
SEGV eleven C Illegal reminiscence get right of entry to
USR2 12 A consumer-defined signal2
PIPE thirteen A write to a pipe without a reader
ALRM 14 A timer sign from the alarm
TERM 15 A terminal sign
CHLD 17 B subprocess termination
CONT 18 E Continue if stopped
STOP 19 DF stop the technique
TSTP 20 D ttytyped prevent command
TTIN 21 D on the history techniquettyEnter
TTOU 22 D on the history systemttyoutput

The following instance sends a SIGINT sign to strategies 104 and 102:

Instance

#!/usr/bin/perl kill('INT', 104, 102) ; 1;





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.