Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Calling Control-C in linux - Perl equivalent

by Freezer (Sexton)
on Aug 17, 2012 at 15:39 UTC ( [id://987998]=perlquestion: print w/replies, xml ) Need Help??

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

This post takes code that was given to me from a previous post.

Within a loop the following is run:
my $timer_go:shared = 0; my $worker = threads->create(\&worker, $call); my $timer = threads->create(\&timer,$worker); $timer_go=1; $timer->join(); $worker->join();
The subroutines used are:
sub timer { my $worker = shift; my $timer_go; while(1){ if($timer_go){ my $count = 0; while(1){ $count++; if($count > 20){ print "timed out\nHit enter to finish\n"; # Send a signal to a thread $worker->kill('INT'); return; } sleep 1; print "timing $count\n"; } }else{sleep 1} } } sub worker { my $call = shift; $|++; $SIG{INT} = sub{ warn "Caught Zap!\n"; sleep 1; exit; }; # do your timed program here. #print "I am here \n\n"; print $call; #exit; system ($call); # This is where my system call is my $worker_pid = open( READ, "top -d 1 -b |" ); print "\t$worker_pid\n"; while(<READ>){ } return; }
For some reason unkown to me the system call works within the subroutine only once. Are all the processes killed and not just the one? I want to do the equivalent of me entering control-C in my Linux command line prompt. Presumably that is not happening with my Perl script.

Replies are listed 'Best First'.
Re: Calling Control-C in linux - Perl equivalent
by tobyink (Canon) on Aug 17, 2012 at 15:49 UTC

    Assuming you know the process ID, then:

    kill 2, $pid;

    Hard-coding the constant 2 may cause you trouble on non-Linux systems though. Better to use the constant defined in POSIX...

    kill(POSIX::SIGINT, $pid);

    If you have multiple processes to kill, kill takes a list...

    kill(POSIX::SIGINT, @murder_victims);

    Note that SIGINT is just a request for the process to end. The process can catch the signal and deliberately ignore it. If you want to force the process to die, then use SIGKILL instead.

    SIGINT though is exactly what Ctrl+C does in the terminal. The terminal sees that you've pressed Ctrl+C and sends a SIGINT to the current foreground job.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      I am struggling to use SIGINT. Because when I try to install it cpan says that it came with the dist. and that force install would have to be used. However when I try to run the program:
      Can't locate POSIX/SIGINT.pm in @INC (@INC contains:
        use POSIX; say POSIX::SIGINT;
        You seem to be trying to include the module POSIX::SIGINT, that is not what you want to do.
      When I run the program it does present a number that might be the process id. How do I use Perl to get the process id? Presumably when I press control-C that is only killing the specific process.
Re: Calling Control-C in linux - Perl equivalent
by Anonymous Monk on Aug 17, 2012 at 17:17 UTC

    On my system at least, if you simply use POSIX; the SIGINT constant becomes visible as 2.

      I have tried that ..... no error presented .... however I still have the problem of the program not doing the equivalent of Control-C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://987998]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found