- 在Linux下可以使用System V共享内存段实现共享内存,一共有4个API:
- 创建共享内存段或使用已经创建的共享内存段-shmget()
- 将进程附加到已经创建的共享内存段-shmat()
- 从已连接的共享内存段中分离进程-shmdt()
- 共享内存段上的控制操作-shmctl()
- 使用System V共享内存段需要引入头文件:
#include <sys/shm.h>
- 多读者和一个写者的样例
#include <sys/shm.h>
#include <unistd.h>
#include <error.h>
#include <iostream>
#include <string>
using namespace std;
#define BUF_SIZE 1024
#define SHM_KEY 114514
void reader(int seq) {
cout << "reader " << seq << endl;
int shm_id = shmget(SHM_KEY, BUF_SIZE, IPC_CREAT);
if (shm_id == -1) {
perror("Fail to get shared memory id");
return;
}
// Attach to the segment to get a pointer to it.
char* shm_buf = static_cast<char*>(shmat(shm_id, NULL, 0));
if (shm_buf == nullptr) {
perror("Fail to attach shared memory");
return;
}
/* Transfer blocks of data from shared memory to stdout*/
while (1) {
cout << "buf[" << seq << "] = " << shm_buf << endl;
if (string(shm_buf) == to_string(seq)) {
break;
}
sleep(3);
}
cout << "Detaching shared memory" << endl;
if (shmdt(shm_buf) == -1) {
perror("Fail to detaching shared memory");
return;
}
}
void writer() {
int shm_id = shmget(SHM_KEY, BUF_SIZE, IPC_CREAT);
if (shm_id == -1) {
perror("Fail to get shared memory id");
return;
}
// Attach to the segment to get a pointer to it.
char* shm_buf = static_cast<char*>(shmat(shm_id, NULL, 0));
if (shm_buf == nullptr) {
perror("Fail to attach shared memory");
return;
}
/* Transfer blocks of data from shared memory to stdout*/
while (1) {
cin >> shm_buf;
}
cout << "Detaching shared memory" << endl;
if (shmdt(shm_buf) == -1) {
perror("Fail to detaching shared memory");
return;
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
return 0;
}
srand(::time(nullptr));
cout << argv[1] << endl;
string argv1 = argv[1];
if (argv1 == "reader") {
reader(rand() % 1000);
} else {
writer();
}
return 0;
}
- 将文件命名为shm_test.cpp,编译后运行
./shm_test reader
./shm_test writer
- 可以看到读者能够读取到写者写入的数据
- 如果报错显示
- Fail to get shared memory id: Permission denied
- 那么执行
sudo ./shm_test reader
sudo ./shm_test writer
标签:共享内存,memory,C++,Linux,test,shared,include,shm
From: https://www.cnblogs.com/umichan0621/p/16588431.html