[asterisk-users] AGI and forking
Tilghman Lesher
tilghman at meg.abyt.es
Wed Apr 13 13:05:43 CDT 2011
On Wednesday 13 April 2011 08:08:03 A J Stiles wrote:
> Hi. I just want to make sure I understand this before doing something
> that might break things spectacularly for our users and customers :)
>
> We are using Asterisk 1.6.2.9 and my programming language of choice is
> Perl.
>
> I want, when a call comes in on someone's DDI number (which the person
> who dialled it can only possibly have obtained by dialling 1471 after
> we called them), to be able to look up the caller's details from one
> of our databases (where the number ought to be stored, because we
> already dialled it).
>
> Now, this search is going to take some time; so I'd like for the AGI
> script to fork a clone of itself, so the parent process can exit and
> the dialplan continue on to ring the person's phone, while the database
> lookup is done in the background (the script doesn't need to have any
> further contact with Asterisk -- it will initiate any necessary future
> communication via other channels).
>
>
> Is this the sort of thing I need?
>
> ########## begin code snippet ##########
>
> #!/usr/bin/perl -w
> use strict;
> use Asterisk::AGI;
>
> my $AGI = new Asterisk::AGI;
> my %params = $AGI->ReadParse();
>
> $SIG{CHLD} = "IGNORE";
>
> if (my $child_pid = fork) {
> # This is executed in the parent process
> exit;
> }
> elsif (defined $child_pid) {
> # This is executed in the child process
>
> close STDIN;
> close STDOUT;
> close STDERR;
>
> # Load some more modules and do some stuff
> # that will take a long time
>
> exit;
> }
> else {
> die "Could not fork: $!";
> };
>
> ########## end code snippet ##########
>
> Am I right in thinking I shouldn't have to worry about zombie processes,
> because the parent exits before the child and the init in modern Linux
> distros is smart enough to deal with orphaned processes itself?
Almost. You should also set a new session ID to ensure that the child gets
a new processgroup. Otherwise, on some systems, it will still wait for the
child to also exit). In Perl, this is accessible from the POSIX module,
function setsid().
--
Tilghman
More information about the asterisk-users
mailing list