ajeet@perl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everybody,

Sorry for my second post, in previous one, wrongly i had given title as "signaling in threads", which not what i want

In any program if i write "use forks;" then there are two perl process running for my program. Now if i send any signal by extracting process ID on the basis of program name, signal get delivered to both of process, and the secondary process, which is created because i had used "use forks;" causes my program to terminate.

My requirement that, is there anyway through which i can register a handle for this secondary process, which do nothing when it receives signal. In a simple means overriding the default behaviour of process's for USR1 Signal.

Thanks in Advance

Replies are listed 'Best First'.
Re: sending signal to forked process
by almut (Canon) on Apr 07, 2010 at 14:30 UTC

    I think using forks only complicates matters unnecessarily (as opposed to plain forking), because forks goes to great lengths to install signal handler wrappers as a tied %SIG hash etc. (see the source of the for-internal-use-only forks::signals in case you're interested in the details).  In other words, unless you already have code implemented for the threads API, you might reconsider using plain forking instead.

    Anyhow, there is an environment variable THREADS_DAEMON_MODEL which you can set to reverse the child-parent relationship between the main process and the secondary "thread state" control process of forks. This makes things somewhat easier, as you can then install an IGNORE signal handler that applies to both of these processes. Something like this:

    #!/usr/local/bin/perl -l BEGIN { # reverses the parent-child relationship # between main and "thread state" process $ENV{THREADS_DAEMON_MODEL} = 1; setpgrp; # for sending signals to our process group } use forks; $SIG{USR1} = 'IGNORE'; my @procs; for (1..3) { # fork a few processes push @procs, threads->new( sub { $SIG{USR1} = sub { print "hi! from 'thread' #$_" }; print "running 'thread' #$_ (pid=$$) ..."; sleep 3; } ); } sleep 1; my $ps = "ps fT -o pid,ppid,pgrp,cmd"; print "\ninitial processes:"; system $ps; sleep 1; my $pgrp = getpgrp; print "\nsending process group $pgrp USR1 signal"; kill -10, $pgrp; $_->join() for @procs; sleep 1; print "\nprocesses after join()"; system $ps; __END__ $ ./833279.pl running 'thread' #1 (pid=24090) ... running 'thread' #2 (pid=24091) ... running 'thread' #3 (pid=24092) ... initial processes: PID PPID PGRP CMD 25932 25930 25932 bash -rcfile .bashrc 24088 25932 24088 \_ /usr/local/bin/perl -l ./833279.pl 24089 24088 24088 \_ /usr/local/bin/perl -l ./833279.pl 24090 24089 24088 \_ /usr/local/bin/perl -l ./833279.pl 24091 24089 24088 \_ /usr/local/bin/perl -l ./833279.pl 24092 24089 24088 \_ /usr/local/bin/perl -l ./833279.pl 24093 24089 24088 \_ ps fT -o pid,ppid,pgrp,cmd sending process group 24088 USR1 signal hi! from 'thread' #1 hi! from 'thread' #2 hi! from 'thread' #3 processes after join() PID PPID PGRP CMD 25932 25930 25932 bash -rcfile .bashrc 24088 25932 24088 \_ /usr/local/bin/perl -l ./833279.pl 24089 24088 24088 \_ /usr/local/bin/perl -l ./833279.pl 24094 24089 24088 \_ ps fT -o pid,ppid,pgrp,cmd

    As you can see, the two processes in question (24088 = control process, 24089 = main process) "survived", despite the signal having been sent to the entire process group.

      Thanks almut, for such an insight, I am agree with you, using plain fork is better in every manner..
Re: sending signal to forked process
by kiruthika.bkite (Scribe) on Apr 07, 2010 at 13:18 UTC
    Is this you are expecting,
    my $pid; sub handler { if($pid==0) { print "sig",shift,"received\n"; } else { $SIG{'INT'}='DEFAULT'; } } $SIG{'INT'}=\&handler; $pid=fork(); if($pid==0) { print "child\n"; sleep(10); } else { wait(); print "parent\n"; sleep(30); }
      Thanks...i got it solved :)