Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

forking hell

  • 25-06-1999 02:40PM
    #1
    Closed Accounts Posts: 225 ✭✭


    Hey there,

    anyone have any forking examples in perl? I'm trying to run a perl proggie that forks off a time consuming loop so the user interface isn't affected.

    I have the "Programing Perl" outline of a fork, but no sample programs (which is what I'm really after). Post sample code here smile.gif :


Comments

  • Registered Users, Registered Users 2 Posts: 10,339 ✭✭✭✭LoLth


    Found this on a FAQ site...

    I want to start a process ( a rather long-running script) when a button is
    pushed and be able to stop it after clicking another button.
    Here's the example code:
    #!/usr/local/bin/perl

    use Tk;

    my $main = new MainWindow;
    $main->Button(-text=>'run', #make run button
    -command => sub {
    FORK:{
    if($pid=fork){
    print "start: $pid\n";
    }elsif (defined $pid) {
    run_tst();
    POSIX::_exit;
    }

    }
    })->pack;


    $main->Button(-text=>'cancel', #make cancel button
    -command =>sub {
    kill 'STOP',$pid;
    })->pack;

    MainLoop;

    sub run_tst{
    print "Start:\n";
    for($i=0;$i<10000;$i++){ print "$i\n";}
    }

    ################End of code ##########

    The code above almost works. I can start the run_tst process by cliking
    on the run button, and I can end the process early by clicking on the
    cancel button. The problem is that if I allow the run_tst process to run
    to its end (count all the way to 10000), I can't stop the run_tst process
    again when its started in the future. so basically, I'd like to be able
    to start and stop a process many times.

    The reply to that was...

    Phil Ptkwt Kristin wrote:
    >
    > I want to start a process ( a rather long-running script) when a button is
    > pushed and be able to stop it after clicking another button.
    > Here's the example code:
    > #!/usr/local/bin/perl
    >
    > use Tk;

    use POSIX; # <<< You need this

    >
    > my $main = new MainWindow;
    > $main->Button(-text=>'run', #make run button
    > -command => sub {
    > FORK:{
    > if($pid=fork){
    > print "start: $pid\n";
    > }elsif (defined $pid) {
    > run_tst();
    > POSIX::_exit;

    That should be
    POSIX::_exit(0);

    > }
    >
    > }
    > })->pack;
    >
    >
    > $main->Button(-text=>'cancel', #make cancel button
    > -command =>sub {
    > kill 'STOP',$pid;
    > })->pack;
    >
    > MainLoop;
    >
    > sub run_tst{
    > print "Start:\n";
    > for($i=0;$i<10000;$i++){ print "$i\n";}
    > }
    >
    > ################End of code ##########
    >
    > The code above almost works. I can start the run_tst process by cliking
    > on the run button, and I can end the process early by clicking on the
    > cancel button.

    STOP signal does _NOT_ end the process, it suspends it until you send
    CONT signal (like 'pause' on CD player or VCR). You probably want TERM.

    >The problem is that if I allow the run_tst process to run
    > to its end (count all the way to 10000), I can't stop the run_tst process
    > again when its started in the future.

    That is because as you had it child process did not exit and
    "took over" MainLoop in some sense. Next "start" actually started a
    grandchild and set $pid in child. But next cancel _may_ have
    been handled by (original parent) - in short a mess.


    Any use?


Advertisement