IPC::Shareable is a perl module that provides variables sharing between perl processes. It uses the same IPC utilities which described in How to create, check and delete IPC share memory Sempahare and message queue on Linux.

Thus, you can also use ipcs, ipcrm commands to manipulate perl shared memory segments.

1. Package info

The package is available in most of linux distro, on RHEL/SL/CentOS

$ rpm -ql perl-IPC-Shareable

2. How to use it in perl

First, in you perl script, include the module first.

use IPC::Shareable;

Then, tie a variable(scalar, hash or array) to a shared memory segment.

IPC::Shareable allows you to tie a variable to shared memory making it easy to share the contents of that variable with other Perl processes. Scalars, arrays, and hashes can be tied. The variable being tied may contain arbitrarily complex data structures - including references to arrays, hashes of hashes, etc.

For example, in program1

tie $var_scalar, 'IPC::Shareable', 'S_s1';
tie %var_hash, 'IPC::Shareable', 'H_v1';
tie @var_array, 'IPC::Shareable', 'A_a1';

Use it in the program as regular variable

In other program2, use

tie $share_var, 'IPC::Shareable', 'S_s1';

The variable $share_var uses the same share memory segment with the first program.

The association between variables in distinct processes is provided by GLUE. This is an integer number or 4 character string, in above case, 'S_s1' is the GLUE to link the shared memory segment between program1 and program2.

3. Options play

IPC::Shareable also provides Options that can be passed to tie function that enchants a variable.

The following fields are recognized in the options hash.


The key field is used to determine the GLUE when using the three-argument form of the call to tie(). This argument is then, in turn, used as the KEY argument in subsequent calls to shmget() and semget().

The default value is IPC_PRIVATE, meaning that your variables cannot be shared with other processes.


create is used to control whether calls to tie() create new shared memory segments or not. If create is set to a true value, IPC::Shareable will create a new binding associated with GLUE as needed. If create is false, IPC::Shareable will not attempt to create a new shared memory segment associated with GLUE. In this case, a shared memory segment associated with GLUE must already exist or the call to tie() will fail and return undef. The default is false.


If exclusive field is set to a true value, calls to tie() will fail (returning undef) if a data binding associated with GLUE already exists. If set to a false value, calls to tie() will succeed even if a shared memory segment associated with GLUE already exists. The default is false


The mode argument is an octal number specifying the access permissions when a new data binding is being created. These access permission are the same as file access permissions in that 0666 is world readable, 0600 is readable only by the effective UID of the process creating the shared variable, etc. The default is 0666 (world readable and writable).


If set to a true value, the shared memory segment underlying the data binding will be removed when the process calling tie() exits (gracefully)[3]. Use this option with care. In particular you should not use this option in a program that will fork after binding the data. On the other hand, shared memory is a finite resource and should be released if it is not needed. The default is false


This field may be used to specify the size of the shared memory segment allocated. The default is IPC::Shareable::SHM_BUFSIZ().

Default values for options are

 key       => IPC_PRIVATE,
 create    => 0,
 exclusive => 0,
 destroy   => 0,
 mode      => 0,
 size      => IPC::Shareable::SHM_BUFSIZ(),


my %option =(
 create    => 1,
 exclusive => 0,
 destroy   => 1,
 mode      => 0600,
 size      => IPC::Shareable::SHM_BUFSIZ(),

tie our %s_mover , 'IPC::Shareable', 'op_mover', \%option ;

4. Locking between processes

IPC::Shareable provices two methods shlock() and shunlock() to support application level shared memory locking.

To use them you must first get the object underlying the tied variable

$mover_handle = tie our %s_mover , 'IPC::Shareable', 'op_mover', \%option ;

Lock the shared memory


Unlock the shared memory


4. How to remove it

IPC::Shareable provides three methods to remove shared memory segment

 remove(), clean_up(), and clean_up_all()

and the destroy option to tie() to remove shared memory segment

Calling remove() on the object underlying a tie()d variable removes the associated shared memory segment

 (tied VARIABLE)->remove;

To remove all shared memory segments created by the process


To remove all shared memory segments encountered by the process. Segments are removed even if they were not created by the calling process


Specifying the destroy option when tie()ing a variable coerces IPC::Shareable to remove the underlying shared memory segment when the process calling tie() exits gracefully. Note that any related shared memory segments created automagically by the use of references will also be removed.

Look this shared memory

 tie $variable, 'IPC::Shareable', 'data' \%options;

create => 1, exclusive => 0, destroy => 1, mode => 0, size => IPC::Shareable::SHM_BUFSIZ()

The field destroy is set to '1', then untie function will remove the shared memory segmen

5.Clear the shareable memory by system level

Use ipcrm, it is also recommended to use ipcrm to clear out shared memory segment if you experience perl IPC::Shareable buggy thing.

ipcrm -m <shmid>

6. Special note about GLUE

The association between variables in distinct processes is provided by GLUE, it is an integer number or 4 character string.

Hense, if GLUE is longer than 4 characters, only the 4 most significant characters are used. These characters are turned into integers by unpack()ing them. If GLUE is less than 4 characters, it is space padded.

The following two shareable variable are tied to one shared memory segment.

tie $variable, 'IPC::Shareable', 'data_a' \%options;
tie $variable, 'IPC::Shareable', 'data_b' \%options;

What to know how to avoid shared memory related permission see How to fix perl Shareable Memeory permission issue

Comments powered by CComment