shmgetのヘルプ・マニュアル
日本語 英語
shmget --help
man shmget
SHMGET(2) Linux Programmer’s Manual SHMGET(2)
名前
shmget - 共有メモリ・セグメントを割り当てる
書式
#include
#include
int shmget(key_t key, size_t size, int shmflg);
説明
shmget() は key 引き数に対応する共有メモリ・セグメントの識別子を返す。
key の値が IPC_PRIVATE の場合、もしくは key に対応する共有メモリ・セ グ
メントが存在せず、 shmflg に IPC_CREAT が指定されていた場合、新しい共有
メモリ・セグメントを作成する。作成される共有メモリ・セグメントは、 size
引き数の値を PAGE_SIZE の倍数へと切り上げた (round up) 大きさとなる。
shmflg に IPC_CREAT と IPC_EXCL の両方が指定された場合、 key に対応する
共有メモリ・セグメントが既に存在すると、 shmget() は失敗し、 errno に
EEXIST が設定される (これは open(2) に O_CREAT | O_EXCL を指定した場合
の動作と同じである)。
shmflg は以下の内容から構成される:
IPC_CREAT 新しいセグメントを作成する。このフラグが指定されなかった 場
合、 shmget() は key に対応するセグメントを探し、ユーザがそ
のセグメントにアクセスする許可があるかどうかをチェックす る
。
IPC_EXCL IPC_CREAT と共に使用し、セグメントが既に存在した場合には失
敗することを保証する。
mode_flags (下位 9 ビット) 所有者、グループ、他人 (world) への許可を指
定する。これらのビットは open(2) の mode 引き数と同じ形式で
同じ意味を持つ。今のところ、システムは実行 (execute) 許可を
参照しない。
SHM_HUGETLB (Linux 2.6 以降)
"ヒュージページ (huge page)" を使うセグメントを割り当てる。
詳細な情報は、カーネル・ソ ー ス の フ ァ イ ル Documenta-
tion/vm/hugetlbpage.txt を参照。
SHM_NORESERVE (Linux 2.6.15 以降)
こ のフラグは、 mmap(2) の MAP_NORESERVE フラグと同じ役割を
果たす。このセグメントに対するスワップ空間の予約を行わな い
。 スワップ空間を予約した場合は、そのセグメントの変更が必ず
成功することが保証される。スワップ空間の予約を行わなかっ た
場合は、物理メモリに空きがないと書き込み時に SIGSEGV を受け
取る可能性がある。 proc(5) に あ る /proc/sys/vm/overcom-
mit_memory ファイルに関する議論も参照のこと。
共 有メモリ・セグメントが新たに作成される際、共有メモリ・セグメントの内
容は 0 で初期化され、関連情報を保持するデータ構造体 shmid_ds は以下のよ
うに初期化される。
shm_perm.cuid と shm_perm.uid に呼び出し元プロセスの実効 (effec-
tive) ユーザーID を設定する。
shm_perm.cgid と shm_perm.gid に呼び出し元プロセスの実効グ ル ー
プID を設定する。
shm_perm.mode の下位 9 ビットに shmflg の下位 9 ビットを設定する
。
shm_segsz に size の値を設定する。
shm_lpid, shm_nattch, shm_atime, shm_dtime に 0 を設定する。
shm_ctime に現在の時刻を設定する。
共有メモリ・セグメントが既に存在する場合、アクセス許可の検査と 、 破 壊
(destruction) マークがつけられていないかのチェックが行われる。
返り値
成 功 した場合、有効なセグメント識別子 shmid が返される。エラーの場合、
-1 が返される。
エラー
失敗した場合は errno が以下のどれかに設定される:
EACCES ユーザーはその共有メモリ・セグメントへのアクセス許可を持た ず 、
CAP_IPC_OWNER ケーパビリティも持っていない。
EEXIST IPC_CREAT | IPC_EXCL が指定されていたが、そのセグメントが既に存
在する。
EINVAL 新しいセグメントを作成しようとした際に size < SHMMIN または size
> SHMMAX であった。もしくは、指定されたキーに対応するセグメント
が既に存在して、新しいセグメントを作成しようとはしな か っ た が
、size が存在するセグメントのサイズよりも大きかった。
ENFILE システム全体でオープンされているファイルの総数が上限に達した。
ENOENT 指定された key に対応するセグメントが存在せず、 IPC_CREAT も指定
されていなかった。
ENOMEM セグメントの管理情報 (overhead) に割り当てるメモリがなかった。
ENOSPC システム全体の共有メモリ・セグメント数の制限 (SHMMNI) に達した、
または要求された size のセグメントの割り当てがシステム全体の共有
メモリサイズの制限 (SHMALL) を超過した。
EPERM SHM_HUGETLB フラグが指定されたが、呼び出し元には権限がなか っ た
(CAP_IPC_LOCK ケーパビリティを持っていなかった)。
準拠
SVr4, POSIX.1-2001.
SHM_HUGETLB は Linux での拡張であり、移植性はない。
注意
IPC_PRIVATE はフラグではなく key_t 型である。この特別な値が key に使用
された場合は、 shmget() は shmflg の下位 9 ビットを除いた全てを無視し、
(成功すれば) 新しい共有メモリ・セグメントを作成する。
shmget() コールに影響する共有メモリ・セグメント資源の制限は以下の通りで
ある:
SHMALL システム全体の共有メモリ・ページの最大数 (Linux では、この上限値
は /proc/sys/kernel/shmall 経由で参照したり、変更したりできる)。
SHMMAX 共有メモリ・セグメントのバイト単位の大きさの上 限: 方 針 依 存
(Linux では、この上限値は /proc/sys/kernel/shmmax 経由で参照した
り、変更したりできる)。
SHMMIN 共有メモリ・セグメントのバイト単位の大きさの下限: 実装依存 (現在
は 1 バイトだが、実質的な最小サイズは PAGE_SIZE である)。
SHMMNI シ ス テ ム 全体の共有メモリーの数の上限: 実装依存 (現在は 4096
。Linux 2.3.99 より前では 128。 Linux で は 、 こ の 上 限 値 は
/proc/sys/kernel/shmmni 経由で参照したり、変更したりできる)。
プ ロセス当りの共有メモリ・セグメントの個数の最大値 (SHMSEG) に関する実
装上の制限はない。
Linux での注意
バージョン 2.3.30 までは、Linux は削除が予定されている共有メモリ・セ グ
メントに対して shmget() が行われると EIDRM を返していた。
バグ
IPC_PRIVATE という名前を選んだのはおそらく失敗であろう。 IPC_NEW の方が
より明確にその機能を表しているだろう。
関連項目
shmat(2), shmctl(2), shmdt(2), ftok(3), capabilities(7),
shm_overview(7), svipc(7)
Linux 2006-05-02 SHMGET(2)
SHMGET(2) Linux Programmer’s Manual SHMGET(2)
NAME
shmget - allocates a shared memory segment
SYNOPSIS
#include
#include
int shmget(key_t key, size_t size, int shmflg);
DESCRIPTION
shmget() returns the identifier of the shared memory segment associated
with the value of the argument key. A new shared memory segment, with
size equal to the value of size rounded up to a multiple of PAGE_SIZE,
is created if key has the value IPC_PRIVATE or key isn’t IPC_PRIVATE,
no shared memory segment corresponding to key exists, and IPC_CREAT is
specified in shmflg.
If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory
segment already exists for key, then shmget() fails with errno set to
EEXIST. (This is analogous to the effect of the combination O_CREAT |
O_EXCL for open(2).)
The value shmflg is composed of:
IPC_CREAT to create a new segment. If this flag is not used, then
shmget() will find the segment associated with key and
check to see if the user has permission to access the seg-
ment.
IPC_EXCL used with IPC_CREAT to ensure failure if the segment
already exists.
mode_flags (least significant 9 bits) specifying the permissions
granted to the owner, group, and world. These bits have
the same format, and the same meaning, as the mode argument
of open(2). Presently, the execute permissions are not
used by the system.
SHM_HUGETLB (since Linux 2.6)
Allocate the segment using "huge pages." See the kernel
source file Documentation/vm/hugetlbpage.txt for further
information.
SHM_NORESERVE (since Linux 2.6.15)
This flag serves the same purpose as the mmap(2) MAP_NORE-
SERVE flag. Do not reserve swap space for this segment.
When swap space is reserved, one has the guarantee that it
is possible to modify the segment. When swap space is not
reserved one might get SIGSEGV upon a write if no physical
memory is available. See also the discussion of the file
/proc/sys/vm/overcommit_memory in proc(5).
When a new shared memory segment is created, its contents are initial-
ized to zero values, and its associated data structure, shmid_ds (see
shmctl(2)), is initialized 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 least significant 9 bits of shm_perm.mode are set to the
least significant 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 permissions are veri-
fied, and a check is made to see if it is marked for destruction.
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:
EACCES The user does not have permission to access the shared memory
segment, and does not have the CAP_IPC_OWNER capability.
EEXIST IPC_CREAT | IPC_EXCL was specified and the segment exists.
EINVAL A new segment was to be created and size < SHMMIN or size > SHM-
MAX, or no new segment was to be created, a segment with given
key existed, but size is greater than the size of that segment.
ENFILE The system limit on the total number of open files has been
reached.
ENOENT No segment exists for the given key, and IPC_CREAT was not spec-
ified.
ENOMEM No memory could be allocated for segment overhead.
ENOSPC All possible shared memory IDs have been taken (SHMMNI), or
allocating a segment of the requested size would cause the sys-
tem to exceed the system-wide limit on shared memory (SHMALL).
EPERM The SHM_HUGETLB flag was specified, but the caller was not priv-
ileged (did not have the CAP_IPC_LOCK capability).
CONFORMING TO
SVr4, POSIX.1-2001.
SHM_HUGETLB is a non-portable Linux extension.
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 least sig-
nificant 9 bits of shmflg and creates a new shared memory segment (on
success).
The following limits on shared memory segment resources affect the
shmget() call:
SHMALL System wide maximum of shared memory pages (on Linux, this limit
can be read and modified via /proc/sys/kernel/shmall).
SHMMAX Maximum size in bytes for a shared memory segment: policy depen-
dent (on Linux, this limit can be read and modified via
/proc/sys/kernel/shmmax).
SHMMIN Minimum size in bytes for a shared memory segment: implementa-
tion dependent (currently 1 byte, though PAGE_SIZE is the effec-
tive minimum size).
SHMMNI System wide maximum number of shared memory segments: implemen-
tation dependent (currently 4096, was 128 before Linux 2.3.99;
on Linux, this limit can be read and modified via /proc/sys/ker-
nel/shmmni).
The implementation has no specific limits for the per-process maximum
number of shared memory segments (SHMSEG).
Linux Notes
Until version 2.3.30 Linux would return EIDRM for a shmget() on a
shared memory segment scheduled for deletion.
BUGS
The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
clearly show its function.
SEE ALSO
shmat(2), shmctl(2), shmdt(2), ftok(3), capabilities(7),
shm_overview(7), svipc(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2006-05-02 SHMGET(2)