Man page - shmat(2)

Packages contains this manual

Available languages:

en fr pl ja ru de

Manual

SHMOP

名 前
書 式
説 明
shmat()
shmdt()
返 り 値
エ ラ ー
準 拠
注 意

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

名 前

shmat, shmdt - System V 共 有 メ モ リ ー (shared memory) の 操 作

書 式

#include <sys/types.h>
#include <sys/shm.h>

void *shmat(int shmid , const void * shmaddr , int shmflg );

int shmdt(const void * shmaddr );

説 明

shmat()

shmat () は shmid で 指 定 さ れ た System V 共 有 メ モ リ ー セ グ メ ン ト (shared memory segment) を コ ー ル し た プ ロ セ ス の ア ド レ ス 空 間 に 付 加 (attach) す る 。 付 加 す る ア ド レ ス は shmaddr に 以 下 の ど れ か の 形 式 で 指 定 す る :

shmaddr が NULL な ら ば 、 シ ス テ ム は セ グ メ ン ト を 付 加 す る た め の 適 切 な (使 用 さ れ て い な い ) ペ ー ジ 境 界 の ア ド レ ス を 選 択 す る 。

shmaddr が NULL で な く SHM_RND shmflg に 指 定 さ れ て い る 場 合 は 、 shmaddr SHMLBA の 倍 数 へ と 切 り 捨 て た (rounding down) の と 等 し い ア ド レ ス へ 付 加 す る 。

そ の 他 の 場 合 は shmaddr は 付 加 を 行 な う ア ド レ ス で 、 ペ ー ジ 境 界 を 指 し て い る 必 要 が あ る 。

SHM_RND に 加 え て 、 以 下 の フ ラ グ を shmflg ビ ッ ト マ ス ク 引 数 に 指 定 で き る 。
SHM_EXEC
(Linux 固 有 ; Linux 2.6.9 以 降 )

セ グ メ ン ト の 内 容 を 実 行 で き る 。 呼 び 出 し 元 は セ グ メ ン ト に 対 す る 実 行 許 可 を 持 っ て い な け れ ば な ら な い 。

SHM_RDONLY

セ グ メ ン ト を 読 み 込 み 専 用 に 付 加 す る 。 プ ロ セ ス は そ の セ グ メ ン ト へ の 読 み 込 み 許 可 を 持 っ て い な け れ ば な ら な い 。 こ の フ ラ グ が 指 定 さ れ な か っ た 場 合 、 そ の セ グ メ ン ト は 読 み 込 み と 書 き 込 み ア ク セ ス の た め に 付 加 さ れ 、 プ ロ セ ス は そ の セ グ メ ン ト に 読 み 込 み と 書 き 込 み の 許 可 を 持 っ て い な け れ ば な ら な い 。 書 き 込 み 専 用 の 共 有 メ モ リ ー セ グ メ ン ト と い う 概 念 は 存 在 し な い 。

SHM_REMAP (Linux 固 有 )

こ の フ ラ グ は 、 セ グ メ ン ト の マ ッ ピ ン グ を 既 存 の マ ッ ピ ン グ に 置 き 換 え る こ と を 指 示 す る 。 マ ッ ピ ン グ の 範 囲 は 、 shmaddr か ら 始 ま り セ グ メ ン ト の サ イ ズ 分 だ け あ る (通 常 EINVAL エ ラ ー は 、 こ の ア ド レ ス 範 囲 に マ ッ ピ ン グ が 既 に 存 在 す る た め に 起 る )。 こ の フ ラ グ を 指 定 す る 場 合 は 、 shmaddr が NULL で あ っ て は な ら な い 。

呼 び 出 し た プ ロ セ ス の brk (2) の 値 は 付 加 に よ っ て 変 化 し な い 。 そ の セ グ メ ン ト は プ ロ セ ス が 終 了 (exit) し た ら 自 動 的 に 分 離 (detach) さ れ る 。 同 じ セ グ メ ン ト を プ ロ セ ス の ア ド レ ス 空 間 に 、 読 み 込 み 専 用 お よ び 読 み 書 き 両 用 と し て 付 加 で き 、 ま た 複 数 回 付 加 す る こ と も で き る 。

成 功 し た shmat () コ ー ル は 共 有 メ モ リ ー セ グ メ ン ト に 関 連 す る shmid_ds 構 造 体 ( shmctl (2) を 参 照 ) の メ ン バ ー を 以 下 の よ う に 更 新 す る :

shm_atime に は 現 在 の 時 刻 を 設 定 す る 。

shm_lpid に は 呼 び 出 し た プ ロ セ ス の プ ロ セ ス ID が 設 定 さ れ る 。

shm_nattch を 1 増 加 さ せ る 。

shmdt()

shmdt () は 呼 び 出 し た プ ロ セ ス の ア ド レ ス 空 間 か ら shmaddr で 指 定 さ れ た ア ド レ ス に 配 置 さ れ た 共 有 メ モ リ ー セ グ メ ン ト を 分 離 (detach) す る 。 分 離 す る 共 有 メ モ リ ー セ グ メ ン ト は 、 現 在 shmaddr に 付 加 さ れ て い る も の で な け れ ば な ら な い 。 shmaddr は 、 そ れ を 付 加 し た 時 に shmat () が 返 し た 値 に 等 し く な け れ ば な ら な い 。

成 功 し た shmdt () コ ー ル は そ の 共 有 メ モ リ ー セ グ メ ン ト に 関 連 す る shmid_ds 構 造 体 の メ ン バ ー を 以 下 の よ う に 更 新 す る :

shm_dtime に は 現 在 の 時 刻 が 設 定 さ れ る 。

shm_lpid に は 呼 び 出 し た プ ロ セ ス の プ ロ セ ス ID が 設 定 さ れ る 。

shm_nattch を 1 減 少 さ せ る 。 も し 0 に な り 、 削 除 マ ー ク が あ っ た 場 合 は そ の セ グ メ ン ト は 削 除 さ れ る 。

返 り 値

shmat () は 、 成 功 し た 場 合 、 付 加 さ れ た 共 有 メ モ リ ー セ グ メ ン ト の ア ド レ ス を 返 す 。 エ ラ ー の 場 合 、 (void *) -1 を 返 し 、 errno に エ ラ ー の 原 因 を 示 す 値 を 設 定 す る 。

shmdt () は 、 成 功 す る と 0 を 返 す 。 エ ラ ー の 場 合 、 -1 を 返 し 、 errno に エ ラ ー の 原 因 を 示 す 値 を 設 定 す る 。

エ ラ ー

shmat () が 失 敗 し た 場 合 、 errno に 以 下 の 値 の ど れ か を 設 定 し て 返 す :

EACCES

The calling process does not have the required permissions for the requested attach type, and does not have the CAP_IPC_OWNER capability in the user namespace that governs its IPC namespace.

EIDRM

shmid が 削 除 (remove) さ れ た 識 別 子 (identifier) を 指 し て い る 。

EINVAL

shmid の 値 が 不 正 で あ る 。 shmaddr の 値 が 境 界 違 反 (unaligned) (つ ま り 、 ペ ー ジ 境 界 に 合 っ て お ら ず 、 SHM_RND が 指 定 さ れ て い な い ) ま た は が 不 正 で あ る 。 shmaddr へ の セ グ メ ン ト の 付 加 に 失 敗 し た 。 ま た は SHM_REMAP が 指 定 さ れ て い る が 、 shmaddr が NULL で あ っ た 。

ENOMEM

デ ィ ス ク リ プ タ ー (descriptor) や ペ ー ジ テ ー ブ ル の た め の メ モ リ ー を 割 り 当 て る こ と が で き な い 。

shmdt () が 失 敗 し た 場 合 、 EINVAL は 以 下 の よ う に セ ッ ト さ れ る :

EINVAL

shmaddr に 付 加 さ れ た 共 有 メ モ リ ー セ グ メ ン ト が 存 在 し な い 。 も し く は 、 shmaddr が ペ ー ジ 境 界 に 合 っ て い な い 。

準 拠

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

