The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Fork::Utils - set of usefull methods to work with processes and signals on Linux platform


    use Fork::Utils qw/ safe_exec /;
    use POSIX ();

    my $sig_action = sub { printf("SIG%s was received\n", $_[0]); };

    $SIG{TERM} = $SIG{INT} = $SIG{QUIT} = $SIG{ALRM} = $sig_action;


    my $result = safe_exec(
        args => [ @params ],
        code => sub {

            my @args = @_;

            my $pending_sigset = new POSIX::SigSet ();


            if ( POSIX::sigpending( $pending_sigset ) == -1 ) {
              die("sigpending error has occurred");

            if ( $pending_sigset->ismember( POSIX::SIGTERM ) ) {
                printf("%s is pending\n", 'SIGTERM');

            if ( $pending_sigset->ismember( POSIX::SIGINT ) ) {
                printf("%s is pending\n", 'SIGINT');

            if ( $pending_sigset->ismember( POSIX::SIGQUIT ) ) {
                printf("%s is pending\n", 'SIGQUIT');

            if ( $pending_sigset->ismember( POSIX::SIGALRM ) ) {
                printf("%s is pending\n", 'SIGALRM');
        sigset => [qw/ ALRM TERM INT QUIT /]

    if (my $error = $@) {
        STDERR->print("Error: $error\n");


    printf("Good bye\n");

The possible output of program is shown below (just press Ctrl+c during the execution to get this certain output):

    SIGINT is pending
    SIGALRM is pending
    SIGINT was received
    SIGALRM was received
    Good bye


This package provides some methods that can be helpfull while working with sub-processes and signals.


Gets a hash with arguments, one of them is code reference is required to be executed in safe context. "Safe context" means context which can't be accidently interrupted by some signals.

This method receives list of signals required to be blocked while code execution. Once code is executed the original signal mask will be restored.

Any signal (except KILL, STOP) can be blocked.

The signal names can be taken from $Config{'sig_names'}.

Returns a result of mentioned code reference as "$code->( @$args )". Be aware that in current implementation this method can't return the list. The return value looks like the one shown below:

    my $result = $code->( @$args );

In case of any error in the executed code reference the standard $@ variable will be set.

  it's a code reference to be executed in safe context
  it's an array reference of arguments required to be passed into C<code> (see above)
  it's an array reference of signal names to be blocked while executing the C<code> (see above)
  It's a flag, by default it's turned off.
  If it's off than passed signals will be added to the current signal mask,
  otherwise mask will be replaced with new one built with mentioned signals


Chernenko Dmitiry


This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0).