By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,851 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,851 IT Pros & Developers. It's quick & easy.

What happend with parent process.

P: 3
What happend with parent proces.
Two scripts:

perl4:
Expand|Select|Wrap|Line Numbers
  1. #
  2. # loop operations
  3. #
  4. use strict;
  5. my @food;
  6. my $morsel;
  7. my $TableLength;
  8. my $i;
  9. @food = ("pear", "plum", "egg", "apple");
  10. #
  11. # Table printing
  12. #
  13. $TableLength = $#food;
  14. print $TableLength;
  15. foreach $i (@food){
  16.     print $i."\n";
  17. }
  18. do
  19. {
  20.     print "Password?";
  21.     $a = <STDIN>;
  22.     chop $a;
  23. }
  24. while ($a ne "fred");
perl5:
Expand|Select|Wrap|Line Numbers
  1. # Variable declaration
  2. my $FileName = "perl4.pl";
  3. my $Line = "";
  4. my @Table;
  5. my $pid;
  6.  
  7. $pid = system $FileName;
  8.  
  9. if(not defined $pid){
  10. #child
  11.    print "no resources";
  12. }elsif($pid == 0)
  13. {   
  14.     print "I am a child";
  15.     STDOUT->autoflush();
  16.     sleep(5);
  17.     print "I am a child2\n";
  18. }else{
  19.     print "I am a parent";
  20.     STDOUT->autoflush();
  21. }
printout:
prear
plum
egg
apple
Password?fred
I am a childI am a child2

And I have no output from parent process. Can anybody explain me why?
Mar 1 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
In your program, $pid is the return value of the system() function, which is the value of wait(), which is the value of the deceased forked process, which must be 0 (zero) judging by the behavior of your program. You are also using the autoflush() method without loading the IO::Handle module, which should cause your script to abort.

See, your program is not checking if a process is a child or a parent, it is only checking the return value of the system() function.
Mar 1 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.