.\" 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 (faith@cs.unc.edu) .\" with material from Luigi P. Bai (lpb@softint.com) .\" 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 .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH SHMOP 2 2008\-06\-03 Linux "Linux Programmer's Manual" .SH 名前 shmat, shmdt \- 共有メモリ (shared memory) の操作 .SH 書式 .nf \fB#include \fP \fB#include \fP \fBvoid *shmat(int \fP\fIshmid\fP\fB, const void *\fP\fIshmaddr\fP\fB, int \fP\fIshmflg\fP\fB);\fP \fBint shmdt(const void *\fP\fIshmaddr\fP\fB);\fP .fi .SH 説明 \fBshmat\fP() は \fIshmid\fP で指定された共有メモリ・セグメント (shared memory segment) を コールしたプロセスのアドレス空間に付加 (attach) する。 付加するアドレスは \fIshmaddr\fP に以下のどれかの形式で指定する: .LP \fIshmaddr\fP が NULL ならば、システムはセグメントを付加するための 適切な (使用されていない) アドレスを選択する。 .LP \fIshmaddr\fP が NULL でなく \fBSHM_RND\fP が \fIshmflg\fP に指定されている場合は、 \fIshmaddr\fP を \fBSHMLBA\fP の倍数へと切り捨てた (rounding down) のと等しいアドレスへ付加する。 その他の場合は \fIshmaddr\fP は付加を行なうアドレスで、ページ境界を指している必要がある。 .PP \fBSHM_RDONLY\fP が \fIshmflg\fP に指定されていた場合は、 セグメントは読み込み専用に付加され、プロセスはそのセグメントへの 読み込み許可を持たなければならない。 そうでなければそのセグメントは読み込みと書き込みのために付加され、 プロセスはそのセグメントに読み込みと書き込みの許可を持つ必要がある。 書き込み専用の共有メモリ・セグメントという概念は存在しない。 .PP (Linux 特有の) \fBSHM_REMAP\fP フラグが \fIshmflg\fP に指定された場合は、 セグメントのマッピングを既存のマッピングに置き換える。 マッピングの範囲は、 \fIshmaddr\fP から始まりセグメントのサイズ分だけある (通常 \fBEINVAL\fP エラーは、このアドレス範囲にマッピングが既に存在するために起る)。 このフラグを指定する場合は、 \fIshmaddr\fP が NULL であってはならない。 .PP 呼び出したプロセスの \fBbrk\fP(2) の値は付加によって変化しない。 そのセグメントはプロセスが終了 (exit) したら自動的に分離 (detach) される。 同じセグメントをプロセスのアドレス空間に、読み込み専用および読み書き両用 として付加でき、また複数回付加することもできる。 .PP 成功した \fBshmat\fP() コールは共有メモリ・セグメントに関連する \fIshmid_ds\fP 構造体 (\fBshmctl\fP(2) を参照) のメンバーを以下のように更新する: .IP \fIshm_atime\fP には現在の時刻を設定する。 .IP \fIshm_lpid\fP には呼び出したプロセスのプロセス ID が設定される。 .IP \fIshm_nattch\fP を 1 増加させる。 .PP \fBshmdt\fP() は呼び出したプロセスのアドレス空間から \fIshmaddr\fP で指定されたアドレスに配置された共有メモリ・セグメントを分離 (detach) する。 分離する共有メモリ・セグメントは、現在 \fIshmaddr\fP に付加されているものでなければならない。 \fIshmaddr\fP は、それを付加した時に \fBshmat\fP() が返した値に等しくなければならない。 .PP 成功した \fBshmdt\fP() コールはその共有メモリ・セグメントに関連する \fIshmid_ds\fP 構造体のメンバーを以下のように更新する: .IP \fIshm_dtime\fP には現在の時刻が設定される。 .IP \fIshm_lpid\fP には呼び出したプロセスのプロセス ID が設定される。 .IP \fIshm_nattch\fP を 1 減少させる。 もし 0 になり、削除マークがあった場合は そのセグメントは削除される。 .PP \fBfork\fP(2) した後、子プロセスは付加された共有メモリ・セグメントを継承する。 \fBexec\fP(2) した後、全ての付加された共有メモリ・セグメントはプロセスから分離される。 \fBexit\fP(2) において、全ての付加された共有メモリ・セグメントはプロセスから分離される。 .SH 返り値 \fBshmat\fP() は、成功した場合、 付加された共有メモリ・セグメントのアドレスを返す。 エラーの場合、 \fI(void\ *)\ \-1\fP を返し、 \fIerrno\fP にエラーの原因を示す値を設定する。 \fBshmdt\fP() は、成功すると 0 を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP にエラーの原因を示す値を設定する。 .SH エラー \fBshmat\fP() が失敗した場合、 \fIerrno\fP に以下の値のどれかを設定して返す: .TP \fBEACCES\fP 呼び出したプロセスに要求された種類の付加に必要な許可がなく、 \fBCAP_IPC_OWNER\fP ケーパビリティ (capability) がない。 .TP \fBEINVAL\fP \fIshmid\fP の値が不正である。 \fIshmaddr\fP の値が境界違反 (unaligned) (つまり、ページ境界に合っておらず、 \fBSHM_RND\fP が指定されていない) または が不正である。 \fIshmaddr\fP へのセグメントの付加に失敗した。 または \fBSHM_REMAP\fP が指定されているが、 \fIshmaddr\fP が NULL であった。 .TP \fBENOMEM\fP ディスクリプター (descriptor) やページ・テーブルのためのメモリを 割り当てることができない。 .PP \fBshmdt\fP() が失敗した場合、 \fBEINVAL\fP は以下のようにセットされる: .TP \fBEINVAL\fP .\" The following since 2.6.17-rc1: \fIshmaddr\fP に付加された共有メモリ・セグメントが存在しない。 もしくは、 \fIshmaddr\fP がページ境界に合っていない。 .SH 準拠 .\" SVr4 documents an additional error condition EMFILE. SVr4, POSIX.1\-2001. SVID 3 で (たぶんそれより前だと思うが) \fIshmaddr\fP 引き数の型は \fIchar *\fP から \fIconst void *\fP に、\fIshmat\fP() の返り値の型は \fIchar *\fP から \fIvoid *\fP に変更された (Linux では libc4 と libc5 のプロトタイプは \fIchar *\fP であり、glibc2 のプロトタイプは \fIvoid *\fP である)。 .SH 注意 共有メモリセグメントを付加する場合の移植性の高い方法としては、 \fIshmaddr\fP を NULL にして \fBshmat\fP() を使用するのがよい。 このような方法で付加される共有メモリセグメントは、 プロセスが異なれば別のアドレスに付加される、という点に注意すること。 よって共有メモリ内で管理されるポインタは、 絶対アドレスではなく、 (一般的にはセグメントの開始アドレスからの) 相対アドレスで作成するべきである。 .PP Linux では共有メモリセグメントに既に削除マークが付けられていても、 その共有メモリセグメントを付加することができる。 しかし POSIX.1\-2001 ではこのような動作を指定しておらず、 他の多くの実装もこれをサポートしていない。 .LP 以下のシステム・パラメーターは、 \fBshmat\fP() に影響する: .TP .\" FIXME A good explanation of the rationale for the existence .\" of SHMLBA would be useful here \fBSHMLBA\fP .\" FIXME That last sentence isn't true for all Linux .\" architectures (i.e., SHMLBA != PAGE_SIZE for some architectures) .\" -- MTK, Nov 04 セグメントの境界アドレスの最小倍数。ページ境界に合ってなければならない。 現在の実装では \fBSHMLBA\fP の値は \fBPAGE_SIZE\fP である。 .PP 現在の実装では、プロセスごとの 共有メモリ・セグメントの最大数 (\fBSHMSEG\fP) に関する実装依存の制限はない。 .SH 関連項目 \fBbrk\fP(2), \fBmmap\fP(2), \fBshmctl\fP(2), \fBshmget\fP(2), \fBcapabilities\fP(7), \fBshm_overview\fP(7), \fBsvipc\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。