Previous | Table of Contents | Next

Page 852

    time_t shm_atime; /* last attach time */
    time_t shm_dtime; /* last detach time */
    time_t shm_ctime; /* last change time */
    unsigned short shm_cpid; /* pid of creator */
    unsigned short shm_lpid; /* pid of last operator */
    short shm_nattch; /* no. of current attaches */
};

struct ipc_perm
{
    key_t key;
    ushort uid; /* owner euid and egid */
    ushort gid;
    ushort cuid; /* creator euid and egid */
    ushort cgid;
    ushort mode; /* lower 9 bits of shmflg */
    ushort seq; /* sequence number */
};

Furthermore, while creating, the system call initializes the system shared memory segment data structure shmid_ds as follows:

  • shm_perm.cuid and shm_perm.uid are set to the effective user ID of the calling process.
  • shm_perm.cgid and shm_perm.gid are set to the effective group ID of the calling process.
  • The lowest-order 9 bits of shm_perm.mode are set to the lowest-order 9 bit of shmflg.
  • shm_segsz is set to the value of size.
  • shm_lpid, shm_nattch, shm_atime, and shm_dtime are set to 0.
  • shm_ctime is set to the current time.

If the shared memory segment already exists, the access permissions are verified, and a check is made to see if it is marked for destruction.

System Calls

fork() After a fork(), the child inherits the attached shared memory segments.
exec() After an exec(), all attached shared memory segments are detached (not destroyed).
exit() On exit(), all attached shared memory segments are detached (not destroyed).

RETURN VALUE

A valid segment identifier, shmid, is returned on success, _1 on error.

ERRORS

On failure, errno is set to one of the following:

EINVAL Returned if SHMMIN is greater than size, if size is greater than SHMMAX, or if size is greater than the size of the segment.
EEXIST Returned if IPC_CREAT | IPC_EXCL was specified and the segment exists.
EIDRM Returned if the segment is marked as destroyed or was removed.
ENOSPC Returned if all possible shared memory IDs have been taken (SHMMNI) or if allocating a segment of the requested size would cause the system to exceed the system-wide limit on shared memory (SHMALL).
ENOENT Returned if no segment exists for the given key, and IPC_CREAT was not specified.
EACCES Returned if the user does not have permission to access the shared memory segment.
ENOMEM Returned if no memory could be allocated for segment overhead.

Page 853

NOTES

IPC_PRIVATE isn't a flag field but a key_t type. If this special value is used for key, the system call ignores everything but the lowest order 9 bits of shmflg and creates a new shared memory segment (on success).

The following are limits on shared memory segment resources affecting a shmget call:

SHMALL System-wide maximum of shared memory pages; policy dependent.
SHMMAX Maximum size, in bytes, for a shared memory segment; implementation dependent (currently 4MB).
SHMMIN Minimum size, in bytes, for a shared memory segment; implementation dependent (currently 1 byte, although PAGE_SIZE is the effective minimum size).
SHMMNI System-wide maximum number of shared memory segments; implementation dependent (currently 4096).

The implementation has no specific limits for the per-process maximum number of shared memory segments (SHMSEG).

BUGS

Use of IPC_PRIVATE does not inhibit other processes' access to the allocated shared memory segment.

As for the files, there is currently no intrinsic way for a process to ensure exclusive access to a shared memory segment. Asserting both IPC_CREAT and IPC_EXCL in shmflg only ensures (on success) that a new shared memory segment will be created; it doesn't imply exclusive access to the segment.

SEE ALSO

ftok(3), ipc(5), shmctl(2), shmat(2), shmdt(2)

Linux 0.99.11, 28 November 1993

shmop

shmop—Shared memory operations

SYPNOSIS

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
char *shmat ( int shmid, char *shmaddr, int shmflg );
int shmdt ( char *shmaddr);

DESCRIPTION

The function shmat attaches the shared memory segment identified by shmid to the data segment of the calling process. The attaching address is specified by shmaddr with one of the following criteria:

  • If shmaddr is 0, the system tries to find an unmapped region in the range 1_1.5GB, starting from the upper value and coming down from there.
  • If shmaddr isn't 0 and SHM_RND is asserted in shmflg, the attach occurs at the address equal to the rounding down of shmaddr to a multiple of SHMLBA. Otherwise, shmaddr must be a page-aligned address at which the attach occurs.

If SHM RDONLY is asserted in shmflg, the segment is attached for reading, and the process must have read access permissions to the segment. Otherwise the segment is attached for read and write, and the process must have read and write access permissions to the segment. There is no notion of a write-only shared memory segment.

The brk value of the calling process is not altered by the attach. The segment will automatically be detached at process exit. The same segment may be attached as a read and as a read-write segment, more than once, in the process's address space.

Page 854

On a successful shmat call, the system updates the members of the structure shmid_ds associated to the shared memory segment as follows:

  • shm_atime is set to the current time.
  • shm_lpid is set to the process ID of the calling process.
  • shm_nattch is incremented by 1.

Note that the attachment will also succeed if the shared memory segment is marked to be deleted.

The function shmdt detaches from the calling process's data segment the shared memory segment located at the address specified by shmaddr. The detaching shared memory segment must be one among the currently attached ones (to the process's address space) with shmaddr equal to the value returned by its attaching shat call.

On a successful shmdt call, the system updates the members of the structure shmid_ds associated to the shared memory segment as follows:

  • shm_dtime is set to the current time.
  • shm_lpid is set to the process ID of the calling process.
  • shm_nattch is decremented by 1. If it becomes 0 and the segment is marked for deletion, the segment is deleted.

The occupied region in the user space of the calling process is unmapped.

System Calls

fork() After a fork(), the child inherits the attached shared memory segments.
exec() After an exec(), all attached shared memory segments are detached (not destroyed).
exit() On exit(), all attached shared memory segments are detached (not destroyed).

RETURN VALUE

On a failure, both functions return _1 with errno indicating the error; otherwise, shmat returns the address of the attached shared memory segment, and shmdt returns 0.

ERRORS

When shmat fails, at return errno will be set to one of the following values:
EACCES The calling process has no access permissions for the requested attach type.
EINVAL Invalid shmid value, unaligned (that is, not page-aligned and SHM_RND was not specified) or invalid shmaddr value, or failing attach at brk.
ENOMEM Could not allocate memory for the descriptor or for the page tables.

The function shmdt can fail only if there is no shared memory segment attached at shmaddr; in such a case, errno will be set to EINVAL at return.

NOTES

On executing a fork(2) system call, the child inherits all the attached shared memory segments.

The shared memory segments attached to a process executing anexec(2) system call will not be attached to the resulting process.

The following is a system parameter affecting a shmat system call:

SHMLBA Segments low-boundary address multiple. Must be page aligned. For the current implementation, the SHMBLA value is PAGE_SIZE.

The implementation has no intrinsic limit to the per-process maximum number of shared memory segments (SHMSEG)

SEE ALSO

ipc(5), shmctl(2), shmget(2)

Linux 0.99.13, 28 November 1993

Previous | Table of Contents | Next

1