Home > Archive > PERL Beginners > March 2004 > problem with fork & wait
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 |
problem with fork & wait
|
|
| T.S. Ravi Shankar 2004-03-29, 2:36 pm |
| Hi,
At reaching a certain point in my perl program, I need to run a process
( say XYZ ) using SYSTEM command. The result file that this process
would produce will be result.<process_id>. I will have to wait until
this result file is produced & then proceed extracting certain things
from this file.
I am implementing this as :
foreach $check (@run) {
$pid=fork;
if($pid == 0) {
exec("XYZ -a $check");
}else{
$pid1=wait;
}
if ( -e "result.$pid1") {
## perform operations on the file result.$pid1##
}
system("rm result.$pid1");
}
This seems to be performing inconsistently. I am seeing this working
properly sometimes & not working properly sometimes. This works on
certain workstations & doesn't work on certain workstations.
Why is this inconsistency ??
I would be satisfied if I am given a clean/consistent/straight forward
implementation methodology for my purpose.
Thanks,
Ravi
| |
| Bob Showalter 2004-03-29, 3:33 pm |
| T.S. Ravi Shankar wrote:
> Hi,
>
> At reaching a certain point in my perl program, I need to run a
> process ( say XYZ ) using SYSTEM command. The result file that this
> process would produce will be result.<process_id>. I will have to
> wait until this result file is produced & then proceed extracting
> certain things from this file.
>
> I am implementing this as :
>
> foreach $check (@run) {
>
> $pid=fork;
> if($pid == 0) {
> exec("XYZ -a $check");
> }else{
> $pid1=wait;
> }
>
> if ( -e "result.$pid1") {
Seems like this should be "result.$pid", since $pid is the child process
number. $pid1 is the exit status from wait().
> ## perform operations on the file result.$pid1##
> }
>
> system("rm result.$pid1");
No need to use system() here; use unlink().
>
> }
>
> This seems to be performing inconsistently. I am seeing this working
> properly sometimes & not working properly sometimes. This works on
> certain workstations & doesn't work on certain workstations.
| |
| Smoot Carl-Mitchell 2004-03-29, 4:31 pm |
| On Mon, 29 Mar 2004 18:38:48 +0530
"T.S. Ravi Shankar" <Ravi.Shankar@synopsys.com> wrote:
> At reaching a certain point in my perl program, I need to run a
> process( say XYZ ) using SYSTEM command. The result file that this
> process would produce will be result.<process_id>. I will have to
> wait until this result file is produced & then proceed extracting
> certain things from this file.
>
> I am implementing this as :
>
> foreach $check (@run) {
>
> $pid=fork;
> if($pid == 0) {
> exec("XYZ -a $check");
> }else{
> $pid1=wait;
> }
The above works, but it is equivalent to:
system("XYZ -a $check");
You should check the return value of system. A non-zero value means the
forked program returned a non-zero exit status which depending on what
you do with the output may or may not be an error.
--
Smoot Carl-Mitchell
Systems/Network Architect
email: smoot@tic.com
cell: +1 602 421 9005
home: +1 480 922 7313
|
|
|
|
|