Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

forking hell

  • 25-06-1999 1: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