What are semaphores and how are they caused?

I recently started having the problem that my Apache crashed and could not be restarted. The hosting company then told me that it has to do with 'semaphores' and sent me this snipped with which they solved the problem:

/usr/bin/ipcrm sem $(/usr/bin/ipcs -s | grep www-data | awk '{print$2}')

Now that's nice to have a command to execute that solves my problem but then again I have no clue what this is all about.

What are semaphores and who the heck puts them, where they are and how do they crash my apache?

I'd be really glad for some general explanations!


As others have said semaphores are IPC (interprocess communications structures). Semaphores like all IPC are used to allow different processes to communicate with each other.

They are basically counters that are created, accessed and destroyed using special system calls, such as sempost(3), semwait(3), semget(2) and semop(2). See sem_overview(7) on a linux system for a brief description.

The definition of communicate here is pretty primitive. "Communicate" for semaphores means reading, incrementing or decrementing a counter via the system/library calls mentioned above.

The special thing about semaphores apart from the fact that they are is that only one process at a time can perform an operation on them, and the semaphore operations are guaranteed atomic, that is to say you can't get into a race condition over a semaphore as the kernel will not swap out a process that is performing a semaphore operation.

The other special thing is that they are created in shared memory which allows multiple processes to access them.

How they manifest/created is that programs create them using semget(2). E.g. apache creates sempahores when it runs.

ipcs -l will tell you about the system's ipc resources.

You can manipulate some system semaphore and ipc related limits with sysctls. Try sysctl kernel.sem to view the sempahore related settings via sysctl. If you want to persist any sysctl changes you try put them into /etc/sysctl.conf.


Semaphores are a form of inter-process communitaction (the ipc in ipcrm). They are provided by the operating system, and the developers of Apache use them for communication between different Apache processes. They are unlikely to be the cause of Apache crashing, but they are not released when it does crash, preventing new instances of Apache from starting.

Wikipedia: Semaphore (programming)