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


IPC::Concurrency - Concurrency guard for processes.


Version 0.5


This module allows you to specify how many processes of given kind you want to run in parallel.

May be usefull when you want to prevent machine overload or provide exclusive access to some resource.

This is NOT a forker.

    use IPC::Concurrency;
    my $name = 'PROC';
    my $c = IPC::Concurrency->new($name);
    # your process will end if there are already 4 processes registered as 'PROC' running
    exit unless $c->get_slot(4);
    # otherwise it will run as usual

System requirements

Your system must support SysV IPC shared memory and semaphores as well as kill command.

If you pass the test suite for IPC::ShareLite then you're ready to go :)


Containers are used to name and group processes (like 'PROC' in SYNOPSIS). They are located in shared memory and are accessible by any user.

Containers must be named as exactly 4 characters from A-Z range (uppercase).


Q: Can i change $0 variable?

A: Yes. You can change $0 variable during runtime if you want. Finding amount of running processes of given kind is totally 'ps ux' independent.


Q: Can i use the same $name for processes running on different users?

A: Yes. For example you can restrict them to access some shared device one at a time.


    package Scanner::GUI;
    use IPC::Concurrency;
    my $c = IPC::Concurrency->new('SCNR');
    while (not $c->get_slot(1)) {
        print 'Scanner is busy', $/;
        sleep 4;

This code will wait till every other 'SCNR' processes are not active. It's much more simple approach than grepping 'ps aux' table or creating lockfiles.


Q: Can i register my process under many names?

A: Yes. For example you may want to run no more that 4 parsers and no more than 32 processes on some machine.


    package Parser;
    use IPC::Concurrency;
    my $c1 = IPC::Concurrency->new('PARS');
    my $c2 = IPC::Concurrency->new('GLOB');
    exit unless $c1->get_slot(4) and $c2->get_slot(32);


Q: What is the limit for number of containers?

A: You can make as many containers as your system allows. You will get Carp::confess('No space left on device') if you exceed available memory or semaphores.


Q: What is the limit for number of processes registered in one container?

A: You can request $c2->get_slot(1024) max.


Q: Can i use this module for limiting child processes?

A: Yes. In the following example child process doesn't know how many other child processes have been spawned. But it can use get_slot() to prevent exceeding 10 live child processes.


    package Scraper;
    use IPC::Concurrency;
    my $c1 = IPC::Concurrency->new('SCRA');
    unless ( my $pid = fork() ) {
        exit unless $c1->get_slot(10);



    my $name = 'PROC';
    my $c = IPC::Concurrency->new($name);

Creates new object and allocates Shared Memory container under $name.

Carp::confess will be called on failure.


    $count = 1;
    exit unless $c->get_slot($count);

Request slot. You will get it if there are no more than $count - 1 processes registered under given $name.


Pawel (bbkr) Pabian, <cpan at>

Private website:

Company website:


Please report any bugs or feature requests to bug-ipc-concurrency at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

PID rollover problem

PIDs of processes are stored in containers. Once get_slot() is called it checks how many processes are still active. This is done by sending kill 0, PID signals to all processes on ths list. Not responding PIDS are cleared from container and slot is gained if number of PIDs left is smaller than number of concurrent processes required. That makes logic vulnerable to PIDs rollover.


Encrypt/validate container content.


You can find documentation for this module with the perldoc command.

    perldoc IPC::Concurrency

You can also look for information at:



Copyright 2008 Pawel bbkr Pabian, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.