For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > July 2004 > Please help! URGENT!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Please help! URGENT!
Nonu

2004-07-06, 6:23 am

Hi All,

I have perl program which takes input from a file and processes each of the lines. There are 2 input lines but the while loop used to read from the file is executed 3 times. I don't understand why.

----------- prob.pl ----------
#!/opt/perl/bin/perl -w
use strict;

my @child_pids;

sub start_cli {
print "start_cli: enter... parent pid : ", getppid (), "
";
print "start_cli: id is $_[0]
";
}

sub start_svr {
my $child_pid;

print "start_svr: enter... parent pid : ", getppid(), "
";

unless (defined ($child_pid = fork())) {
die "Can't fork: $!";
}

push(@child_pids, $child_pid);

# Child process - Server (local)
unless ($child_pid) {
print "start_svr: from child process - $$; id is $_[0]
";
exit (0);
}

# Parent process - Return
print "start_svr from parent process
";
}

# main

my @fields;

open(FILE, "$ARGV[0]");

while (<FILE> ) {
chomp $_;
# Server: s(for server):id
# Client: c(for client):id
print "$_ : $$
";
@fields = split(/:/);

if ($fields[0] eq "c") {
print "This is a client entry - $fields[0]
";
/c:([0-9]+)/;
start_cli($1);
} else {
print "This is a server entry - $fields[0]
";
/s:([0-9]+)/;
start_svr($1);
}
}

close (FILE);

foreach (@child_pids) {
print "pid - $_
";
waitpid($_, 0);
}

---------------- prob_input ---------------
s:10
c:20

--------------- output got -----------------
#perl prob.pl prob_input
s:10 : 25934
This is a server entry - s
start_svr: enter... parent pid : 25022
start_svr from parent process
start_svr: from child process - 25935; id is 10

c:20 : 25934
This is a client entry - c
start_cli: enter... parent pid : 25022
start_cli: id is 20

c:20 : 25934
This is a client entry - c
start_cli: enter... parent pid : 25022
start_cli: id is 20

pid - 25935
#

~~~~~~~~~~~~

Now that was the problem. Now what i think is that, the fork causes the child and parent processes to share the same file handlers. Since the child process exits after printing a line of output, the file handler is automatically closed in the child process. This is affects the file handler in the parent process (this is happening on HP-UX only, supposedly works on Linux - I have not checked it on Linux) So what I need is to find out if this is how the close system call in is implemented? (that is, is this is a predetermined action? ) As a step towards this, I downloaded the source file for the perl i'm using but don't know how to find out where the close system call is implemented.

That sums up my whole problem.

Thanks in advance
Binu George
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com