The integer keys for sem_get() and shm_attach() have to be systemwide unique. There is no method to ensure that no other process on the system will use your specific key (security! and possible malfunction). Also shared memory is very seldom used there are possibilities for conflicts! To see the used id's you can use the program 'ipcs' (at least under SuseLinux;) ). Thanks Christian C.

I was confused by two things that caused strange behaviour in my use of semaphores with php scripts running under apache.

Often enough page requests will end up being filled by the same process as other simultaneous requests. So semaphores will block when you may not have expected.

Also note that sem_remove() will remove it for all processes, not just the calling one. So you have to be sure that the last process running removes the semaphore and none before. I thought there was some failures occurring when my child processes were dropping out with errors.

So you can't just use get, acquire, release, remove in one script that will be hit by a web user. (1) They may end up in the same process and will wait on the other, and (2) the first one to finish will destroy the semaphore for others.

I left out the remove call, and it works ok, but I still wonder if the semaphore is removed by php when the last script that did a get finishes? Also creating a child process to do the work using proc_open works to ensure seperate processes but to be careful you would want to limit the number somehow as well.

As for security, please look at the perm argument to shm_get. Shared Memory blocks has the same permission semantics as unix user/group/other file permissions. As long as your webserver is running as a user that no other users can script to.. and as long as the permissions are set to 600, you should be fine and have no security concerns.

For shared memory needs, these functions are greatly outdated and inefficient.They don't provide read/write locking and they use a single linked list for storing the variables, which means they have an average complexity of O(n/2) where n is the number of variables in the segment.

For a better alternative use APC Functions which implement proper read/write locking and use a hash table so they have good efficiency even for large amounts of variables:http://php.net/manual/en/ref.apc.php

I have been trying to get a php console script and a C application to use a common semaphore for a while. I just got it working, so I thought Id paste the code here incase anyone needs to do this, however, this is not the place for long code examples

I used c code from the php implementation to set up the semaphore set and then mimic the way the php interpreter implements a mutext type locking scheme, using a common semop call.

One has to do the process in the same way as its done in the php implementation, otherwise you run the risk of the php interpreter resetting the semaphore set for you.

The basic idea is.1) sem_get - use a three semaphore set1.1) increment the first sem1.2) check the usage count (sem 3), if only one, set the max_attach using sem 2 for mutex behaviour to sem 3 2) decrement sem 13) for locking / unlocking use the first semaphore, but always call the above from your c-code.

If you want a copy of my code, email me and I'll happily send it to you !

Many (most?) developers use Win32 platform for PHP Web applications development while production servers mostly run Unix/Linux OS. Below is the stub code I use to make it possible to write scripts on Win32 that use semaphores:

Don't use semaphores to serialize access to an undefined number of resources. There is no way (yet) to know before locking if a semaphore is already locked, thus not being able to fully release the semaphore and occupying a semaphore resource for an undefined time.

A possible solution is to build a shared mem pool and store there the current number of locks for a semaphore id.

Here is a simple Mutex class implementation, using semaphore on Linux and flock on windows.Filename is optionnal, but you can provide it anyway. This way the file will be created on windows, not on linux as it is not needed.It's fast written and certainly lacks error checking code.