Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Perl - doing several things at the same time

What if you want a Perl process to go off in two different directions at the same time - perhaps a server that's going to handle several connections at the same time, or an application that's going to be doing heavy processing, but at the same time has to respond to inputs from elsewhere, such as the keyboard?

• You can have a process "fork" - divide into two processes. At the point of forking, each process will be identical except that the original ("parent") will get back the child's process number from the fork call, and the child will get a zero back; based on the one difference, the code that's run can (and usually will) diverge.

• You can start another process on a pipe - opening a file handle and reading from it or writing too it from the second process, with a buffer of 4k between the processes so that they can run asyncronously.

• You can talk to another process via a network connection - typically a TCP / IP connection. If you instigate the process, you'll typically be the "client" and you'll connect to a server that's already running - perhaps on your machine or perhaps on another system to provide a specific service.

Once you've started a second process running in one of these ways ... how do you communicate with it? On some occasions, you might not need to - you can fire off a process and leave it to its own devices as perhaps it sends an email, prints a report, runs a tidy up of the disc, and then exits. But that's not universal ...

• Before you fork, you can set up a pipe with the pipe function which (after the fork) will have connected the write file handle on the parent to the read handle on the child, and the read handle on the parent to the write handle on the child

• If you prefer (or also need) asyncronous communication between the processes, you can use a signal. A signal is a sent by the frighteningly names kill function of command, and does no more that presses a button on the receiving process to alert it to the fact that someone else needs attention. So it's then up to the receiving process, at an appropriate opportunity, to look around and do what's needed

• With network connections (TCP/IP) and also pipes to other commands, there are intrinisic read and write channels set up for communication, and it's vital that both parties to the conversation understand the protocol - otherwise you'll end up with lockups where one end is waiting for a message that never comes.

We've got all sorts of examples of these on our web site ...
[link] - forking, then the processes talk through a pipe
[link] - forking with a loop, where the parent process starts a whole school of children
[link] - two way comminucation between parent and child
[link] - A process than contacts multiple servers (tcp/ip)
[link] - simple time server
(that's just a selection - follow the links above and you'll be offered more similar examples as well!)

Threading, where you've got several things happening within the same process, is a story for another day!
(written 2010-09-25)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
  [2694] Multiple processes (forking) in Python - (2010-03-25)
  [1918] Perl Socket Programming Examples - (2008-12-02)
  [604] Perl - multiprocess applications - (2006-02-13)

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
What does blessing a variable in Perl mean?
Previous and next
or
Horse's mouth home
Forward to
Should the public sector compete with businesses? and other deep questions
Some other Articles
Formatting your output - options available in Ruby
Learning to program - where to start if you have never programmed before
Some more advanced Perl examples from a recent course
Should the public sector compete with businesses? and other deep questions
Perl - doing several things at the same time
What does blessing a variable in Perl mean?
Well House Consultants - a potted history
Multiway branches in Perl - the given and when syntax
Cheap Country Hotel in Melksham, Wiltshire?
Testimonials - Well House Consultants Open Source courses
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2970_Per ... -time.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat