G'day ristov,

Welcome to the Monastery.

"I am trying to write the code which would wait for the child process to exit -- but if the code gets the TERM signal while waiting, the child process should be terminated with the same signal. ... I am interested whether the same task can be accomplished with blocking waitpid() ..."

This code demonstrates how you could do this:

#!/usr/bin/env perl -l use strict; use warnings; use constant SLEEP_TIME => 20; my $pid = fork; die "Can't fork()" unless defined $pid; if ($pid) { print "Parent: $$; Child: $pid"; local $SIG{TERM} = sub { print "Parent received signal: @_"; kill TERM => $pid if kill 0 => $pid; }; print "Waiting on child ..."; waitpid($pid, 0); print "Child terminated."; } else { print "Child: $$"; local $SIG{TERM} = sub { print "Child received signal: @_"; die "Child died via signal handler.\n"; }; sleep SLEEP_TIME; exit; }

Running without any intervention:

Parent: 28186; Child: 28187 Waiting on child ... Child: 28187 ... SLEEP_TIME seconds pass Child terminated.

Running and sending TERM to the parent:

Parent: 28192; Child: 28193 Waiting on child ... Child: 28193 ... on another command line: kill -TERM 28192 Parent received signal: TERM Child received signal: TERM Child died via signal handler. Child terminated.

Running and sending TERM to the child:

Parent: 28201; Child: 28202 Waiting on child ... Child: 28202 ... on another command line: kill -TERM 28202 Child received signal: TERM Child died via signal handler. Child terminated.

The signal handler in the child was purely for demonstration purposes. Here's the output, from similar runs, with it removed.

Parent: 28405; Child: 28406 Waiting on child ... Child: 28406 ... SLEEP_TIME seconds pass Child terminated.
Parent: 28419; Child: 28420 Waiting on child ... Child: 28420 ... kill -TERM 28419 Parent received signal: TERM Child terminated.
Parent: 28430; Child: 28431 Waiting on child ... Child: 28431 ... kill -TERM 28431 Child terminated.
"... checking if the child process exists, in order to avoid sending TERM to a non-existing process."

That's handled by "... if kill 0 => $pid;". See kill.

See also: perlipc: Signals.

— Ken


In reply to Re: using waitpid() with signals by kcott
in thread using waitpid() with signals by ristov

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.