Semaphore functions in PHP

Ruby Lin's picture

Semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment.

A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e. without race conditions) adjust that record as units are required or become free, and if necessary wait until a unit of the resource becomes available.

Semaphores are a useful tool in the prevention of race conditions and deadlocks; however, their use is by no means a guarantee that a program is free from these problems. Semaphores which allow an arbitrary resource count are called counting semaphores, whilst semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores.

The following are semaphore functions in PHP:
int ftok (string $pathname, string $proj) - Convert a pathname and a project identifier to a System V IPC key.

sem_acquire (resource $sem_identifier) - Acquire a semaphore.

resource sem_get (int $key [,int $max_acquire = 1 [,int $perm = 0666 [,int $auto_release = 1]]]) - Get a semaphore id.

bool sem_release (resource $sem_identifier) - Release a semaphore.

bool sem_remove (resource $sem_identifier) - Remove a semaphore.

resource shm_attach (int $key [, int $memsize [, int $perm]]) - Creates or open a shared memory segment.

bool shm_detach (resource $shm_identifier) - Disconnects from shared memory segment.

mixed shm_get_var (resource $shm_identifier, int $variable_key) - Returns a variable from shared memory.

bool shm_has_var (resource $shm_identifier, int $variable_key) - Check whether a specific entry exists.

bool shm_put_var (resource $shm_identifier, int $variable_key, mixed $variable) - Inserts or updates a variable in shared memory.

bool shm_remove_var (resource $shm_identifier, int $variable_key) - Removes a variable from shared memory.

bool shm_remove (resource $shm_identifier) - Removes shared memory from Unix systems.