Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Perl - multiprocess applications

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 then fork.

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 as below, or enter http://melksh.am/nnnn for individual articles
P201 - Perl - Introduction
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2812] What is Perl? - (2010-06-15)
  [2070] Converting to Perl - the sort of programs you will write - (2009-03-08)
  [1852] Perl and Blackberries - (2008-10-23)
  [1753] Perl v PHP, choosing the right language - (2008-08-14)
  [1717] Q - Should I use Perl or Python? - (2008-07-23)
  [924] The LAMP Cookbook - Linux, Apache, MySQL, PHP / Perl - (2006-11-13)
  [846] Is Perl being replaced by PHP and Python? - (2006-08-27)
  [743] How to debug a Perl program - (2006-06-04)
  [691] Testing you Perl / PHP / MySQL / Tcl knowledge - (2006-04-19)
  [629] Choosing the right language - (2006-03-01)
  [594] Twice is a co-incidence and three times is a pattern - (2006-02-07)
  [577] Learning to program in Perl or PHP - (2006-01-26)
  [382] Central London Courses - Perl, PHP, Python, Tcl, MySQL - (2005-07-18)
  [317] Programming languages - a comparison - (2005-05-20)
  [25] Release numbers - (2004-08-23)

P223 - Perl - Interprocess Communication
  [3940] Run other processes from within your Perl program - (2012-12-03)
  [3412] Handling binary data in Perl is easy! - (2011-08-30)
  [3011] What are .pid files? - (2010-10-23)
  [3010] Children, zombies, and reaping processes - (2010-10-23)
  [2970] Perl - doing several things at the same time - (2010-09-25)
  [2694] Multiple processes (forking) in Python - (2010-03-25)
  [1918] Perl Socket Programming Examples - (2008-12-02)

P224 - Perl - Intersystem Communications
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2695] TCP v UDP / Client v Server - Python examples - (2010-03-25)
  [2402] Automated Browsing in Perl - (2009-09-11)
  [1073] Heartbeat script in Perl - (2007-02-09)


Back to
PHP - setting sort order with an associative array
Previous and next
or
Horse's mouth home
Forward to
Design your day with a walk
Some other Articles
Don't expose your regular expressions
On being British
Train service from December
Design your day with a walk
Perl - multiprocess applications
PHP - setting sort order with an associative array
MP face to face
A fond memory of Sir Freddie
It costs nothing to say THANK YOU
Perl/Tk real time display
4280 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/604_Perl ... tions.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat