8

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!

markus
  • 572
  • 2
  • 8
  • 18

2 Answers2

9

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.

Jason Tan
  • 2,742
  • 2
  • 17
  • 24
4

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)

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
pgs
  • 3,471
  • 18
  • 19
  • There's also a thread on the subject over on StackOverflow: http://stackoverflow.com/questions/34519/what-is-a-semaphore – James Sneeringer May 07 '10 at 05:53
  • There is a mistake in your URL.. The URL is missing the right ) It seems to be a bug in the Serverfault software... – tegbains May 07 '10 at 06:14
  • *grumble* It gets the URL right in the preview, and drops the ) when I save it. – pgs May 07 '10 at 06:28
  • Thanks so far. But what are they, how do they manifest? Are they files, entries in a log? What do they have to do with memory? Can they be caused by bad php programming (as my hosting company told me)? – markus May 07 '10 at 06:59
  • They are shared memory and kernel structures. PHP is not thread-safe, so you should be using Apache2's prefork multi-processing module. – pgs May 07 '10 at 08:45