文章目录
- 代码 /* @Time : 2023/1/10 11:46 @Author : emperinter @File : test_shared_memory.py @desc : - */ #include <sys/shm.h> #include <unistd.h> #include <iostream> #include <string> using namespace std; typedef void *HANDLE; void reader(int seq) { cout << "reader " << seq << endl; int shm_id = shmget((key_t) 1234, 1024, 0666 | 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); int shm_id = shmget((key_t) 1234, 1024, 0666 | 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; } 开启writer 假设编译好的可执行文件为test_shared_memory sudo ./test_shared_memory writer 开启reader sudo ./test_shared_memory reader
- https://www.cnblogs.com/52php/p/5861372.html https://blog.csdn.net/tojohnonly/article/details/70246965 https://www.cnblogs.com/umichan0621/p/16588431.html https://blog.csdn.net/guoping16/article/details/6584058
顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存。共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式。不同进程之间共享的内存通常安排为同一段物理内存。进程可以将同一段共享内存连接到它们自己的地址空间中,所有进程都可以访问共享内存中的地址,就好像它们是由用C语言函数malloc()分配的内存一样。而如果某个进程向共享内存写入数据,所做的改动将立即影响到可以访问同一段共享内存的任何其他进程。
- 代码
/*
@Time : 2023/1/10 11:46
@Author : emperinter
@File : test_shared_memory.py
@desc :
-
*/
#include <sys/shm.h>
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;
typedef void *HANDLE;
void reader(int seq) {
cout << "reader " << seq << endl;
int shm_id = shmget((key_t) 1234, 1024, 0666 | 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);
int shm_id = shmget((key_t) 1234, 1024, 0666 | 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;
}
- 开启writer
假设编译好的可执行文件为test_shared_memory
sudo ./test_shared_memory writer
- 开启reader
sudo ./test_shared_memory reader
/*
@Time : 2023/1/10 11:46
@Author : emperinter
@File : test_shared_memory.py
@desc :
-
*/
#include <sys/shm.h>
#include <unistd.h>
#include <iostream>
#include <string>
using namespace std;
typedef void *HANDLE;
void reader(int seq) {
cout << "reader " << seq << endl;
int shm_id = shmget((key_t) 1234, 1024, 0666 | 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);
int shm_id = shmget((key_t) 1234, 1024, 0666 | 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;
}
假设编译好的可执行文件为test_shared_memory
sudo ./test_shared_memory writer
sudo ./test_shared_memory reader
