Semaphore Functions
PHP Manual


(PHP 4, PHP 5, PHP 7)

shm_attachCreates or open a shared memory segment


resource shm_attach ( int $key [, int $memsize [, int $perm = 0666 ]] )

shm_attach() returns an id that can be used to access the System V shared memory with the given key, the first call creates the shared memory segment with memsize and the optional perm-bits perm.

A second call to shm_attach() for the same key will return a different shared memory identifier, but both identifiers access the same underlying shared memory. memsize and perm will be ignored.



A numeric shared memory segment ID


The memory size. If not provided, default to the sysvshm.init_mem in the php.ini, otherwise 10000 bytes.


The optional permission bits. Default to 0666.

Return Values

Returns a shared memory segment identifier.


Version Description
5.3.0 This function now returns a resource instead of an integer.



This function used to return an integer value prior to PHP 5.3.0. To achieve the same value in a portable manner, the return value can be cast to an integer like:

// Create a temporary file and return its path
$tmp tempnam('/tmp''PHP');

// Get the file token key
$key ftok($tmp'a');

// Attach the SHM resource, notice the cast afterwards
$id shm_attach($key);

if (
$id === false) {
'Unable to create the shared memory segment');

// Cast to integer, since prior to PHP 5.3.0 the resource id 
// is returned which can be exposed when casting a resource
// to an integer
$id = (integer) $id;

See Also

Semaphore Functions
PHP Manual