函數(shù)介紹
/* The following System V style IPC functions implement a shared memory
facility. The definition is found in XPG4.2. */
/* Shared memory control operation. */
int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf);
/* Get shared memory segment. */
int shmget (key_t __key, size_t __size, int __shmflg) ;
/* Attach shared memory segment. */
void *shmat (int __shmid, const void *__shmaddr, int __shmflg);
/* Detach shared memory segment. */
int shmdt (const void *__shmaddr) ;
shmget 說明
/* Get shared memory segment. */
int shmget (key_t __key, size_t __size, int __shmflg) ;
- key shmget 中的key與shmid對應真慢,可以用ftok來生成key或者指定IPC_PRIVATE
- size 共享內(nèi)存的大小
- flag 權(quán)限設(shè)置
/* Mode bits for `msgget', `semget', and `shmget'. */
#define IPC_CREAT 01000 /* Create key if key does not exist. */
#define IPC_EXCL 02000 /* Fail if key exists. */
#define IPC_NOWAIT 04000 /* Return error on wait. */
后面3位是用戶毅臊、用戶組、其他用戶的讀寫執(zhí)行權(quán)限黑界,如 0777
- 返回值 成功返回 shmid管嬉,用戶后面的讀寫id,失敗返回負數(shù)
shmctl 說明
/* Shared memory control operation. */
int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf);
- shmid 通過shmget返回的id
- cmd 控制選項
#define IPC_RMID 0 /* Remove identifier. */
#define IPC_SET 1 /* Set `ipc_perm' options. */
#define IPC_STAT 2 /* Get `ipc_perm' options. */
/* Commands for `shmctl'. */
#define SHM_LOCK 11 /* lock segment (root only) */
#define SHM_UNLOCK 12 /* unlock segment (root only) */
后面兩個選項只適用于linux 和solaris
lock :當內(nèi)存不夠時不允許與外存換入換出
unlock 反之
- buf set 和get的結(jié)構(gòu)體信息朗鸠,如可以通過get獲取共享內(nèi)存的大小
/* Data structure describing a shared memory segment. */
struct shmid_ds
{
struct ipc_perm shm_perm; /* operation permission struct */
size_t shm_segsz; /* size of segment in bytes */
__time_t shm_atime; /* time of last shmat() */
#ifndef __x86_64__
unsigned long int __glibc_reserved1;
#endif
__time_t shm_dtime; /* time of last shmdt() */
#ifndef __x86_64__
unsigned long int __glibc_reserved2;
#endif
__time_t shm_ctime; /* time of last change by shmctl() */
#ifndef __x86_64__
unsigned long int __glibc_reserved3;
#endif
__pid_t shm_cpid; /* pid of creator */
__pid_t shm_lpid; /* pid of last shmop */
shmatt_t shm_nattch; /* number of current attaches */
__syscall_ulong_t __glibc_reserved4;
__syscall_ulong_t __glibc_reserved5;
};
shmat說明
/* Attach shared memory segment. */
void *shmat (int __shmid, const void *__shmaddr, int __shmflg);
- shmid 通過get返回的id
- shmaddr 如何為空蚯撩,則系統(tǒng)給你返回映射的地址,如果該參數(shù)非空烛占,會映射到此地址上
為了不必要的麻煩胎挎,一般傳遞空
- flag 在地址傳遞非空的時候用于設(shè)置SHM_RND, 好像是什么地址隨機之類的
同樣不用自找麻煩,傳遞0就完事了
代碼展示
/**
* @file shm_lsw.c
* @author 李斯文 (lisiwen945@163.com)
* @brief 共享內(nèi)存的接口測試函數(shù)
* @version 0.1
* @date 2021-01-28
*
* @copyright Copyright (c) 2021
*
*/
#include "apue.h"
#include <sys/shm.h>
#include <fcntl.h>
#include <errno.h>
void shm_send()
{
int id = shmget(11, 1024, 0777);
if (id < 0) {
printf("%s shmget error errno %d\n", __func__, errno);
return;
} else {
printf("shmget success id %d\n", id);
}
struct shmid_ds ds;
if (shmctl(id, IPC_STAT, &ds) < 0) {
printf("func %s line %d error\n", __func__, __LINE__);
return;
} else {
printf("get share memory size %ld\n", ds.shm_segsz);
}
char * buf = shmat(id, 0, 0);
if (buf == NULL) {
printf("func %s line %d error\n", __func__, __LINE__);
return;
}
char msg[] = "lisiwen debug";
strcpy(buf, msg);
}
void shm_recv()
{
int id = shmget(11, 1024, 0777);
if (id < 0) {
printf("%s shmget error errno %d\n", __func__, errno);
return;
} else {
printf("shmget success id %d\n", id);
}
struct shmid_ds ds;
if (shmctl(id, IPC_STAT, &ds) < 0) {
printf("func %s line %d error\n", __func__, __LINE__);
return;
} else {
printf("get share memory size %ld\n", ds.shm_segsz);
}
char * buf = shmat(id, 0, 0);
printf("get msg %s\n", buf);
}
void shm_del()
{
int id = shmget(11, 1024, 0777);
if (id < 0) {
printf("%s shmget error errno %d\n", __func__, errno);
return;
} else {
printf("shmget success id %d\n", id);
}
if (shmctl(id, IPC_RMID, NULL) < 0) {
printf("func %s line %d error\n", __func__, __LINE__);
return;
} else {
printf("remove share memory %d success\n", id);
}
}
void shm_create()
{
int id = shmget(11, 1024, IPC_CREAT|IPC_EXCL|0777);
if (id < 0) {
printf("create shared memory error errno %d\n", errno);
} else {
printf("shmget success id %d\n", id);
}
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("%s arg", argv[0]);
return 0;
}
if (strcmp(argv[1], "send") == 0) {
shm_send();
} else if (strcmp(argv[1], "recv") == 0) {
shm_recv();
} else if (strcmp(argv[1], "del") == 0) {
/* 使用ipcrm -q id也可以刪除 */
shm_del();
} else if (strcmp(argv[1], "create") == 0) {
shm_create();
}
}
運行結(jié)果
ipcs 可以查看ipc的詳細信息 -m表示共享內(nèi)存
swing@ubun:~/code/apue.3e/ipc1$ ipcs -m
------------ 共享內(nèi)存段 --------------
鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài)
0x00000000 8 swing 600 67108864 2 目標
0x00000000 11 swing 600 524288 2 目標
0x00000000 12 swing 600 524288 2 目標
0x00000000 13 swing 600 524288 2 目標
0x00000000 20 swing 600 524288 2 目標
創(chuàng)建共享內(nèi)存犹菇,可以看到多路一個id為23的內(nèi)存德迹,大小為1024字節(jié)
swing@ubun:~/code/apue.3e/ipc1$ ./shm_lsw create
shmget success id 23
swing@ubun:~/code/apue.3e/ipc1$ ipcs -m
------------ 共享內(nèi)存段 --------------
鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài)
0x00000000 8 swing 600 67108864 2 目標
0x00000000 11 swing 600 524288 2 目標
0x00000000 12 swing 600 524288 2 目標
0x00000000 13 swing 600 524288 2 目標
0x00000000 20 swing 600 524288 2 目標
0x0000000b 23 swing 777 1024 0
swing@ubun:~/code/apue.3e/ipc1$ ./shm_lsw send
shmget success id 23
get share memory size 1024
swing@ubun:~/code/apue.3e/ipc1$ ./shm_lsw recv
shmget success id 23
get share memory size 1024
get msg lisiwen debug
swing@ubun:~/code/apue.3e/ipc1$ ./shm_lsw del
shmget success id 23
remove share memory 23 success
swing@ubun:~/code/apue.3e/ipc1$ ipcs -m
------------ 共享內(nèi)存段 --------------
鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài)
0x00000000 8 swing 600 67108864 2 目標
0x00000000 11 swing 600 524288 2 目標
0x00000000 12 swing 600 524288 2 目標
0x00000000 13 swing 600 524288 2 目標
0x00000000 20 swing 600 524288 2 目標
swing@ubun:~/code/apue.3e/ipc1$