Article View: pl.comp.lang.perl
Article #52008Re: Równoległe uruchamianie dwóch procesów
From: neo77
Date: Wed, 03 Nov 2010 10:18
Date: Wed, 03 Nov 2010 10:18
49 lines
3220 bytes
3220 bytes
On 2 Lis, 17:54, Szymon Sokół <szy...@bastard.operator.from.hell.pl>
wrote:
> On Mon, 01 Nov 2010 20:31:40 +0100, Pawel wrote:
> > On 2010-11-01 20:22, Waldek M. wrote:
> >>> Zerknalem takze do opisu forka, ale z tego co zrozumialem, to fork
> >>> robi kopie procesu glownego, a nie uruchamia osobnego skryptu.
>
> >> Owszem, dlatego stosuje się najczęściej fork+exec.
> > A moglbys dac jakis przyklad kodu, bo nie bardzo wiem jak sie do tego
> > zabrac.
>
> Przykładów są tysiące (wystarczy w Google wpisać "perl fork exec
> example"...), np. tu:http://www.wellho.net/resources/ex.php4?item=p223/f1.pl
> albo i w FAQ tej grupy:http://www.kt.agh.edu.pl/perl-faq/Uruchamianie#20
> ale pewnie prościej będzie to wystrugać za pomocą modułu IPC::Run.
>
> --
> Szymon Sokół (SS316-RIPE) -- Network Manager B
> Computer Center, AGH - University of Science and Technology, Cracow, Poland Ohttp://home.agh.edu.pl/szymon/PGP key id: RSA: 0x2ABE016B, DSS: 0xF9289982 F
> Free speech includes the right not to listen, if not interested -- Heinlein H
sub forker {
my $pid = fork;
if (my $pid) { # $pid > 0 => parent
return; # czy co tam innego
ma robic parent
} elsif (defined $pid) { # $pid == 0 => child
system(...);
exit 0;
} else {
# $pid not defined => error podczas forkowania..
}
}
no a teraz wystarczy tylko kontrolowac ilosc uruchamianych procesow,
ale to juz prosta sprawa
forker for (1 .. $MAX_PROC-1);
while (forker) {
waitpid(-1, 0); # czekaj az dowolne dziecko sie skonczy
}
# czekanie na reszte niedobitkow
my $p=-1;
waitpid(-1, 0) while $p != -1;
pozdrawiam
neo77
Technical Details
Message-ID:
<a4049f7e-4756-4566-8f54-aa97ee8fcd91@g13g2000yqj.googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!newsfeed00.sul.t-online.de!t-online.de!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!g13g2000yqj.googlegroups.com!not-for-mailReferences:
<ian314$2bc$1@inews.gazeta.pl> <116s06x665nir$.dlg@localhost.localdomain> <ian4j3$7u1$1@inews.gazeta.pl> <1bj979bu5bw0s$.dlg@falcon.sloth.hell.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!news.nask.pl!news.nask.org.pl!newsfeed00.sul.t-online.de!t-online.de!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!g13g2000yqj.googlegroups.com!not-for-mail From: neo77 <neo77@panth-net.com> Newsgroups: pl.comp.lang.perl Subject: Re: Równoległe uruchamianie dwóch procesów Date: Wed, 3 Nov 2010 03:18:52 -0700 (PDT) Organization: http://groups.google.com Lines: 57 Message-ID: <a4049f7e-4756-4566-8f54-aa97ee8fcd91@g13g2000yqj.googlegroups.com> References: <ian314$2bc$1@inews.gazeta.pl> <116s06x665nir$.dlg@localhost.localdomain> <ian4j3$7u1$1@inews.gazeta.pl> <1bj979bu5bw0s$.dlg@falcon.sloth.hell.pl> NNTP-Posting-Host: 87.204.150.226 Mime-Version: 1.0 X-Trace: posting.google.com 1288779532 31588 127.0.0.1 (3 Nov 2010 10:18:52 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 3 Nov 2010 10:18:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: g13g2000yqj.googlegroups.com; posting-host=87.204.150.226; posting-account=FoRMkgoAAAB9M583cuIYMaM3EaLsY1IT User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3,gzip(gfe) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit