Man page - shm_unlink(3)

Packages contains this manual

Available languages:

en fr ja

Manual

SHM_OPEN

名 前
書 式
説 明
返 り 値
エ ラ ー
バ ー ジ ョ ン
属 性
準 拠
注 意

プ ロ グ ラ ム の ソ ー ス : pshm_ucase.h
プ ロ グ ラ ム の ソ ー ス : pshm_ucase_bounce.c
プ ロ グ ラ ム の ソ ー ス : pshm_ucase_send.c
関 連 項 目
こ の 文 書 に つ い て

名 前

shm_open, shm_unlink - POSIX 共 有 メ モ リ ー オ ブ ジ ェ ク ト の 作 成 /オ ー プ ン /削 除 を 行 う

書 式

#include <sys/mman.h>
#include <sys/stat.h>
/* mode 定 数 用 */
#include <fcntl.h>
/* O_* 定 数 の 定 義 用 */

int shm_open(const char * name , int oflag , mode_t mode );

int shm_unlink(const char * name );

-lrt で リ ン ク す る 。

説 明

shm_open () は 、 POSIX 共 有 メ モ リ ー オ ブ ジ ェ ク ト を 新 規 に 作 成 /オ ー プ ン し た り 、 す で に 存 在 す る オ ブ ジ ェ ク ト を オ ー プ ン し た り す る 。 POSIX 共 有 メ モ リ ー オ ブ ジ ェ ク ト は 、 実 際 に は 、 関 係 の な い プ ロ セ ス が 共 有 メ モ リ ー の 同 じ 領 域 を mmap (2) す る た め に 使 用 す る こ と が で き る 手 段 で あ る 。 shm_unlink () は 、 逆 の 操 作 、 つ ま り 以 前 に shm_open () で 作 成 さ れ た オ ブ ジ ェ ク ト の 削 除 を 行 う 。

shm_open () の 動 作 は open (2) と よ く 似 て い る 。 name で 作 成 し た り オ ー プ ン し た り す る 共 有 メ モ リ ー オ ブ ジ ェ ク ト を 指 定 す る 。 移 植 性 を 持 た せ る た め に は 、 共 有 メ モ リ ー オ ブ ジ ェ ク ト は /somename と い う 形 式 の 名 前 で 識 別 し 、 そ の 名 前 は 、 最 大 で NAME_MAX (す な わ ち 255) 文 字 の ヌ ル 終 端 さ れ た 文 字 列 で 、 ス ラ ッ シ ュ で 始 ま り 、 ス ラ ッ シ ュ 以 外 の 文 字 が 1 文 字 以 上 続 く 形 式 に す べ き で あ る 。

oflag は ビ ッ ト マ ス ク で 、 O_RDONLY O_RDWR の い ず れ か 一 方 と 、 以 下 に 述 べ る 他 の フ ラ グ の 論 理 和 を と っ た も の を 指 定 す る 。
O_RDONLY

読 み 出 し ア ク セ ス 用 に オ ブ ジ ェ ク ト を オ ー プ ン す る 。 こ の フ ラ グ を 指 定 し て オ ー プ ン さ れ た 共 有 メ モ リ ー オ ブ ジ ェ ク ト は 、 読 み 出 し ( PROT_READ ) ア ク セ ス で の み mmap (2) す る こ と が で き る 。

O_RDWR

読 み 書 き ア ク セ ス 用 に オ ブ ジ ェ ク ト を オ ー プ ン す る 。

O_CREAT

存 在 し な い 場 合 、 共 有 メ モ リ ー オ ブ ジ ェ ク ト を 作 成 す る 。 オ ブ ジ ェ ク ト の ユ ー ザ ー と グ ル ー プ の 所 有 権 は 、 呼 び 出 し 元 プ ロ セ ス の 対 応 す る 実 効 ID が 使 わ れ 、 オ ブ ジ ェ ク ト の 許 可 ビ ッ ト は mode の 下 位 9 ビ ッ ト に 基 づ い て 設 定 さ れ る 。 た だ し 、 フ ァ イ ル モ ー ド 作 成 マ ス ク ( umask (2) 参 照 ) に 設 定 さ れ て い る 値 は 、 新 規 オ ブ ジ ェ ク ト に 関 し て は ク リ ア さ れ る 。 mode を 定 義 す る た め に 使 用 で き る マ ク ロ 定 数 (群 )は open (2) に 記 載 さ れ て い る (こ れ ら の 定 数 の シ ン ボ ル 定 義 は <sys/stat.h> の イ ン ク ル ー ド に よ り 得 ら れ る )。

新 規 に 作 成 さ れ た 共 有 メ モ リ ー オ ブ ジ ェ ク ト は 長 さ 0 で 初 期 化 さ れ る 。 オ ブ ジ ェ ク ト の 大 き さ は ftruncate (2) を 使 っ て 設 定 で き る 。 共 有 メ モ リ ー オ ブ ジ ェ ク ト と し て 新 規 に 確 保 さ れ た バ イ ト は 自 動 的 に 0 に 初 期 化 さ れ る 。

O_EXCL

O_CREAT が 一 緒 に 指 定 さ れ て お り 、 name で 指 定 さ れ た 共 有 メ モ リ ー オ ブ ジ ェ ク ト が 既 に 存 在 し た 場 合 、 エ ラ ー を 返 す 。 オ ブ ジ ェ ク ト の 存 在 確 認 と 、 存 在 し な か っ た 場 合 の オ ブ ジ ェ ク ト 作 成 は 、 必 ず 一 連 の 操 作 と し て 実 行 さ れ る (performed atomically)。

O_TRUNC

共 有 メ モ リ ー オ ブ ジ ェ ク ト が す で に 存 在 し た 場 合 、 そ の オ ブ ジ ェ ク ト を 0 バ イ ト に 切 り 詰 め る 。

こ れ ら の フ ラ グ 値 の 定 義 は <fcntl.h> の イ ン ク ル ー ド に よ り 得 ら れ る 。

成 功 し て 完 了 し た 場 合 、 shm_open () は 共 有 メ モ リ ー オ ブ ジ ェ ク ト を 参 照 す る 新 し い フ ァ イ ル デ ィ ス ク リ プ タ ー を 返 す 。 こ の フ ァ イ ル デ ィ ス ク リ プ タ ー は 、 そ の プ ロ セ ス 内 で 過 去 に オ ー プ ン さ れ て い な い フ ァ イ ル デ ィ ス ク リ プ タ ー の 中 で 最 も 小 さ な 数 に な る こ と が 保 証 さ れ る 。 FD_CLOEXEC フ ラ グ ( fcntl (2) を 参 照 ) が 、 こ の フ ァ イ ル デ ィ ス ク リ プ タ ー に 設 定 さ れ る 。

通 常 、 こ れ ら の フ ァ イ ル デ ィ ス ク リ プ タ ー は 、 こ の 後 続 け て 実 行 さ れ る ftruncate (2) (新 規 に 作 成 さ れ た オ ブ ジ ェ ク ト の 場 合 の み ) と mmap (2) の 呼 び 出 し に 使 用 さ れ る 。 mmap (2) を 呼 び 出 し た 後 は 、 フ ァ イ ル デ ィ ス ク リ プ タ ー を ク ロ ー ズ し て も よ く 、 ク ロ ー ズ し て も メ モ リ ー マ ッ ピ ン グ に 影 響 を 与 え る こ と は な い 。

shm_unlink () の 動 作 は unlink (2) と よ く 似 て い る : 共 有 メ モ リ ー オ ブ ジ ェ ク ト 名 を 削 除 し 、 す べ て の プ ロ セ ス が 処 理 対 象 の オ ブ ジ ェ ク ト を ア ン マ ッ プ し た 時 点 で オ ブ ジ ェ ク ト の 割 り 当 て を 解 除 し 、 対 応 す る メ モ リ ー 領 域 の 内 容 を 破 棄 す る 。 shm_unlink () が 成 功 し た 後 で 、 同 じ name を 持 つ オ ブ ジ ェ ク ト に 対 し て shm_open () を 行 う と 、 ( O_CREAT が 指 定 さ れ て い な い 場 合 ) 失 敗 す る 。 ( O_CREAT が 指 定 さ れ て い る 場 合 、 新 し く 別 の オ ブ ジ ェ ク ト が 作 成 さ れ る )。

返 り 値

成 功 し た 場 合 、 shm_open () は フ ァ イ ル デ ィ ス ク リ プ タ ー (非 負 の 整 数 ) を 返 す 。 失 敗 し た 場 合 、 shm_open () は -1 を 返 す 。 shm_unlink () は 、 成 功 し た 場 合 0 を 、 エ ラ ー が 起 こ っ た 場 合 -1 を 返 す 。

エ ラ ー

失 敗 し た 場 合 、 エ ラ ー の 原 因 を 示 す た め errno が 設 定 さ れ る 。 errno に 設 定 さ れ る 値 は 以 下 の 通 り で あ る :

