I try to make a script that run three child process an return some information, when all child process finish, i need to continue with a parent process, with the information returned by the childrens, but i have soooo manyyy problems becouse i can't understand on my mind, how works the fork function, perhaps somebody help me with that, anyway, i developed this script on windows but i'm use or i hope use on linux.

when i test the script, return this:

Child 1 -> -3334

Child 2 -> -4564

parent process

Child 3 -> -4365



yes, on windows return negative process id.

On windows at least the signal CHLD or CLD not work, (but both are defined in %SIG hash),

i say that, becouse, if i do something like SIG{CHLD} = sub { print "it's work"; }; nothing happen, it's correct? why?, in fact if i use something like:

sub REAPER { # don't work almost on windows while (($child = waitpid(-1,WNOHANG)) > 0) {} $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER;


the result of the script it's the same, why?, what wrong i do? to replace this i put three waitpit but nothing change, what can i do?

To set the priority i tried to use setpriotity, but it's not implement on windows yet,

on linux, work?

one interesting thing, if i use three wait; the script work fine, but i try to do the right,

i don't know why work with that and don't work with the other.

well nothing more for now

thank you

use POSIX ":sys_wait_h"; $|=1; #$pid=fork(); # when i use setpriority i call a fork here and replace +below in the if sentence. #$pid1=fork(); #$pid2=fork(); #setpriority($$,0); # not implement on windows :-( #setpriority($pid,1); #setpriority($pid1,2); #setpriority($pid2,3); my %CHILD = (); sub REAPER { # don't work almost on windows while (($child = waitpid(-1,WNOHANG)) > 0) {} $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER; # if i do something like $SIG{CHLD} = sub { print "it's work"; }; # well ... nothing happen, why? if (!($CHLD{1} = fork())) { # when use setpriority i replace "if (!($C +HLD{1} = fork())) {" to "if (!$pid) {" print "child 1 -> $$\n"; sleep 2; # other small thing, why this sleep don't work here and s +leep the parent process? exit; # but only for 2 seconds, if i have three sleep with 2 se +conds 2*3 = 6 seconds, # itn't it? yeah yeah i'm lost ;-( } if (!($CHLD{2} = fork())) { print "child 2 -> $$\n"; sleep 2; exit; } if (!($CHLD{3} = fork())) { print "child 3 -> $$\n"; sleep 2; exit; } # if i replace $CHILD{1} with -1... dosen't work :-( # obviusly, when i use this i don't use the signal $SIG{CHLD} # if i only put one waitpid... well don't work #waitpid $CHILD{1}, WNOHANG; # if i put wait; the script work fine #waitpid $CHILD{2}, WNOHANG; # wait; #waitpid $CHILD{3}, WNOHANG; # wait; print "parent process\n";

Edited by planetscape - added code tags and line breaks


In reply to forks, how do that? by apolo

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.