[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Base Operating System and Extensions Technical Reference, Volume 2

shmget Subroutine

Purpose

Gets shared memory segments.

Library

Standard C Library (libc.a)

Syntax

#include <sys/shm.h>

int shmget (Key, Size, SharedMemoryFlag)
key_t Key;
size_t Size
int SharedMemoryFlag;

Description

The shmget subroutine returns the shared memory identifier associated with the specified Key parameter.

The following limits apply to shared memory:

Parameters

Key Specifies either the IPC_PRIVATE value or an IPC key constructed by the ftok subroutine (or by a similar algorithm).
Size Specifies the number of bytes of shared memory required.
SharedMemoryFlag Constructed by logically ORing one or more of the following values:
IPC_CREAT Creates the data structure if it does not already exist.
IPC_EXCL Causes the shmget subroutine to be unsuccessful if the IPC_CREAT flag is also set, and the data structure already exists.
S_IRUSR Permits the process that owns the data structure to read it.
S_IWUSR Permits the process that owns the data structure to modify it.
S_IRGRP Permits the group associated with the data structure to read it.
S_IWGRP Permits the group associated with the data structure to modify it.
S_IROTH Permits others to read the data structure.
S_IWOTH Permits others to modify the data structure.

Values that begin with the S_I prefix are defined in the sys/mode.h file and are a subset of the access permissions that apply to files.

A shared memory identifier, its associated data structure, and a shared memory segment equal in number of bytes to the value of the Size parameter are created for the Key parameter if one of the following is true:

  • The Key parameter is equal to the IPC_PRIVATE value.
  • The Key parameter does not already have a shared memory identifier associated with it, and the IPC_CREAT flag is set in the SharedMemoryFlag parameter.

Upon creation, the data structure associated with the new shared memory identifier is initialized as follows:

  • The shm_perm.cuid and shm_perm.uid fields are set to the effective user ID of the calling process.
  • The shm_perm.cgid and shm_perm.gid fields are set to the effective group ID of the calling process.
  • The low-order 9 bits of the shm_perm.mode field are set to the low-order 9 bits of the SharedMemoryFlag parameter.
  • The shm_segsz field is set to the value of the Size parameter.
  • The shm_lpid, shm_nattch, shm_atime, and shm_dtime fields are set to 0.
  • The shm_ctime field is set to the current time.
    Note: Once created, a shared memory segment is deleted only when the system reboots or by issuing the following shmctl command:
    if (shmctl (id, IPC_RMID, 0) == -1)
     perror ("error in closing segment"),exit (1);

Return Values

Upon successful completion, a shared memory identifier is returned. Otherwise, the shmget subroutine returns a value of -1 and sets the errno global variable to indicate the error.

Error Codes

The shmget subroutine is unsuccessful if one or more of the following are true:

EACCES A shared memory identifier exists for the Key parameter, but operation permission as specified by the low-order 9 bits of the SharedMemoryFlag parameter is not granted.
EEXIST A shared memory identifier exists for the Key parameter, and both the IPC_CREAT and IPC_EXCL flags are set in the SharedMemoryFlag parameter.
EINVAL A shared memory identifier does not exist and the Size parameter is less than the system-imposed minimum or greater than the system-imposed maximum.
EINVAL A shared memory identifier exists for the Key parameter, but the size of the segment associated with it is less than the Size parameter, and the Size parameter is not equal to 0.
ENOENT A shared memory identifier does not exist for the Key parameter, and the IPC_CREAT flag is not set in the SharedMemoryFlag parameter.
ENOMEM A shared memory identifier and associated shared memory segment are to be created but the amount of available physical memory is not sufficient to meet the request.
ENOSPC A shared memory identifier will be created, but the system-imposed maximum of shared memory identifiers allowed will be exceeded.

Implementation Specifics

This subroutine is part of Base Operating System (BOS) Runtime.

Related Information

The ftok subroutine, mmap subroutine, munmap subroutine, shmat subroutine, shmctl subroutine, shmdt subroutine.

List of Memory Manipulation Services, Subroutines Overview, Understanding Memory Mapping in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]