We help IT Professionals succeed at work.

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

s_l
s_l asked
on
Medium Priority
379 Views
Last Modified: 2008-03-17
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
Comment
Watch Question

listening...

Commented:
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????????
two things seem to be combined:
1) from the perlfunc manpages for system: "Return value of -1 indicates a failure to start the program (inspect $! for the reason)."
2) exit(-1) in the test.cpp application.

If exit(-1) is changed to anything else (exit(-2), exit(3), ...) the problem seems to be solved.
However, when using exit(-1), you can include
$!="";
or
$!=0;
to have the system call executed once:

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

The issue can be isolated to:
$!=1;
system("test.exe");

"Hello World" is printed twice, while in:

$!=0;
system("test.exe");

"Hello World" is printed once.

Commented:
looks like a bug in perl.  What's the output of "perl -V" ?
s_l

Author

Commented:
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.
s_l

Author

Commented:
Thanks for the work around!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.