SVID 3 で (た ぶ ん そ れ よ り 前 だ と 思 う が ) shmaddr 引 数 の 型 は char * か ら const void * に 、 shmat () の 返 り 値 の 型 は char * か ら void * に 変 更 さ れ た 。

注 意

fork (2) し た 後 、 子 プ ロ セ ス は 付 加 さ れ た 共 有 メ モ リ ー セ グ メ ン ト を 継 承 す る 。

exec (2) し た 後 、 全 て の 付 加 さ れ た 共 有 メ モ リ ー セ グ メ ン ト は プ ロ セ ス か ら 分 離 さ れ る 。

exit (2) に お い て 、 全 て の 付 加 さ れ た 共 有 メ モ リ ー セ グ メ ン ト は プ ロ セ ス か ら 分 離 さ れ る 。

共 有 メ モ リ ー セ グ メ ン ト を 付 加 す る 場 合 の 移 植 性 の 高 い 方 法 と し て は 、 shmaddr を NULL に し て shmat () を 使 用 す る の が よ い 。 こ の よ う な 方 法 で 付 加 さ れ る 共 有 メ モ リ ー セ グ メ ン ト は 、 プ ロ セ ス が 異 な れ ば 別 の ア ド レ ス に 付 加 さ れ る 、 と い う 点 に 注 意 す る こ と 。 よ っ て 共 有 メ モ リ ー 内 で 管 理 さ れ る ポ イ ン タ ー は 、 絶 対 ア ド レ ス で は な く 、 (一 般 的 に は セ グ メ ン ト の 開 始 ア ド レ ス か ら の ) 相 対 ア ド レ ス で 作 成 す る べ き で あ る 。

Linux で は 共 有 メ モ リ ー セ グ メ ン ト に 既 に 削 除 マ ー ク が 付 け ら れ て い て も 、 そ の 共 有 メ モ リ ー セ グ メ ン ト を 付 加 す る こ と が で き る 。 し か し POSIX.1 で は こ の よ う な 動 作 を 指 定 し て お ら ず 、 他 の 多 く の 実 装 も こ れ を サ ポ ー ト し て い な い 。

以 下 の シ ス テ ム パ ラ メ ー タ ー は 、 shmat () に 影 響 す る :

SHMLBA

セ グ メ ン ト の 下 限 ア ド レ ス 倍 数 (Segment low boundary address multiple)。 shmat () の 呼 び 出 し に お い て 付 加 す る ア ド レ ス を 明 示 的 に 指 定 す る 際 、 呼 び 出 し 元 は 指 定 す る ア ド レ ス が こ の 値 の 倍 数 に な る よ う に 保 証 し な け れ ば な ら な い 。 こ れ は い く つ か の ア ー キ テ ク チ ャ ー で は 必 要 な こ と で 、 CPU キ ャ ッ シ ュ の 性 能 を 保 証 す る た め で あ っ た り 、 同 じ セ グ メ ン ト の 別 の 付 与 を CPU キ ャ ッ シ ュ 内 部 で 一 貫 し て 扱 え る よ う に す る た め だ っ た り す る 。 SHMLBA は 通 常 は シ ス テ ム ペ ー ジ サ イ ズ の 倍 数 で あ る (Linux の 多 く の ア ー キ テ ク チ ャ ー で は SHMLBA は シ ス テ ム ペ ー ジ サ イ ズ と 同 じ で あ る )。

現 在 の 実 装 で は 、 プ ロ セ ス ご と の 共 有 メ モ リ ー セ グ メ ン ト の 最 大 数 ( SHMSEG ) に 関 す る 実 装 依 存 の 制 限 は な い 。

The two programs shown below exchange a string using a shared memory segment. Further details about the programs are given below. First, we show a shell session demonstrating their use.

In one terminal window, we run the "reader" program, which creates a System V shared memory segment and a System V semaphore set. The program prints out the IDs of the created objects, and then waits for the semaphore to change value.

$ ./svshm_string_read
shmid = 1114194; semid = 15

In another terminal window, we run the "writer" program. The "writer" program takes three command-line arguments: the IDs of the shared memory segment and semaphore set created by the "reader", and a string. It attaches the existing shared memory segment, copies the string to the shared memory, and modifies the semaphore value.

