Link to home
Start Free TrialLog in
Avatar of s_l
s_l

asked on

system() call does the call twice - is it a bug?

Here is a very tricky situation that happened with the following version of Perl (haven't checked it with other versions):
This is perl, v5.6.0 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2000, Larry Wall

Here is the applicatiob (in C) that I use inside the system call:

#include "stdafx.h"
#include <stdlib.h>
int main(int argc, char* argv[])
{
     fprintf(stderr,"\nHello World!\n");
     exit(-1);
     return 0;
}

Now, here is the Perl program:

mkdir("Result",0777);
system("test.exe");

Now, the result is that I get the "Hello World!" printed twice. This bizzare situation happen only when my application exits with -1 (other exit codes works fine) and when the mkdir command is used.

Any one have an idea? Is this a bug? Is there some work around that I can use? (My application must exit with -1, and I must create the directory). I'm not sure, but I think that it only happens if the mkdir fails because the directoy already exists.

Thanks
Avatar of kvsaarland
kvsaarland

listening...
That's a new one on me.

Does it only happen with stderr or does it still happen if you use stdout ?

A usefull diagnostic might be to add a sleep to test.exe..

{
    fprintf(stderr,"\nHello World!\n");
    sleep(3);
    exit(-1);
    return 0;
}

.. then you'll be able to tell wether test.exe is being run twice or its output is just being duplicated.
Your application prints "Hello World" twice only when the "Result" directory does exist.

So I tried to have this small work-around:
if (-d "Result") {
  print "Directory exists already...";
} else {
  mkdir ("Result",0777);
}
system("test.exe");

But this prints "Hello World" twice when the directory does not exist, it works fine when the directory exists!

(stderr or stdout seem to have the same result, I'm using 'stdio.h' in stead of 'stdafx.h',  did not test sleep yet)

Any Perl Guru around????????
ASKER CERTIFIED SOLUTION
Avatar of vermeylen
vermeylen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
looks like a bug in perl.  What's the output of "perl -V" ?
Avatar of s_l

ASKER

vermeylen,

Thanks for your help. It looks like a bug in Perl to me, like the $! variable is not reset in the beginning of the system call.

I want to see if any one else has some insights about this, maybe some one with accessibility to the codes of perl... if nothing new - I will close it.

Thanks.
Avatar of s_l

ASKER

Thanks for the work around!