home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5810 < prev    next >
Encoding:
Text File  |  1992-09-09  |  2.1 KB  |  82 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!caen!destroyer!ubc-cs!dhami
  3. From: dhami@cs.ubc.ca (Mandeep S Dhami)
  4. Subject: Re: fork question
  5. Message-ID: <1992Sep9.204742.21562@cs.ubc.ca>
  6. Sender: usenet@cs.ubc.ca (Usenet News)
  7. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  8. References: <peter.716061574@merlin>
  9. Date: Wed, 9 Sep 92 20:47:42 GMT
  10. Lines: 70
  11.  
  12. peter@merlin.acadiau.ca (Peter Steele) writes:
  13. >
  14. >$| = 1;
  15. >print "Starting...\n";
  16. >fork && &test1;
  17. >fork && &test2;
  18. >print "Done.\n";
  19. >wait; wait;
  20. >
  21. >When I run this program, I'd expect the to wait calls to wait for the
  22. >two child processes to complete. However, both of the calls return -1,
  23. >and the parent exits before the child processes are done. Observe:
  24.  
  25. >merlin% testit
  26. >Starting...
  27. >test1
  28. >Done.
  29. >test2
  30. >Done test1
  31. >merlin% Done test2
  32.  
  33. >Why are the wait calls not waiting?
  34. >
  35.  
  36. The problem, as I see it, is that you want fork || &test* NOT fork && &test*.
  37. Because you want test to be executed by the child and the child sees
  38. a value of fork == 0 (i.e && fails) but parent sees the pid and executes
  39. test*.
  40.  
  41. Hope this variation on your solution helps:
  42.  
  43. print "Parent is $$\n";
  44. fork || &test1;
  45. fork || &test2;
  46. print "wait 1 returns the pid - ", wait, "\n";
  47. print "wait 2 returns the pid - ", wait, "\n";
  48. print "Done $$\n";
  49. exit 0;
  50.  
  51. sub test1 {sleep(2); print "Done test1 ($$)\n"; exit 0;}
  52. sub test2 {sleep(4); print "Done test2 ($$)\n"; exit 0;}
  53.  
  54. which produces:
  55. % testprog
  56. Parent is 8836
  57. Done test1 (8837)
  58. wait 1 returns the pid - 8837
  59. Done test2 (8838)
  60. wait 2 returns the pid - 8838
  61. Done 8836
  62.  
  63. What I think you want. To compare, check what I get if && is changed to ||.
  64. which produces:
  65. % testbroken
  66. Parent is 8859
  67. wait 1 returns the pid - -1
  68. wait 2 returns the pid - -1
  69. Done 8861
  70. Done test1 (8859)
  71. % Done test2 (8860)
  72.  
  73. NOT what you want!.
  74.  
  75. Hope this helps.
  76. Mandeep (another grad looking for a real job!) Dhami.
  77. __________________________________________________________________
  78. "I'm very brave generally," he went on in a low voice:
  79. "only to-day I happen to have a headache."
  80.                                       -- Lewis Carroll
  81. __________________________________________________________________
  82.