shmopのヘルプ・マニュアル
日本語 英語
shmop --help
man shmop
SHMOP(2) Linux Programmer’s Manual SHMOP(2)
名前
shmat, shmdt - 共有メモリ (shared memory) の操作
書式
#include
#include
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
説明
shmat() は shmid で指定された共有メモリ・セグメント (shared memory seg-
ment) をコールしたプロセスのアドレス空間に付加 (attach) する。付加す る
アドレスは shmaddr に以下のどれかの形式で指定する:
shmaddr が NULL ならば、システムはセグメントを付加するための適切な (使
用されていない) アドレスを選択する。
shmaddr が NULL でなく SHM_RND が shmflg に指定されて い る 場 合 は 、
shmaddr を SHMLBA の倍数へと切り捨てた (rounding down) のと等しいアドレ
スへ付加する。その他の場合は shmaddr は付加を行なうアドレスで、ページ境
界を指している必要がある。
SHM_RDONLY が shmflg に指定されていた場合は、セグメントは読み込み専用に
付加され、プロセスはそのセグメントへの読み込み許可を持たなければなら な
い 。そうでなければそのセグメントは読み込みと書き込みのために付加され、
プロセスはそのセグメントに読み込みと書き込みの許可を持つ必要がある。 書
き込み専用の共有メモリ・セグメントという概念は存在しない。
(Linux 特有の) SHM_REMAP フラグが shmflg に指定された場合は、セグメント
のマッピングを既存のマッピングに置き換える。マッピ ン グ の 範 囲 は 、
shmaddr から始まりセグメントのサイズ分だけある (通常 EINVAL エラーは、
このアドレス範囲にマッピングが既に存在するために起る)。このフラグを指定
する場合は、 shmaddr が NULL であってはならない。
呼 び出したプロセスの brk(2) の値は付加によって変化しない。そのセグメン
トはプロセスが終了 (exit) したら自動的に分離 (detach) される。同じセ グ
メ ントをプロセスのアドレス空間に、読み込み専用および読み書き両用として
付加でき、また複数回付加することもできる。
成功した shmat() コールは共有メモリ・セグメントに関連する shmid_ds 構造
体 (shmctl(2) を参照) のメンバーを以下のように更新する:
shm_atime には現在の時刻を設定する。
shm_lpid には呼び出したプロセスのプロセス ID を設定する。
shm_nattch を 1 増加させる。
shmdt() は呼び出したプロセスのアドレス空間から shmaddr で指定されたアド
レスに配置された共有メモリ・セグメントを分離 (detach) する。分離する 共
有メモリ・セグメントは、現在 shmaddr に付加されているものでなければなら
ない。 shmaddr は、それを付加した時に shmat() が返した値に等しくなけ れ
ばならない。
成 功した shmdt() コールはその共有メモリ・セグメントに関連する shmid_ds
構造体のメンバーを以下のように更新する:
shm_dtime には現在の時刻が設定される。
shm_lpid には呼び出したプロセスのプロセス ID が設定される。
shm_nattch を 1 減少させる。もし 0 になり、削除マークがあった 場
合はそのセグメントは削除される。
fork(2) した後、子プロセスは付加された共有メモリ・セグメントを継承する
。
exec(2) した後、全ての付加された共有メモリ・セグメントはプロセスから 分
離される。
exit(2) において、全ての付加された共有メモリ・セグメントはプロセスから
分離される。
返り値
shmat() は、成功した場合、付加された共有メモリ・セグメントのアドレス を
返 す。エラーの場合、 (void *) -1 を返し、 errno にエラーの原因を示す値
を設定する。
shmdt() は、成功すると 0 を返す。エラーの場合、-1 を返し、 errno にエラ
ーの原因を示す値を設定する。
エラー
shmat() が失敗した場合、 errno に以下の値のどれかを設定して返す:
EACCES 呼 び 出 したプロセスに要求された種類の付加に必要な許可がなく、
CAP_IPC_OWNER ケーパビリティ (capability) がない。
EIDRM shmid が削除 (remove) された識別子 (identifier) を指している。
EINVAL shmid の値が不正である。 shmaddr の値が境界違反 (unaligned) ( つ
ま り、ページ境界に合っておらず、 SHM_RND が指定されていない) ま
たはが不正である。 shmaddr へのセグメントの付加に失敗した。ま た
は SHM_REMAP が指定されているが、 shmaddr が NULL であった。
ENOMEM ディスクリプター (descriptor) やページ・テーブルのためのメモリを
割り当てることができない。
shmdt() が失敗した場合、 EINVAL は以下のようにセットされる:
EINVAL shmaddr に付加された共有メモリ・セグメントが存在しない。もしくは
、 shmaddr がページ境界に合っていない。
準拠
SVr4, POSIX.1-2001.
SVID 3 で (たぶんそれより前だと思うが) shmaddr 引き数の型は char * から
const void * に、shmat() の返り値の型は char * から void * に変更された
(Linux では libc4 と libc5 のプロトタイプは char * であり、glibc2 のプ
ロトタイプは void * である)。
注意
共有メモリセグメントを付加する場合の移植性の高い方法としては、 shmaddr
を NULL にして shmat() を使用するのがよい。このような方法で付加される共
有メモリセグメントは、プロセスが異なれば別のアドレスに付加される、と い
う 点に注意すること。よって共有メモリ内で管理されるポインタは、絶対アド
レスではなく、 (一般的にはセグメントの開始アドレスからの) 相対アドレ ス
で作成するべきである。
Linux では共有メモリセグメントに既に削除マークが付けられていても、その
共有メモリセグメントを付加することができる。しかし POSIX.1-2001 では こ
の ような動作を指定しておらず、他の多くの実装もこれをサポートしていない
。
以下のシステム・パラメーターは、 shmat() に影響する:
SHMLBA セグメントの境界アドレスの最小倍数。ページ境界に合ってなければな
らない。現在の実装では SHMLBA の値は PAGE_SIZE である。
現 在 の実装では、プロセスごとの共有メモリ・セグメントの最大数 (SHMSEG)
に関する実装依存の制限はない。
関連項目
brk(2), mmap(2), shmctl(2), shmget(2), capabilities(7),
shm_overview(7), svipc(7)
Linux 2008-06-03 SHMOP(2)
SHMOP(2) Linux Programmer’s Manual SHMOP(2)
NAME
shmat, shmdt - shared memory operations
SYNOPSIS
#include
#include
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
DESCRIPTION
shmat() attaches the shared memory segment identified by shmid to the
address space of the calling process. The attaching address is speci-
fied by shmaddr with one of the following criteria:
If shmaddr is NULL, the system chooses a suitable (unused) address at
which to attach the segment.
If shmaddr isn’t NULL and SHM_RND is specified in shmflg, the attach
occurs at the address equal to shmaddr rounded down to the nearest mul-
tiple of SHMLBA. Otherwise shmaddr must be a page-aligned address at
which the attach occurs.
If SHM_RDONLY is specified in shmflg, the segment is attached for read-
ing and the process must have read permission for the segment. Other-
wise the segment is attached for read and write and the process must
have read and write permission for the segment. There is no notion of
a write-only shared memory segment.
The (Linux-specific) SHM_REMAP flag may be specified in shmflg to indi-
cate that the mapping of the segment should replace any existing map-
ping in the range starting at shmaddr and continuing for the size of
the segment. (Normally an EINVAL error would result if a mapping
already exists in this address range.) In this case, shmaddr must not
be NULL.
The brk(2) 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 one, and more
than once, in the process’s address space.
A successful shmat() call updates the members of the shmid_ds structure
(see shmctl(2)) associated with 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 one.
shmdt() detaches the shared memory segment located at the address spec-
ified by shmaddr from the address space of the calling process. The
to-be-detached segment must be currently attached with shmaddr equal to
the value returned by the attaching shmat() call.
On a successful shmdt() call the system updates the members of the
shmid_ds structure associated with the shared memory segment as fol-
lows:
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 one. If it becomes 0 and the seg-
ment is marked for deletion, the segment is deleted.
After a fork(2) the child inherits the attached shared memory segments.
After an execve(2) all attached shared memory segments are detached
from the process.
Upon _exit(2) all attached shared memory segments are detached from the
process.
RETURN VALUE
On success shmat() returns the address of the attached shared memory
segment; on error (void *) -1 is returned, and errno is set to indicate
the cause of the error.
On success shmdt() returns 0; on error -1 is returned, and errno is set
to indicate the cause of the error.
ERRORS
When shmat() fails, errno is set to one of the following:
EACCES The calling process does not have the required permissions for
the requested attach type, and does not have the CAP_IPC_OWNER
capability.
EIDRM shmid points to a removed identifier.
EINVAL Invalid shmid value, unaligned (i.e., not page-aligned and
SHM_RND was not specified) or invalid shmaddr value, or can’t
attach segment at shmaddr, or SHM_REMAP was specified and
shmaddr was NULL.
ENOMEM Could not allocate memory for the descriptor or for the page
tables.
When shmdt() fails, errno is set as follows:
EINVAL There is no shared memory segment attached at shmaddr; or,
shmaddr is not aligned on a page boundary.
CONFORMING TO
SVr4, POSIX.1-2001.
In SVID 3 (or perhaps earlier) the type of the shmaddr argument was
changed from char * into const void *, and the returned type of shmat()
from char * into void *. (Linux libc4 and libc5 have the char * proto-
types; glibc2 has void *.)
NOTES
Using shmat() with shmaddr equal to NULL is the preferred, portable way
of attaching a shared memory segment. Be aware that the shared memory
segment attached in this way may be attached at different addresses in
different processes. Therefore, any pointers maintained within the
shared memory must be made relative (typically to the starting address
of the segment), rather than absolute.
On Linux, it is possible to attach a shared memory segment even if it
is already marked to be deleted. However, POSIX.1-2001 does not spec-
ify this behavior and many other implementations do not support it.
The following system parameter affects shmat():
SHMLBA Segment low boundary address multiple. Must be page aligned.
For the current implementation the SHMLBA value is PAGE_SIZE.
The implementation places no intrinsic limit on the per-process maximum
number of shared memory segments (SHMSEG).
SEE ALSO
brk(2), mmap(2), shmctl(2), shmget(2), 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 2008-06-03 SHMOP(2)