Semaphores


Semaphores are often considered (and used) as an on-off flag. They are actually implemented as short counters that contain positive values. Think of them as if each contained a count of the number of "things" available for use. User programs can use them to control common resources (files or shared memory areas) not already controlled by the kernel.

Semaphores are allocated in sets. Each set has one ID number ( semid ) associated with it. A user program, when it wants a semaphore, must " open " a previously created set. Many semaphores can be in one set, subject to some tuning parameters. The semaphore number itself (the index into the set) is an unsigned short, which will allow over 65,000 semaphores to be associated with one ID. Each semaphore is kept as an unsigned short value. Generally you "request items" from a semaphore by decrementing it, and you release/restore those items by incrementing. A request will cause the process to be blocked if the value in the semaphore is not big enough. The request size can be added to a semaphore "undo" value when a request succeeds. This undo number is added to the semaphore value if the process exits and the semaphores are released, which prevents a semaphore from keeping a count low after the requestor is long gone.

Tunable parameters for semaphores

For Solaris 2, some parameters are described in Appendix A of the manual Administering Security, Performance, and Accounting . This appendix lists the variables that can be adjusted using the /etc/system file. These values are stored into the seminfo structure and used to initialize the semaphore tables when the semsys kernel module is first loaded. (These tables and maps will not be deallocated when the module is unloaded.)

The variables are not really well defined in the manual, so we'll go over them here.

  • seminfo_semmap ” Defines the size of the free space map for the actual semaphore structures, which come from a common pool. This map (see the map.h header file in /usr/include/sys for a description of resource maps) identifies contiguous blocks of free semaphore structures in the pool, so the maximum size you really need is half the total number of semaphores in the system (to handle the worst case: every other structure in the pool is in use).

  • seminfo_semmni ” Defines the number of semaphore identifiers that can exist in the system. This variable controls the total number of sets available or the maximum number of semaphore groups that can be declared simultaneously .

  • seminfo_semmns ” Is the total number of semaphores in the system (the size of the pool of semaphore structures).

  • seminfo_semmnu ” Declares the total number of undo structures in the kernel. Each structure will be associated with one semaphore that has been updated by a process (or thread) using the sem_undo flag with that operation.

  • seminfo_semmsl ” Gives the maximum number of semaphores per ID, or the set size. In the SunOS 4.x kernel, seminfo_semmsl is given as a short, allowing up to 32,767 semaphores in one set. In Solaris 2, this has been changed to an unsigned short. It's still probably best to keep seminfo_semmsl to a reasonable value.

  • seminfo_semopm ” Defines the maximum number of simultaneous operations that can be performed on a set of semaphores (i.e., the total number of semaphores you can update or check in one semop system call.)

  • seminfo_semume ” Is the maximum number of undo entries per process. A table of undo entries is constructed for each process that wants to perform undo operations.

  • seminfo_semusz ” Is the size in bytes of the sem_undo structure. This is listed as a tunable parameter but is rather pointless to change. This variable is set by default to the size of the undo entry itself times the number of entries per process ( seminfo_semume ) added to the basic size of the sem_undo structure.

  • seminfo_semvmx ” Set by default to 32767. This is the maximum semaphore value. Since this is an unsigned short value, it could conceivably be set as high as 65535.

  • seminfo_semaem ” Is the maximum value allowed for the undo structure (adjust-on-exit number). Any attempt to have a semaphore operation modify this to a value larger than 16384 will result in an error.

Every structure in the kernel that is allocated for semaphores is based on these values. An idea of the various sizes of these structures can be obtained from the sem.h header file. Most of the structures are not very large.

For a SunOS 4.x system, there are fewer tunables, but they control basically the same parameters.

The tunables are documented in the System and Network Administration manual under "Advanced Administration Topics, Tuning IPC System Parameters." These values are set in the kernel configuration file with options statements, in the form

  options NAME=value  

These values are:

  • SEMMNI ” Maximum number of sets

  • SEMMNS ” Total number of semaphores in the system

  • SEMUME ” Maximum number of undo entries per process with non-zero values

  • SEMMNU ” Maximum number of processes that can have undo's

The other existing parameters are considered to be untunable and are fixed constants.

Internal variables

The sem.h include file also defines some global variables that enable you to find these structures in the kernel.

  • struct semid_ds *sema ” Pointer to the pool of semaphore id structures, which contain the pointer to the semaphores themselves .

  • struct sem *sem ” Address of the semaphore pool.

  • struct map *semmap ” Map pointer, indicating the semaphore allocation map.

  • struct sem_undo **sem_undo ” Pointer to a list of per process undo tables.

  • int *semu ” The actual undo structure pool. (It's not really a pointer to an integer.)

  • struct seminfo seminfo ” Contains the tunable information.

  • struct sem_undo *semunp ” Is declared only in the Solaris 2 header file as a pointer to the head of an undo chain. This variable and the next both exist in SunOS 4.x.

  • struct sem_undo *semfup ” Pointer to the undo free chain.

Internal structures

The semaphore structures are all defined in the sem.h include file. You have one semid_ds structure per set, one sem structure per semaphore, and one sem_undo structure for every process that is using a backout, or semadj , value. This sem_undo structure contains an array of undo structures, each of which contains a value for adjust-on-exit, a semaphore ID to define the set it applies to, and a semaphore number to identify the individual semaphore in that set.

Each sem_undo structure contains a fixed-length array of undo entries, even though not all of them may be used.

Functions

The following functions appear in both SunOS 4.x and Solaris 2 to handle various semaphore operations, including the system calls, initialization, and cleanup.

  • seminit() ” Takes no parameters and returns nothing. It is responsible for semaphore table initialization, map setup, and space reservations . In Solaris 2, this function takes the tunable parameters and sets the seminfo structure accordingly .

  • semexit() ” Cleans up and handles undo operations when a process exits.

  • semsys() ” Is the system call entry point. It handles initial processing of the user arguments. In SunOS 4.x, these arguments are obtained from the user structure. In Solaris 2, a pointer to a "user argument structure" is passed in as a parameter, along with a location for the return value. In SunOS 4.x, the return value is placed, again, in the user structure.

  • semctl () , semget() , and semop() ” Performs the actual processing for the specific system calls. In SunOS 4.x, they take no parameters. In Solaris, they receive the user argument and return value pointers.

  • semaoe() ” Updates the adjust-on-exit value in an undo entry.

  • semunrm() ” Clears out undo entries that belong to semaphores being removed or globally reset.

  • semundo() ” Backs out any semaphore operations that have been done when an error is detected .

  • semconv() ” Converts a user-supplied semid into a pointer to the actual semaphore header.



PANIC. UNIX System Crash Dump Analysis Handbook
PANIC! UNIX System Crash Dump Analysis Handbook (Bk/CD-ROM)
ISBN: 0131493868
EAN: 2147483647
Year: 1994
Pages: 289
Authors: Chris Drake

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net