Can you say a little more about what you think you don't get? It's a little hard to know where to begin except to point you at other, more general references...

Do you understand the idea of time-slicing? The idea is that even if you've just got one processor down there running everything, you can handle multiple jobs at the same time by having the processor constantly switching between jobs. In Unix, each of these jobs is called a "process" (or these days, a "thread", which is a similar idea).

In unix-like systems, one process can spin off another process by doing a "fork", which creates a clone of the first process -- and note that both the original and the clone then are running the same code. The code itself has to be able to figure out whether it's the original process (the "parent") or the clone (the "child"), and it can do that by looking at the return from the "fork" command. A "fork" returns the process id of the newly cloned child to the parent... but it doesn't return anything to the child, so the code can check that return value to find out whether it's parent or child.

Just for the hell of it, here's a demo script I wrote recently to show how you can use a child process to handle a long-running task while the parent goes off and does something else. This script has the child reporting on it's status to the parent so that, for example, you can update a "percent done" message while the child is working:

#!/usr/bin/perl =head1 NAME child_to_parent_ipc_demo - child process reports on it's status to the + parent =head1 SYNOPSIS child_to_parent_ipc_demo =head1 DESCRIPTION A demonstration of a technique of spinning off a child process which can report on it's progress back to the parent process, while letting the parent go off and do some other things. =cut use warnings; use strict; # $|=1; our $VERSION = 0.01; use IO::Handle; # brings in autoflush and blocking # first set up a bi-directional pipe, # (will later close one direction) my ($reader, $writer); pipe $reader, $writer; $writer->autoflush(1); # still needed, even with modern perls $reader->blocking(0); # keeps reads from hanging my $pid; if ($pid = fork) { # this is parent code close $writer; print "Parent $$ is hearing from child $pid:\n"; while (1) { my $status = ''; $status = <$reader>; # magic "blocking(0)" keeps # this from blocking if ( defined( $status ) and ( $status =~ m{ ^ \d+ $ }xms ) ){ # recieved an update child process status chomp( $status ); print "\n$status% "; if ( $status >= 90 ) { close $reader; print "\n"; exit; } } else { # can continue doing stuff here while child is running print "."; sleep 1; } } } else { # this is child code die "cannot fork: $!" unless defined $pid; close $reader; # child code that takes awhile, but reports on it's status for my $i (1..10) { # the child takes a varying amount of time my $pause = int( rand(4) + .5 ); sleep $pause; my $status = $i * 10; print $writer "$status\n"; } close $writer; # i/o doesn't go through until closed, # *unless* autoflush is set to 1 exit; } __END__ =head1 SEE ALSO L<perlipc> The Perl Cookbook, 2nd ed. Chapter 16 (interprocess communication) Chapter 7 (non-blocking i/o) =head1 AUTHOR Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt> =head1 COPYRIGHT AND LICENSE Copyright (C) 2008 by Joseph Brenner This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.2 or, at your option, any later version of Perl 5 you may have available. =head1 BUGS None reported... yet. =cut

In reply to Re: Process Management by doom
in thread Process Management by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.