Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Sempaphores limited in Linux

We just ran out of semaphores on our Linux box, due to the use of too many Websphere Message Broker instances or somesuch.

A colleague and I got to wondering why this is even limited - it's just a bit of memory, right?

I thoroughly googled and found nothing.

Anyone know why this is?

cheers

like image 665
user3010555 Avatar asked Oct 02 '14 10:10

user3010555


People also ask

Does Linux use semaphores?

Semaphore in Linux plays an important role in a multiprocessing system. We need to review the semaphore status if we need to kill unwanted entries to free up the memory allocated to the server.

Why semaphore is used in Linux?

Semaphores are commonly use for two purposes: to share a common memory space and to share access to files. Semaphores are one of the techniques for interprocess communication (IPC). The C programming language provides a set of interfaces or "functions" for managing semaphores.

How do semaphores work in Linux?

DESCRIPTION top. POSIX semaphores allow processes and threads to synchronize their actions. A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)) ...

Can semaphore used in kernel?

The Linux kernel provides an implementation of semaphores that conforms to the above semantics, although the terminology is a little different. To use semaphores, kernel code must include <asm/semaphore. h>. The relevant type is struct semaphore ; actual semaphores can be declared and initialized in a few ways.


1 Answers

Semaphores, when being used, require frequent access with very, very low overhead.

Having an expandable system where memory for each newly requested semaphore structure is allocated on the fly would introduce complexity that would slow down access to them because it would have to first look up where the particular semaphore in question at the moment is stored, then go fetch the memory where it is stored and check the value. It is easier and faster to keep them in one compact block of fixed memory that is readily at hand.

Having them dispersed throughout memory via dynamic allocation would also make it more difficult to efficiently use memory pages that are locked (that is, not subject to being swapped out when there are high demands on memory). The use of "locked in" memory pages for kernel data is especially important for time-sensitive and/or critical kernel functions.

Having the limit be a tunable parameter (see links in the comments of original question) allows it to be increased at runtime if needed via an "expensive" reallocation and relocation of the block. But typically this is done one time at system initialization before anything much is even using semaphores.

That said, the amount of memory used by a semaphore set is rather tiny. With modern memory available on systems being in the many gigabytes the original default limits on the number of them might seem a bit stingy. But keep in mind that on many systems semaphores are rarely used by user space processes and the linux kernel finds its way into lots of small embedded systems with rather limited memory, so setting the default limit arbitrarily high in case it might be used seems wasteful.

The few software packages, such as Oracle database for example, that do depend on having many semaphores available, typically do recommend in their installation and/or system tuning advice to increase the system limits.

like image 73
wojtow Avatar answered Oct 13 '22 13:10

wojtow