.\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it) .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Modified Sun Nov 28 17:06:19 1993, Rik Faith .\" with material from Luigi P. Bai .\" Portions Copyright 1993 Luigi P. Bai .\" Modified Tue Oct 22 22:04:23 1996 by Eric S. Raymond .\" Modified, 5 Jan 2002, Michael Kerrisk .\" Modified, 19 Sep 2002, Michael Kerrisk .\" Added SHM_REMAP flag description .\" Modified, 27 May 2004, Michael Kerrisk .\" Added notes on capability requirements .\" Modified, 11 Nov 2004, Michael Kerrisk .\" Language and formatting clean-ups .\" Changed wording and placement of sentence regarding attachment .\" of segments marked for destruction .\" .\" FIXME . Add an example program to this page. .\" FIXME Linux 2.6.9 added SHM_EXEC, which should be documented .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated 1999-08-15, HANATAKA Shinya .\" Updated & Modified 2002-05-07, Yuichi SATO .\" Updated & Modified 2003-01-18, Yuichi SATO .\" Updated & Modified 2005-01-07, Yuichi SATO .\" Updated 2005-12-05, Akihiro MOTOKI, Catch up to LDP man-pages 2.16 .\" Updated 2006-04-14, Akihiro MOTOKI, Catch up to LDP man-pages 2.29 .\" .\"WORD: shared memory 共有メモリ .\"WORD: segment セグメント .\"WORD: super-user スーパー・ユーザー .\"WORD: detach 分離 .\"WORD: attach 付加 .\"WORD: member メンバー .\"WORD: implement 実装 .\"WORD: page aligned ページ境界に合った .\"WORD: unaligned 境界違反 .\"WORD: descriptor ディスクリプター .\"WORD: parameter パラメーター .\" .TH SHMOP 2 2008-06-03 "Linux" "Linux Programmer's Manual" .SH 名前 shmat, shmdt \- 共有メモリ (shared memory) の操作 .SH 書式 .nf .B #include .B #include .BI "void *shmat(int " shmid ", const void *" shmaddr ", int " shmflg ); .BI "int shmdt(const void *" shmaddr ); .fi .SH 説明 .BR shmat () は .I shmid で指定された共有メモリ・セグメント (shared memory segment) を コールしたプロセスのアドレス空間に付加 (attach) する。 付加するアドレスは .I shmaddr に以下のどれかの形式で指定する: .LP .I shmaddr が NULL ならば、システムはセグメントを付加するための 適切な (使用されていない) アドレスを選択する。 .LP .I shmaddr が NULL でなく .B SHM_RND が .I shmflg に指定されている場合は、 .I shmaddr を .B SHMLBA の倍数へと切り捨てた (rounding down) のと等しいアドレスへ付加する。 その他の場合は .I shmaddr は付加を行なうアドレスで、ページ境界を指している必要がある。 .PP .B SHM_RDONLY が .I shmflg に指定されていた場合は、 セグメントは読み込み専用に付加され、プロセスはそのセグメントへの 読み込み許可を持たなければならない。 そうでなければそのセグメントは読み込みと書き込みのために付加され、 プロセスはそのセグメントに読み込みと書き込みの許可を持つ必要がある。 書き込み専用の共有メモリ・セグメントという概念は存在しない。 .PP (Linux 特有の) .B SHM_REMAP フラグが .I shmflg に指定された場合は、 セグメントのマッピングを既存のマッピングに置き換える。 マッピングの範囲は、 .I shmaddr から始まりセグメントのサイズ分だけある (通常 .B EINVAL エラーは、このアドレス範囲にマッピングが既に存在するために起る)。 このフラグを指定する場合は、 .I shmaddr が NULL であってはならない。 .PP 呼び出したプロセスの .BR brk (2) の値は付加によって変化しない。 そのセグメントはプロセスが終了 (exit) したら自動的に分離 (detach) される。 同じセグメントをプロセスのアドレス空間に、読み込み専用および読み書き両用 として付加でき、また複数回付加することもできる。 .PP 成功した .BR shmat () コールは共有メモリ・セグメントに関連する .I shmid_ds 構造体 .RB ( shmctl (2) を参照) のメンバーを以下のように更新する: .IP .I shm_atime には現在の時刻を設定する。 .IP .I shm_lpid には呼び出したプロセスのプロセス ID を設定する。 .IP .I shm_nattch を 1 増加させる。 .PP .BR shmdt () は呼び出したプロセスのアドレス空間から .I shmaddr で指定されたアドレスに配置された共有メモリ・セグメントを分離 (detach) する。 分離する共有メモリ・セグメントは、現在 .I shmaddr に付加されているものでなければならない。 .I shmaddr は、それを付加した時に .BR shmat () が返した値に等しくなければならない。 .PP 成功した .BR shmdt () コールはその共有メモリ・セグメントに関連する .I shmid_ds 構造体のメンバーを以下のように更新する: .IP .I shm_dtime には現在の時刻が設定される。 .IP .I shm_lpid には呼び出したプロセスのプロセス ID が設定される。 .IP .I shm_nattch を 1 減少させる。 もし 0 になり、削除マークがあった場合は そのセグメントは削除される。 .PP .BR fork (2) した後、子プロセスは付加された共有メモリ・セグメントを継承する。 .BR exec (2) した後、全ての付加された共有メモリ・セグメントはプロセスから分離される。 .BR exit (2) において、全ての付加された共有メモリ・セグメントはプロセスから分離される。 .PP .SH 返り値 .BR shmat () は、成功した場合、 付加された共有メモリ・セグメントのアドレスを返す。 エラーの場合、 .I (void\ *)\ \-1 を返し、 .I errno にエラーの原因を示す値を設定する。 .BR shmdt () は、成功すると 0 を返す。 エラーの場合、\-1 を返し、 .I errno にエラーの原因を示す値を設定する。 .SH エラー .BR shmat () が失敗した場合、 .I errno に以下の値のどれかを設定して返す: .TP .B EACCES 呼び出したプロセスに要求された種類の付加に必要な許可がなく、 .B CAP_IPC_OWNER ケーパビリティ (capability) がない。 .TP .B EINVAL .I shmid の値が不正である。 .I shmaddr の値が境界違反 (unaligned) (つまり、ページ境界に合っておらず、 \fBSHM_RND\fP が指定されていない) または が不正である。 .I shmaddr へのセグメントの付加に失敗した。 または .B SHM_REMAP が指定されているが、 .I shmaddr が NULL であった。 .TP .B ENOMEM ディスクリプター (descriptor) やページ・テーブルのためのメモリを 割り当てることができない。 .PP .BR shmdt () が失敗した場合、 .B EINVAL は以下のようにセットされる: .TP .B EINVAL .I shmaddr に付加された共有メモリ・セグメントが存在しない。 もしくは、 .\" こちらは 2.6.17-rc1 以降の場合だけ。 .I shmaddr がページ境界に合っていない。 .SH 準拠 SVr4, POSIX.1-2001. .\" SVr4 には他に EMFILE エラー状態についての記述がある。 SVID 3 で (たぶんそれより前だと思うが) \fIshmaddr\fP 引き数の型は .I "char *" から .I "const void *" に、\fIshmat\fP() の返り値の型は .I "char *" から .I "void *" に変更された (Linux では libc4 と libc5 のプロトタイプは .I "char *" であり、glibc2 のプロトタイプは .I "void *" である)。 .SH 注意 共有メモリセグメントを付加する場合の移植性の高い方法としては、 .I shmaddr を NULL にして .BR shmat () を使用するのがよい。 このような方法で付加される共有メモリセグメントは、 プロセスが異なれば別のアドレスに付加される、という点に注意すること。 よって共有メモリ内で管理されるポインタは、 絶対アドレスではなく、 (一般的にはセグメントの開始アドレスからの) 相対アドレスで作成するべきである。 .PP Linux では共有メモリセグメントに既に削除マークが付けられていても、 その共有メモリセグメントを付加することができる。 しかし POSIX.1-2001 ではこのような動作を指定しておらず、 他の多くの実装もこれをサポートしていない。 .LP 以下のシステム・パラメーターは、 .BR shmat () に影響する: .TP .\" FIXME A good explanation of the rationale for the existence .\" of SHMLBA would be useful here .B SHMLBA セグメントの境界アドレスの最小倍数。ページ境界に合ってなければならない。 現在の実装では .B SHMLBA の値は .B PAGE_SIZE である。 .\" FIXME That last sentence isn't true for all Linux .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures) .\" -- MTK, Nov 04 .PP 現在の実装では、プロセスごとの 共有メモリ・セグメントの最大数 .RB ( SHMSEG ) に関する実装依存の制限はない。 .SH 関連項目 .BR brk (2), .BR mmap (2), .BR shmctl (2), .BR shmget (2), .BR capabilities (7), .BR shm_overview (7), .BR svipc (7)