$ ./svshm_string_write 1114194 15 'Hello, world'

Returning to the terminal where the "reader" is running, we see that the program has ceased waiting on the semaphore and has printed the string that was copied into the shared memory segment by the writer:

Hello, world

プ ロ グ ラ ム の ソ ー ス : svshm_string.h

The following header file is included by the "reader" and "writer" programs.

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)

union semun { /* Used in calls to semctl() */
int val;
struct semid_ds * buf;
unsigned short * array;
#if defined(__linux__)
struct seminfo * __buf;
#endif
};

#define MEM_SIZE 4096

プ ロ グ ラ ム の ソ ー ス : svshm_string_read.c

The "reader" program creates a shared memory segment and a semaphore set containing one semaphore. It then attaches the shared memory object into its address space and initializes the semaphore value to 1. Finally, the program waits for the semaphore value to become 0, and afterwards prints the string that has been copied into the shared memory segment by the "writer".

/* svshm_string_read.c

Licensed under GNU General Public License v2 or later.
*/
#include "svshm_string.h"

int
main(int argc, char *argv[])
{
int semid, shmid;
union semun arg, dummy;
struct sembuf sop;
char *addr;

/* Create shared memory and semaphore set containing one
semaphore */

shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
if (shmid == -1)
errExit("shmget");

semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
if (shmid == -1)
errExit("shmget");

/* Attach shared memory into our address space */

addr = shmat(shmid, NULL, SHM_RDONLY);
if (addr == (void *) -1)
errExit("shmat");

/* Initialize semaphore 0 in set with value 1 */

arg.val = 1;
if (semctl(semid, 0, SETVAL, arg) == -1)
errExit("semctl");

printf("shmid = %d; semid = %d\n", shmid, semid);

/* Wait for semaphore value to become 0 */

sop.sem_num = 0;
sop.sem_op = 0;
sop.sem_flg = 0;

if (semop(semid, &sop, 1) == -1)
errExit("semop");

/* Print the string from shared memory */

printf("%s\n", addr);

/* Remove shared memory and semaphore set */

if (shmctl(shmid, IPC_RMID, NULL) == -1)
errExit("shmctl");
if (semctl(semid, 0, IPC_RMID, dummy) == -1)
errExit("semctl");

exit(EXIT_SUCCESS);
}

プ ロ グ ラ ム の ソ ー ス : svshm_string_write.c

The writer program takes three command-line arguments: the IDs of the shared memory segment and semaphore set that have already been created by the "reader", and a string. It attaches the shared memory segment into its address space, and then decrements the semaphore value to 0 in order to inform the "reader" that it can now examine the contents of the shared memory.

/* svshm_string_write.c

Licensed under GNU General Public License v2 or later.
*/
#include "svshm_string.h"

int
main(int argc, char *argv[])
{
int semid, shmid;
struct sembuf sop;
char *addr;
size_t len;

if (argc != 4) {
fprintf(stderr, "Usage: %s shmid semid string\n", argv[0]);
exit(EXIT_FAILURE);
}

len = strlen(argv[3]) + 1; /* +1 to include trailing '\0' */
if (len > MEM_SIZE) {
fprintf(stderr, "String is too big!\n");
exit(EXIT_FAILURE);
}

/* Get object IDs from command-line */

shmid = atoi(argv[1]);
semid = atoi(argv[2]);

/* Attach shared memory into our address space and copy string
(including trailing null byte) into memory. */

addr = shmat(shmid, NULL, 0);
if (addr == (void *) -1)
errExit("shmat");

memcpy(addr, argv[3], len);

/* Decrement semaphore to 0 */

sop.sem_num = 0;
sop.sem_op = -1;
sop.sem_flg = 0;

if (semop(semid, &sop, 1) == -1)
errExit("semop");

exit(EXIT_SUCCESS);
}

関 連 項 目

brk (2), mmap (2), shmctl (2), shmget (2), capabilities (7), shm_overview (7), sysvipc (7)

こ の 文 書 に つ い て

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