There are times that you'll be writing an application that you want to go off and perform task "x" and AT THE SAME TIME have it perform task "y", co-ordinating task "y" with task "x". Threading (NOT the subject covered here) is one possible approach. And another possible approach is forking. How does that work?
Let's say that you want to write a process that receives requests from a number of different clients and handles them sequentially - but quite slowly. A print driver would be a good example ...
1. Set up the main (to be monitoring) process, open a port, wait for requests.
2. When a request is received, note you process ID (the $$ variable), set up a pipe
The effect of the fork is to split your single process into a parent and a child each of which has copies of all your variables and each of which can continue on its own way.
You recognise that you're in the child process because the fork function returns a false value, whereas in the parent it returns the child's process ID.
The parent can then resume its wait loop, and the child can process the data.
3. When the child wants to talk to the parent, or vice versa, it can write to the pipe. A pipe opened before a fork connects any writes on the parent to reads on the child, and any writes on the child to reads on the parent.
4. If the child or parent needs to communicate with the other asyncronously (i.e. when the other is NOT waiting for an input), you use a signal. Signals are sent by the alarmingly named kill
function, and collected by a sub that you've named in the %SIG hash in the receiver.
If you're writing a Perl program that forks, have a look too at the $| or autoflush special variable, and both of the select
functions that are built in - one to set the default channel, and the other to check whether there's any data in your read buffers without having to wait if there isn't. Note also the use of temporary files to transfer information, and perhaps look at shared memory.
There's a simple fork example
and also a more complex talker written in Perl
available on our site. Training (with practicals) is available if you're attending Perl on the Web
or Perl for larger projects
. (written 2006-02-13, updated 2006-06-05)
Associated topics are indexed underP201 - Perl - Introduction 
Are you learning Perl? Some more examples for you! - (2010-06-27) 
What is Perl? - (2010-06-15) 
Converting to Perl - the sort of programs you will write - (2009-03-08) 
Perl and Blackberries - (2008-10-23) 
Perl v PHP, choosing the right language - (2008-08-14) 
Q - Should I use Perl or Python? - (2008-07-23) 
The LAMP Cookbook - Linux, Apache, MySQL, PHP / Perl - (2006-11-13) 
Is Perl being replaced by PHP and Python? - (2006-08-27) 
How to debug a Perl program - (2006-06-04) 
Testing you Perl / PHP / MySQL / Tcl knowledge - (2006-04-19) 
Choosing the right language - (2006-03-01) 
Twice is a co-incidence and three times is a pattern - (2006-02-07) 
Learning to program in Perl or PHP - (2006-01-26) 
Central London Courses - Perl, PHP, Python, Tcl, MySQL - (2005-07-18) 
Programming languages - a comparison - (2005-05-20) 
Release numbers - (2004-08-23)P223 - Perl - Interprocess Communication 
Run other processes from within your Perl program - (2012-12-03) 
Handling binary data in Perl is easy! - (2011-08-30) 
What are .pid files? - (2010-10-23) 
Children, zombies, and reaping processes - (2010-10-23) 
Perl - doing several things at the same time - (2010-09-25) 
Multiple processes (forking) in Python - (2010-03-25) 
Perl Socket Programming Examples - (2008-12-02)P224 - Perl - Intersystem Communications 
Different perl examples - some corners I rarely explore - (2010-07-18) 
TCP v UDP / Client v Server - Python examples - (2010-03-25) 
Automated Browsing in Perl - (2009-09-11) 
Heartbeat script in Perl - (2007-02-09)
Some other Articles
Don't expose your regular expressionsOn being BritishTrain service from DecemberDesign your day with a walkPerl - multiprocess applicationsPHP - setting sort order with an associative arrayMP face to faceA fond memory of Sir FreddieIt costs nothing to say THANK YOUPerl/Tk real time display