Wise Monks,

this is more a request for confirmation than a real question, because it seems that I worked out by myself.

I have to use a Java application to do some homework stuff about neural networks (using Joone), but I mostly hate to use Java and so I reduced its usage to a minimum. I resorted to use a controlling Perl script, which spawns the different Java applications and reads results from their standard output.

This time, too, the operations are quite lengthy, and I only have a personal laptop for these computations. In the evening I like to go home, but stopping a 2-hour computation and waste it is not good (some of you may recall I'm not new to this kind of problems: Variable persistence for suspend-and-resume programs). So, I implemented a save-and-resume mechanism inside the Java code, based on the test for presence of a file inside the filesystem as a signal from the outside to stop computing, save all and exit. Yes, I don't even want to know how Java handles real signaling.

In the Perl script, I set $SIG{INT} to generate the given file as follows:

$SIG{INT} = sub { open my $fh, ">PleaseStop" or warn "open(PleaseStop): $!"; }
At the very start of the program I also wipe out the PleaseStop file, of course. Then, later in the program, I called the Java app like this:
my $outcome = qx/java MyClass param1 param2 param3 ... /;
As you may guess, this approach fails half of the time to give the required graceous-stop behaviour. After some time, I realised that the backtick/qx operator actually calls fork() at some time, and I probably end up with two possible situations: in the desired one the Perl script is receiving the INT when I hit CTRL-C; in the other, it's the Java program that receives the signal.

As anticipated, I have no will to dig the secrets of signal handling in Java, so I worked out the following solution: disabling the INT signal before the call to the Java program, but not in the main Perl script:

# When it's time to call... call call_command sub call_command { my $command = shift; my ($pid, $rd, $wt); { local $SIG{INT} = 'IGNORE'; $pid = open2($rd, $wt, $command); # From IPC::Open2 } my $outcome = join "", <$rd>; waitpid $pid, 0; return $outcome; }
Yes, I know that the call to waitpid should be more robust, but this is not a widespread script and I can live with it.

Is this approach correct, or should I do something better? Do you see troubles in my future with this? I work with Perl 5.8.6 in Linux (kernel 2.4), and I don't plan to use the script other than in this environment.

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Signal handling with (implicit) fork by polettix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.