anna has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone. I am trying to write a simple shell interpreter. And I want it to act like bash, i.e. when user wants to run a process in the background, and the process tries to access stdin or stdout, the shell sends it SIGTTOU. So, I write:
... $pid = fork() if($pid == 0) { if($command =~ /\&$/) { if(-t STDIN and -t STDOUT) { kill TTOU =>$pid; exec($command); } } else { ... } }
As I expect, it kills the forked process without executing the command. Can anyone help me with the problem?

thanks in advance, anna.

Replies are listed 'Best First'.
Re: send SIGTTOU to forked process?
by ikegami (Patriarch) on Dec 11, 2009 at 08:30 UTC
    It seems to stop the parent too. I don't know if this is the proper workaround, but this works:
    use Time::HiRes qw( sleep ); my $pid = fork(); die $! if !defined($pid); if ($pid == 0) { for (1..8) { sleep(.250) if $_ != 1; print(time(), "\n"); } } else { $SIG{TTOU}='IGNORE'; sleep 1; kill TTOU => $pid or die $!; sleep 3; kill CONT => $pid or die $!; waitpid($pid, 0); }
    1260520147 1260520147 1260520148 1260520148 1260520151 1260520151 1260520152 1260520152

    And it also works if the child is the one to send the signal.

    use Time::HiRes qw( sleep ); my $pid = fork(); die $! if !defined($pid); if ($pid == 0) { for (1..2) { kill TTOU => 0 or die $! if $_ != 1; for (1..4) { sleep(.250) if $_ != 1; print(time(), "\n"); } } } else { $SIG{TTOU}='IGNORE'; sleep 4; kill CONT => $pid or die $!; waitpid($pid, 0); }
    1260520571 1260520572 1260520572 1260520572 1260520575 1260520576 1260520576 1260520576
Re: send SIGTTOU to forked process?
by ikegami (Patriarch) on Dec 11, 2009 at 08:04 UTC

    This works:

    $ perl -le'kill TTOU => 0 or die $!; print "done"' [1]+ Stopped perl -le'kill TTOU => 0 or die $!; print "done"' $ fg perl -le'kill TTOU => 0 or die $!; print "done"' done

    Check for an error. If there's no error, it's not a problem sending the signal aka not a Perl problem.

Re: send SIGTTOU to forked process?
by Anonymous Monk on Dec 11, 2009 at 07:08 UTC
    kill takes numbers as arguments
      It's perfectly happy with signal names too