.\" Copyright (C) 2002, 2020 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" 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. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2002 Akihiro MOTOKI, all rights reserved. .\" Translated Mon Feb 2 2003 by Akihiro MOTOKI .\" Updated 2009-02-23, Akihiro MOTOKI , LDP v3.19 .\" .TH SHM_OPEN 3 2020\-11\-01 Linux "Linux Programmer's Manual" .SH 名前 shm_open, shm_unlink \- POSIX 共有メモリーオブジェクトの作成/オープン/削除を行う .SH 書式 \fB#include \fP .br \fB#include \fP /* mode 定数用 */ .br \fB#include \fP /* O_* 定数の定義用 */ .PP \fBint shm_open(const char *\fP\fIname\fP\fB, int \fP\fIoflag\fP\fB, mode_t \fP\fImode\fP\fB);\fP .PP \fBint shm_unlink(const char *\fP\fIname\fP\fB);\fP .PP \fI\-lrt\fP でリンクする。 .SH 説明 \fBshm_open\fP() は、POSIX 共有メモリーオブジェクトを新規に作成/オープンしたり、 すでに存在するオブジェクトをオープンしたりする。 POSIX 共有メモリーオブジェクトは、実際には、関係のないプロセスが 共有メモリーの同じ領域を \fBmmap\fP(2) するために使用することができる手段である。 \fBshm_unlink\fP() は、逆の操作、つまり以前に \fBshm_open\fP() で作成されたオブジェクトの削除を行う。 .PP .\" glibc allows the initial slash to be omitted, and makes .\" multiple initial slashes equivalent to a single slash. .\" This differs from the implementation of POSIX message queues. .\" glibc allows subdirectory components in the name, in which .\" case the subdirectory must exist under /dev/shm, and allow the .\" required permissions if a user wants to create a shared memory .\" object in that subdirectory. \fBshm_open\fP() の動作は \fBopen\fP(2) とよく似ている。 \fIname\fP で作成したりオープンしたりする共有メモリーオブジェクトを指定する。 移植性を持たせるためには、共有メモリーオブジェクトは \fI/somename\fP という形式の名前で識別し、 その名前は、最大で \fBNAME_MAX\fP (すなわち 255) 文字のヌル終端された文字列で、 スラッシュで始まり、スラッシュ以外の文字が 1 文字以上続く形式 にすべきである。 .PP \fIoflag\fP はビットマスクで、 \fBO_RDONLY\fP と \fBO_RDWR\fP のいずれか一方と、以下に述べる他のフラグの論理和をとったもの を指定する。 .TP \fBO_RDONLY\fP 読み出しアクセス用にオブジェクトをオープンする。 このフラグを指定してオープンされた共有メモリーオブジェクトは、 読み出し (\fBPROT_READ\fP) アクセスでのみ \fBmmap\fP(2) することができる。 .TP \fBO_RDWR\fP 読み書きアクセス用にオブジェクトをオープンする。 .TP \fBO_CREAT\fP .\" In truth it is actually the filesystem IDs on Linux, but these .\" are nearly always the same as the effective IDs. (MTK, Jul 05) 存在しない場合、共有メモリーオブジェクトを作成する。 オブジェクトのユーザーとグループの所有権は、 呼び出し元プロセスの対応する実効 ID が使われ、 オブジェクトの許可ビットは \fImode\fP の下位 9 ビットに基づいて設定される。ただし、 ファイルモード作成マスク (\fBumask\fP(2) 参照) に設定されている値は、新規オブジェクトに関してはクリアされる。 \fImode\fP を定義するために使用できるマクロ定数(群)は \fBopen\fP(2) に記載されている (これらの定数のシンボル定義は \fI\fP のインクルードにより得られる)。 .IP 新規に作成された共有メモリーオブジェクトは長さ 0 で初期化される。 オブジェクトの大きさは \fBftruncate\fP(2) を使って設定できる。 共有メモリーオブジェクトとして新規に確保されたバイトは自動的に 0 に初期化される。 .TP \fBO_EXCL\fP \fBO_CREAT\fP が一緒に指定されており、 \fIname\fP で指定された共有メモリーオブジェクトが既に存在した場合、 エラーを返す。 オブジェクトの存在確認と、存在しなかった場合のオブジェクト作成は、 必ず一連の操作として実行される (performed atomically)。 .TP \fBO_TRUNC\fP 共有メモリーオブジェクトがすでに存在した場合、 そのオブジェクトを 0 バイトに切り詰める。 .PP これらのフラグ値の定義は \fI\fP のインクルードにより得られる。 .PP 成功して完了した場合、 \fBshm_open\fP() は共有メモリーオブジェクトを参照する新しいファイルディスクリプターを返す。 このファイルディスクリプターは、そのプロセス内で過去にオープンされていない ファイルディスクリプターの中で最も小さな数になることが保証される。 \fBFD_CLOEXEC\fP フラグ (\fBfcntl\fP(2) を参照) が、このファイルディスクリプターに設定される。 .PP 通常、これらのファイルディスクリプターは、この後続けて実行される \fBftruncate\fP(2) (新規に作成されたオブジェクトの場合のみ) と \fBmmap\fP(2) の呼び出しに使用される。 \fBmmap\fP(2) を呼び出した後は、ファイルディスクリプターをクローズしてもよく、 クローズしてもメモリーマッピングに影響を与えることはない。 .PP \fBshm_unlink\fP() の動作は \fBunlink\fP(2) とよく似ている: 共有メモリーオブジェクト名を削除し、すべてのプロセスが処理対象の オブジェクトをアンマップした時点でオブジェクトの割り当てを解除し、 対応するメモリー領域の内容を破棄する。 \fBshm_unlink\fP() が成功した後で、同じ \fIname\fP を持つオブジェクトに対して \fBshm_open\fP() を行うと、 (\fBO_CREAT\fP が指定されていない場合) 失敗する。 (\fBO_CREAT\fP が指定されている場合、新しく別のオブジェクトが作成される)。 .SH 返り値 成功した場合、 \fBshm_open\fP() はファイルディスクリプター (非負の整数) を返す。 失敗した場合、 \fBshm_open\fP() は \-1 を返す。 \fBshm_unlink\fP() は、成功した場合 0 を、エラーが起こった場合 \-1 を返す。 .SH エラー 失敗した場合、エラーの原因を示すため \fIerrno\fP が設定される。 \fIerrno\fP に設定される値は以下の通りである: .TP \fBEACCES\fP 共有メモリーオブジェクトを \fBshm_unlink\fP() する権限がなかった。 .TP \fBEACCES\fP 指定された \fImode\fP で \fIname\fP を \fBshm_open\fP() する権限がなかった。もしくは、 \fBO_TRUNC\fP が指定されたが、呼び出し元にはそのオブジェクトに対する書き込み権限が なかった。 .TP \fBEEXIST\fP \fBO_CREAT\fP と \fBO_EXCL\fP の両方が \fBshm_open\fP() に指定されたが、 \fIname\fP で指定された共有メモリーオブジェクトが既に存在した。 .TP \fBEINVAL\fP \fBshm_open\fP() に与えられた \fIname\fP 引数が不正であった。 .TP \fBEMFILE\fP オープンされているファイルディスクリプター数のプロセス単位の上限に達した。 .TP \fBENAMETOOLONG\fP \fIname\fP の長さが \fBPATH_MAX\fP を越えている。 .TP \fBENFILE\fP システム全体でオープンされているファイルの総数が上限に達した。 .TP \fBENOENT\fP 存在していない \fIname\fP のオブジェクトを \fBshm_open\fP() しようとしたが、 \fBO_CREAT\fP が指定されていなかった。 .TP \fBENOENT\fP 存在しない \fIname\fP のオブジェクトを \fBshm_unlink\fP() しようとした。 .SH バージョン これらの関数は glibc 2.2 以降で提供されている。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lbw24 lb lb l l l. インターフェース 属性 値 T{ \fBshm_open\fP(), \fBshm_unlink\fP() T} Thread safety MT\-Safe locale .TE .sp 1 .SH 準拠 POSIX.1\-2001, POSIX.1\-2008. .PP POSIX.1\-2001 says that the group ownership of a newly created shared memory object is set to either the calling process's effective group ID or "a system default group ID". POSIX.1\-2008 says that the group ownership may be set to either the calling process's effective group ID or, if the object is visible in the filesystem, the group ID of the parent directory. .SH 注意 POSIX は \fBO_RDONLY\fP と \fBO_TRUNC\fP が一緒に指定された場合の動作を未定義にしている。Linux では、 既存の共有メモリーオブジェクトに対する切り詰め (truncate) は成功する。 しかし、他の UNIX システムでも同じであるとは限らない。 .PP Linux における POSIX 共有メモリーオブジェクトの実装は専用の \fBtmpfs\fP(5) ファイルシステムを使用する。そのファイルシステムは通常 \fI/dev/shm\fP にマウントされる。 .SH 例 The programs below employ POSIX shared memory and POSIX unnamed semaphores to exchange a piece of data. The "bounce" program (which must be run first) raises the case of a string that is placed into the shared memory by the "send" program. Once the data has been modified, the "send" program then prints the contents of the modified shared memory. An example execution of the two programs is the following: .PP .in +4n .EX $ \fB./pshm_ucase_bounce /myshm &\fP [1] 270171 $ \fB./pshm_ucase_send /myshm hello\fP HELLO .EE .in .PP .\" Further detail about these programs is provided below. .SS "プログラムのソース: pshm_ucase.h" The following header file is included by both programs below. Its primary purpose is to define a structure that will be imposed on the memory object that is shared between the two programs. .PP .in +4n .EX #include #include #include #include #include #include #include #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e } while (0) #define BUF_SIZE 1024 /* Maximum size for exchanged string */ /* Define a structure that will be imposed on the shared memory object */ struct shmbuf { sem_t sem1; /* POSIX unnamed semaphore */ sem_t sem2; /* POSIX unnamed semaphore */ size_t cnt; /* Number of bytes used in \(aqbuf\(aq */ char buf[BUF_SIZE]; /* Data being transferred */ }; .EE .in .\" .SS "プログラムのソース: pshm_ucase_bounce.c" The "bounce" program creates a new shared memory object with the name given in its command\-line argument and sizes the object to match the size of the \fIshmbuf\fP structure defined in the header file. It then maps the object into the process's address space, and initializes two POSIX semaphores inside the object to 0. .PP After the "send" program has posted the first of the semaphores, the "bounce" program upper cases the data that has been placed in the memory by the "send" program and then posts the second semaphore to tell the "send" program that it may now access the shared memory. .PP .in +4n .EX /* pshm_ucase_bounce.c Licensed under GNU General Public License v2 or later. */ #include #include "pshm_ucase.h" int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s /shm\-path\en", argv[0]); exit(EXIT_FAILURE); } char *shmpath = argv[1]; /* Create shared memory object and set its size to the size of our structure */ int fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); if (fd == \-1) errExit("shm_open"); if (ftruncate(fd, sizeof(struct shmbuf)) == \-1) errExit("ftruncate"); /* Map the object into the caller\(aqs address space */ struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED) errExit("mmap"); /* Initialize semaphores as process\-shared, with value 0 */ if (sem_init(&shmp\->sem1, 1, 0) == \-1) errExit("sem_init\-sem1"); if (sem_init(&shmp\->sem2, 1, 0) == \-1) errExit("sem_init\-sem2"); /* Wait for \(aqsem1\(aq to be posted by peer before touching shared memory */ if (sem_wait(&shmp\->sem1) == \-1) errExit("sem_wait"); /* Convert data in shared memory into upper case */ for (int j = 0; j < shmp\->cnt; j++) shmp\->buf[j] = toupper((unsigned char) shmp\->buf[j]); /* Post \(aqsem2\(aq to tell the to tell peer that it can now access the modified data in shared memory */ if (sem_post(&shmp\->sem2) == \-1) errExit("sem_post"); /* Unlink the shared memory object. Even if the peer process is still using the object, this is okay. The object will be removed only after all open references are closed. */ shm_unlink(shmpath); exit(EXIT_SUCCESS); } .EE .in .\" .SS "プログラムのソース: pshm_ucase_send.c" The "send" program takes two command\-line arguments: the pathname of a shared memory object previously created by the "bounce" program and a string that is to be copied into that object. .PP The program opens the shared memory object and maps the object into its address space. It then copies the data specified in its second argument into the shared memory, and posts the first semaphore, which tells the "bounce" program that it can now access that data. After the "bounce" program posts the second semaphore, the "send" program prints the contents of the shared memory on standard output. .PP .in +4n .EX /* pshm_ucase_send.c Licensed under GNU General Public License v2 or later. */ #include #include "pshm_ucase.h" int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s /shm\-path string\en", argv[0]); exit(EXIT_FAILURE); } char *shmpath = argv[1]; char *string = argv[2]; size_t len = strlen(string); if (len > BUF_SIZE) { fprintf(stderr, "String is too long\en"); exit(EXIT_FAILURE); } /* Open the existing shared memory object and map it into the caller\(aqs address space */ int fd = shm_open(shmpath, O_RDWR, 0); if (fd == \-1) errExit("shm_open"); struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED) errExit("mmap"); /* Copy data into the shared memory object */ shmp\->cnt = len; memcpy(&shmp\->buf, string, len); /* Tell peer that it can now access shared memory */ if (sem_post(&shmp\->sem1) == \-1) errExit("sem_post"); /* Wait until peer says that it has finished accessing the shared memory */ if (sem_wait(&shmp\->sem2) == \-1) errExit("sem_wait"); /* Write modified data in shared memory to standard output */ write(STDOUT_FILENO, &shmp\->buf, len); write(STDOUT_FILENO, "\en", 1); exit(EXIT_SUCCESS); } .EE .in .SH 関連項目 \fBclose\fP(2), \fBfchmod\fP(2), \fBfchown\fP(2), \fBfcntl\fP(2), \fBfstat\fP(2), \fBftruncate\fP(2), \fBmemfd_create\fP(2), \fBmmap\fP(2), \fBopen\fP(2), \fBumask\fP(2), \fBshm_overview\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。