Shared memory


Surprisingly enough, shared memory is exactly what it sounds like ” common memory shared between processes. This is, however, special memory that is allocated deliberately for this purpose; a chunk of swap space (so called "anonymous memory") is reserved and is mmap 'ed in to whatever process requests this shared memory. As with semaphores and messages, a structure defines the ownership and permissions associated with this particular area. When a shared memory area is mapped in, the pages are mapped directly into the process address space. If another process maps that same shared area in as well, the pages are double-mapped: there is exactly one copy of the data, and anybody that touches it will have an immediate effect on everybody else's view of that data.

Tunable parameters for shared memory

Very few parameters are actually available, because little data is kept in the kernel other than the description of the memory area and some bookkeeping information. The tunable parameters are simply there to keep a user process from running wild and chewing up the entire system swap space.

For both versions of the kernel, the parameters perform basically the same functions. For Solaris 2:

  • shmmax ” Defines the maximum shared memory segment size , in bytes. The documentation indicates that this is set to 128 kilobytes, but in reality the default is 1 megabyte.

  • shmmin ” Indicates the minimum shared memory segment size. The default is 1, which is about as small as you can get.

  • shmmni ” Specifies the maximum number of shared memory identifiers. This parameter defines the size of the pool of shmid_ds structures available to describe shared memory segments.

  • shmseg ” Puts an upper limit on the number of attached shared memory segments per process. The default is 6. This number is checked only when a process attempts to attach another one. A current counter is kept in the user structure.

For SunOS 4.x, there are even fewer tunable parameters.

  • SHMSIZE ” Defines the maximum segment size (in kilobytes), 1 megabyte by default.

  • SHMMNI ” Indicates the total number of shared memory description structures in the kernel.

  • SHMMIN ” Is defined as 1, assumed not to require any tuning.

  • SHMMAX (the size in bytes) ” Is defined from SHMSIZE (the size in kilobytes).

There is no maximum number of segments per process in SunOS 4.x.

Internal variables

Since shared memory is considerably simpler to manage than some of the other IPC facilities, very few variables are of interest: shminfo is the structure that contains the tunable parameter values, and shmem points to the pool of descriptor structures.

Internal structures

The shmid_ds structure, defined in the shm.h header file, contains all the information about the shared memory area. It includes a pointer to the anon_map structure, which tells the system how to actually locate the swap space dedicated to the particular shared memory chunk.

Functions

The internal functions differ slightly between the two releases because the SunOS 4.x virtual memory system is slightly less sophisticated than that in Solaris 2 and more careful checking needs to be done in some places. The Solaris 2 version includes more accounting and some differences in the deletion of a shared memory segment. In addition, for compatibility, SunOS 4.x would allow a request with an address of zero under some circumstances to mean literally a mapping address into the first page, at location 0, rather than indicating that the system could put the page where it liked . This "feature" does not appear in Solaris 2.

The SunOS 4.x routines are:

  • shmat() ” Handles attach operations. shmat() immediately calls kshmat().

  • kshmat() ” Performs the real work.

  • shmconv() ” Converts the shmid value into a pointer to the descriptive structure.

  • shmctl() ” Handles the control system call. It promptly calls kshmctl().

  • kshmctl() ” Handles control functions from the shmctl system call.

  • shmdt() ” Handles detaches by calling the internal version, kshmdt() .

  • kshmdt() ” Performs detach operations.

  • shmget () ” Is another front end to the real function: kshmget() .

  • kshmget() ” Performs the shmget system call to allocate new shared memory segments.

  • shmsys() ” Is the front end to all the shared memory system calls: shmat() , shmdt() , shmget() , and shmctl() .

  • shm_vmlookup() ” Is an internal function to check for possibly conflicting virtual memory segments (to avoid hooking up the wrong one).

In Solaris 2, we have the following internal functions.

  • shminit() ” Performs the memory allocation request for the pool of shm_ds structures.

  • shmconv() ” Performs conversion of an ID number into a pointer to a header.

  • shmat() ” Attaches a segment to a process.

  • shmctl() ” Handles shmctl() system call functions.

  • shmdt() ” Detaches a segment from a process address space.

  • kshmdt() ” Is a common routine to do detaches. This can be performed with a detach call or by exiting.

  • shmget() ” Handles the system call to create a segment or find an existing one.

  • shmsys() ” Is the front-end checker for all the system calls.

  • sa_add() ” Adds this record to the segacct list, which is kept in the proc structure. This is a linked list of segacct structures.

  • sa_del() ” Deletes this record from the segacct list.

  • sa_find() ” Searches the list of accounting structures and finds the one that corresponds to this address.

  • shmfork() ” Duplicates the parent's segacct records in the child. The new process has access to the same segments that the parent has.

  • shmexit() ” Detaches all shared memory segments from a process doing an exit.

  • shm_rm_amp() ” Removes all references to this anonymous memory. This means all processes have detached it and an IPC_RMID request has been issued.

One of the allowed control functions is to lock the pages in memory. The following functions deal with locking and unlocking the segment.

  • lock_again() ” Pages should be in memory, so just lock them down.

  • check_locked() ” Checks to see if this segment is already locked.

  • shmem_lock() ” Attaches the shared memory segment to the process address space and locks the pages.

  • shmem_unlock() ” Unlocks this shared memory segment.



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