#!/usr/bin/perl -w
use strict;

my $MAX = 100;
use Time::HiRes q/usleep/;

$SIG{USR1} = sub { 1 };

if (my $kid = open(my $p, "|-")) { # parent
    if (!fork) { # coprocess to send signals
        close $p;
        while (1) {
            kill USR1 => $kid;
            usleep(rand()*30000);
        }
        exit 0;
    } 
    my $i=0;
    while ($i++ < $MAX) {
        syswrite($p, "y\n", 2);
        usleep(rand()*30000);
    }
    exit 0;
} else { # kid
    my $j=0;
    $j++ while <>;
    print $j == $MAX ? "ok # reached $j tries\n" :
                       "not ok # exited after $j tries\n";

}