EACCES

共 有 メ モ リ ー オ ブ ジ ェ ク ト を shm_unlink () す る 権 限 が な か っ た 。

EACCES

指 定 さ れ た mode name shm_open () す る 権 限 が な か っ た 。 も し く は 、 O_TRUNC が 指 定 さ れ た が 、 呼 び 出 し 元 に は そ の オ ブ ジ ェ ク ト に 対 す る 書 き 込 み 権 限 が な か っ た 。

EEXIST

O_CREAT O_EXCL の 両 方 が shm_open () に 指 定 さ れ た が 、 name で 指 定 さ れ た 共 有 メ モ リ ー オ ブ ジ ェ ク ト が 既 に 存 在 し た 。

EINVAL

shm_open () に 与 え ら れ た name 引 数 が 不 正 で あ っ た 。

EMFILE

オ ー プ ン さ れ て い る フ ァ イ ル デ ィ ス ク リ プ タ ー 数 の プ ロ セ ス 単 位 の 上 限 に 達 し た 。

ENAMETOOLONG

name の 長 さ が PATH_MAX を 越 え て い る 。

ENFILE

シ ス テ ム 全 体 で オ ー プ ン さ れ て い る フ ァ イ ル の 総 数 が 上 限 に 達 し た 。

ENOENT

存 在 し て い な い name の オ ブ ジ ェ ク ト を shm_open () し よ う と し た が 、 O_CREAT が 指 定 さ れ て い な か っ た 。

ENOENT

存 在 し な い name の オ ブ ジ ェ ク ト を shm_unlink () し よ う と し た 。

バ ー ジ ョ ン

こ れ ら の 関 数 は glibc 2.2 以 降 で 提 供 さ れ て い る 。

属 性

こ の 節 で 使 用 さ れ て い る 用 語 の 説 明 に つ い て は 、 attributes (7) を 参 照 。

Image grohtml-23683-1.png

準 拠

POSIX.1-2001, POSIX.1-2008.

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.

注 意

POSIX は O_RDONLY O_TRUNC が 一 緒 に 指 定 さ れ た 場 合 の 動 作 を 未 定 義 に し て い る 。 Linux で は 、 既 存 の 共 有 メ モ リ ー オ ブ ジ ェ ク ト に 対 す る 切 り 詰 め (truncate) は 成 功 す る 。 し か し 、 他 の UNIX シ ス テ ム で も 同 じ で あ る と は 限 ら な い 。

Linux に お け る POSIX 共 有 メ モ リ ー オ ブ ジ ェ ク ト の 実 装 は 専 用 の tmpfs (5) フ ァ イ ル シ ス テ ム を 使 用 す る 。 そ の フ ァ イ ル シ ス テ ム は 通 常 /dev/shm に マ ウ ン ト さ れ る 。

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:

$ ./pshm_ucase_bounce /myshm &
[1] 270171
$ ./pshm_ucase_send /myshm hello
HELLO

Further detail about these programs is provided below.

プ ロ グ ラ ム の ソ ー ス : 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.

#include <sys/mman.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} 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 'buf' */
char buf[BUF_SIZE]; /* Data being transferred */
};

プ ロ グ ラ ム の ソ ー ス : 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 shmbuf 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.

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.

/* pshm_ucase_bounce.c

Licensed under GNU General Public License v2 or later.
*/
#include <ctype.h>
#include "pshm_ucase.h"

int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s /shm-path\n", 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's 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 'sem1' 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 'sem2' 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);
}

プ ロ グ ラ ム の ソ ー ス : 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.

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.

/* pshm_ucase_send.c

Licensed under GNU General Public License v2 or later.
*/
#include <string.h>
#include "pshm_ucase.h"

int
main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s /shm-path string\n", 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\n");
exit(EXIT_FAILURE);
}

/* Open the existing shared memory object and map it
into the caller's 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, "\n", 1);

exit(EXIT_SUCCESS);
}

関 連 項 目

close (2), fchmod (2), fchown (2), fcntl (2), fstat (2), ftruncate (2), memfd_create (2), mmap (2), open (2), umask (2), shm_overview (7)

こ の 文 書 に つ い て

こ の man ペ ー ジ は Linux man-pages プ ロ ジ ェ ク ト の リ リ ー ス 5.10 の 一 部 で あ る 。 プ ロ ジ ェ ク ト の 説 明 と バ グ 報 告 に 関 す る 情 報 は https://www.kernel.org/doc/man-pages/ に 書 か れ て い る 。