Re: fork() and main_quit()



Daniel Flemming wrote on 9/11/2004, 12:04 PM:

If you're communicating via pipes anyway, can't you send a command
over the pipe
from the parent process to cause the child to exit? Surely that'd be
easier than
setting up signal handlers in the child.

That would certainly be an option, but I just wanted something simple.

I've played with different approaches since the original post and it 
looks like calling init() *after* the fork in the parent *only* is 
indeed the solution to the problem.

If you don't do that, like in

     use Gtk2 -init;

     my $pid = fork();

     die "Fork failed" unless defined $pid;

     if($pid) {
         print "Parent: Child pid is $pid\n";
     } else {
         # Child
         print "Child: My pid is $$\n";
         sleep(120);
         exit 0;
     }

         # Parent
     my $window = Gtk2::Window->new ('toplevel');
     my $button = Gtk2::Button->new ('Quit');
     $button->signal_connect (clicked => sub {
         kill $pid;
         Gtk2->main_quit });
     $window->add ($button);
     $window->show_all;
     Gtk2->main;

you get the described mess. If, however, you call init() manually 
*after* the fork and only in the parent, it works fine:

     use Gtk2;

     my $pid = fork();

     die "Fork failed" unless defined $pid;

     if($pid) {
         Gtk2->init();
         print "Parent: Child pid is $pid\n";
     } else {
         # Child
         print "Child: My pid is $$\n";
         sleep(120);
         exit 0;
     }

         # Parent
     my $window = Gtk2::Window->new ('toplevel');
     my $button = Gtk2::Button->new ('Quit');
     $button->signal_connect (clicked => sub {
         kill $pid;
         Gtk2->main_quit });
     $window->add ($button);
     $window->show_all;
     Gtk2->main;

Thanks for your help on this, guys!

--
-- Mike
Mike Schilli
m perlmeister com




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]