in reply to Process Management
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Process Management
by gwadej (Chaplain) on Jan 18, 2009 at 02:11 UTC | |
by almut (Canon) on Jan 18, 2009 at 13:53 UTC | |
by gwadej (Chaplain) on Jan 19, 2009 at 14:18 UTC | |
by doom (Deacon) on Jan 18, 2009 at 09:03 UTC | |
by gwadej (Chaplain) on Jan 19, 2009 at 14:27 UTC | |
|
Re^2: Process Management
by koolgirl (Hermit) on Jan 19, 2009 at 19:21 UTC |