
Lead Image © Carsten Reisinger, 123RF.com
Interprocess communication essentials in Perl
Connections
Anyone who has written a fairly complex script has had to deal with interprocess communication (IPC) at one time or another. In this article, I talk about the use and structure of IPC by reading and writing from parent to child and from child to parent processes, and I'll look at bi-directional communication to achieve both at the same time.
A process is a task given to the operating system that has its own execution resources, including its own allocation of CPU time. A process can interact with other processes in a number of different ways, as you'll see in this article.
The support for IPC in Perl is considerable. At its most basic is the use of backticks to perform system commands (e.g., unlink
). When you submit a command with backticks, a new child process is created. When the task the child process has been given is complete, the child process reports back to the parent process and the parent script continues.
Perl supplies the fork
function to start a new process after setting up a pipe with the pipe
command. Although you can use the open
function in lieu of the pipe
function, I'll stick to piping at first.
It should be noted that the use of fork
is entirely operating system dependant. It is readily available on Unix or Linux machines, but you'll run into problems on a Windows system because the command is not supported on that platform.
Signal Handling
On Linux, processes can communicate with each other with signals. Perl implements an easy mechanism for catching signals sent to your processes. All you do is connect a signal-handling subroutine to the signal's entry in a predefined %SIG hash. To set up a machination to catch an INT
signal, use:
$SIG{INT} = \&handler_sub;
Note that a hard reference to the handler_sub
subroutine
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
