Semaphores PHP

PHP offers an extension for using System V semaphores. If your operating system supports this feature, you may add this extension to your installation of PHP. At the time of this writing, only the Solaris, Linux, and AIX operating systems were known to support semaphores. Semaphores are a way to control a resource so that it is used by a single entity at once, and they were inspired by the flags used to communicate between ships. The idea to use an integer counter to ensure single control of a resource was described first by Edsger Dijkstra in the early 1960s for use in operating systems. Semaphores are a standard topic for college computer science courses, and you will find adequate descriptions in books about operating systems.

boolean sem_acquire(integer identifier)
The sem_acquire function attempts to acquire a semaphore you've identified with the sem_get function. The function will block until the semaphore is acquired. Note that it is possible to wait forever while attempting to acquire a semaphore. One way is if a script acquires a semaphore to its limit and then tries to acquire it another time. In this case the semaphore can never decrement.
If you do not release a semaphore with sem_release, PHP will release it for you and display a warning.

<?
/*
** Semaphore example
**
** To see this in action, try opening two or more
** browsers and load this script at the same time.
** You should see that each script will execute the
** fake procedure when it alone has acquired the
** semaphore. Pay attention to the output of the
** microtime function in each browser window.
*/
//Define integer for this semaphore
//This simply adds to readability
define("SEM_COREPHP", 1970);
//Get or create the semaphore
//This semaphore can be acquired only once
$sem = sem_get(SEM_COREPHP, 1);
//acquire semaphore
if(sem_acquire($sem))
{
//perform some atomic function
print("Faking procedure... " . microtime());
sleep(3);
print("Finishing fake procedure... " .
microtime());
//release semaphore
sem_release($sem);
}
else
{
//we failed to acquire the semaphore
print("Failed to acquire semaphore!<BR> ");
}
?>

integer sem_get(integer key, integer maximum, integer permission)
Use sem_get to receive an identifier for a semaphore. If the semaphore does not exist, it will be created. The optional maximum and permission arguments are used only during creation. The maximum argument controls how many times a semaphore may be acquired. It defaults to 1. The permission argument controls read and write privileges to the semaphore in the same way file permissions do. It defaults to 0x666, which is read and write access for all users. The key argument is used to identify the semaphore among processes in the system. The integer returned by sem_get may be unique each time it is called, even when the same key is specified.

boolean sem_release(integer identifier)
Use sem_release to reverse the process of the sem_acquire function

« Previous Topics
Ldap
Java
Imap
Next Topics »
Shared Memory
Snmp
Wddx

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics