RE: threads in perl

From: Igor Ryaboy (igor.ryaboy_at_starcore-dsp.com)
Date: 01/25/04

  • Next message: mcdavis941_at_netscape.net: "Using CPAN.PM on Win32: How Direct to Proper Perl Location"
    Date: Sun, 25 Jan 2004 14:20:13 +0200
    To: "zentara" <zentara@highstream.net>,	<beginners@perl.org>
    
    

    Hi,
    Thanks for your help
    I've tried to use your second example in the following way:
    Instead of xterm I tried to invoke some other script:
    pid1 = open(PH, "my_perl_script |") or warn $!;
    The my_perl_script refused to die. I still run when the program end?
    Any ideas?
    Igor

    -----Original Message-----
    From: Linux@onion.perl.org [mailto:Linux@onion.perl.org] On Behalf Of
    zentara
    Sent: Thursday, January 22, 2004 9:11 PM
    To: beginners@perl.org
    Subject: Re: threads in perl

    On Thu, 22 Jan 2004 08:22:04 +0200, igor.ryaboy@starcore-dsp.com (Igor
    Ryaboy) wrote:

    >Hi,
    >Thanks for your help, 1 more question related to your advice
    >Ok, How can I kill exec after it was started in different thread?
    >Igor

    You have to be careful with exec, because it replaces the current
    running process(i.e.) thread. But threads all share the same pid,
    so you would probably kill the main thread.

    You could use a form of system, to get the pid and pass it back with
    shared variables. REMEMBER, when you start a program up, you
    are usually dealing with multiple pids, you have the xterm, the bash
    shell, and the program run by the shell. So if you kill such a beast,
    you need to get the parent pid. So the following examples work, but
    you may need to make adjustments to account for your particular setup.

    The first example just shows how to share a variable:
    The second example shows what you want, a thread starting a process,
    then having it timed out in the parent.
    ###########################################
    #!/usr/bin/perl
    use threads;
    use threads::shared;
    use strict;

    our $a:shared = 1;
    print "main start->$a\n";
    threads->create(\&display);
    threads->create(\&display1);
    <STDIN>;
    print "main done-> $a\n";

    sub display {
       our $a = 2;
       $a++;
       print "from t1->$a\n";
    }

    sub display1 {
      our $a = 99;
       print "front2->$a\n";
    lock $a;
    cond_broadcast($a);
    }
    __END__

    #################################################
    #!/usr/bin/perl
    use threads;
    use threads::shared;
    use strict;
    use warnings;

    print "pid->$$\n";
    our $pid1 : shared = 1;
    threads->create(\&display);
    threads->create(\&display1);

    #main thread again
    for(1..10){
      print "$_ $pid1\n";
      sleep 1;
    }
    print "$pid1\n";
    kill 'INT',$pid1;
    print "$pid1 ->killed\n";
    #####################################3

    sub display {print "from t1->howdy\n";}

    sub display1 {
      our $pid1;
      $pid1 = open(PH, "xterm -e top |") or warn $!;

    print "t1->newpid->$pid1\n";
    lock $pid1;
    cond_broadcast($pid1);
    }
    __END__

    --
    When life conspires against you, and no longer floats your boat,
    Don't waste your time with crying, just get on your back and float.
    -- 
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>
    

  • Next message: mcdavis941_at_netscape.net: "Using CPAN.PM on Win32: How Direct to Proper Perl Location"

    Relevant Pages


    Loading