home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!caen!destroyer!ubc-cs!dhami
- From: dhami@cs.ubc.ca (Mandeep S Dhami)
- Subject: Re: fork question
- Message-ID: <1992Sep9.204742.21562@cs.ubc.ca>
- Sender: usenet@cs.ubc.ca (Usenet News)
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- References: <peter.716061574@merlin>
- Date: Wed, 9 Sep 92 20:47:42 GMT
- Lines: 70
-
- peter@merlin.acadiau.ca (Peter Steele) writes:
- >
- >$| = 1;
- >print "Starting...\n";
- >fork && &test1;
- >fork && &test2;
- >print "Done.\n";
- >wait; wait;
- >
- >When I run this program, I'd expect the to wait calls to wait for the
- >two child processes to complete. However, both of the calls return -1,
- >and the parent exits before the child processes are done. Observe:
-
- >merlin% testit
- >Starting...
- >test1
- >Done.
- >test2
- >Done test1
- >merlin% Done test2
-
- >Why are the wait calls not waiting?
- >
-
- The problem, as I see it, is that you want fork || &test* NOT fork && &test*.
- Because you want test to be executed by the child and the child sees
- a value of fork == 0 (i.e && fails) but parent sees the pid and executes
- test*.
-
- Hope this variation on your solution helps:
-
- print "Parent is $$\n";
- fork || &test1;
- fork || &test2;
- print "wait 1 returns the pid - ", wait, "\n";
- print "wait 2 returns the pid - ", wait, "\n";
- print "Done $$\n";
- exit 0;
-
- sub test1 {sleep(2); print "Done test1 ($$)\n"; exit 0;}
- sub test2 {sleep(4); print "Done test2 ($$)\n"; exit 0;}
-
- which produces:
- % testprog
- Parent is 8836
- Done test1 (8837)
- wait 1 returns the pid - 8837
- Done test2 (8838)
- wait 2 returns the pid - 8838
- Done 8836
-
- What I think you want. To compare, check what I get if && is changed to ||.
- which produces:
- % testbroken
- Parent is 8859
- wait 1 returns the pid - -1
- wait 2 returns the pid - -1
- Done 8861
- Done test1 (8859)
- % Done test2 (8860)
-
- NOT what you want!.
-
- Hope this helps.
- Mandeep (another grad looking for a real job!) Dhami.
- __________________________________________________________________
- "I'm very brave generally," he went on in a low voice:
- "only to-day I happen to have a headache."
- -- Lewis Carroll
- __________________________________________________________________
-