
共享内存
方式1 使用
方法2 使用
发布日期:2021-05-08 04:53:58
浏览次数:21
分类:原创文章
本文共 2466 字,大约阅读时间需要 8 分钟。
方式1 使用mmap
#include <sys/mman.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <stdio.h>void perror(const char *s);void* create_shm(int size) { int protection = PROT_READ | PROT_WRITE; int visibility = MAP_ANONYMOUS | MAP_SHARED; return mmap(NULL, size, protection, visibility, 0, 0);}int main() { char* msg1 = "heheheheh"; char* msg2 = "xxxxoxoxoxox"; printf("size of msg2: %d\n", strlen(msg2)); void* shm = create_shm(12); memcpy(shm, msg1, strlen(msg1)); int pid = fork(); if (pid < 0) { perror("fork faild"); } if (pid == 0) { printf("entry child process...\n"); printf("child process read message from shared memory: %s\n", shm); memcpy(shm, msg2, strlen(msg2)); printf("child process write message to shared memory: %s\n", msg2); } else { printf("this is parent process\n"); printf("parent process read message: %s\n", shm); sleep(1); printf("after 1 second parent process read: %s\n", shm); }}
方法2 使用shmget
程序1 创建共享内存,并写入数据,程序2 获取共享内存,然后读取内容。
- 程序1
#include <sys/ipc.h>#include <sys/shm.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>// shmkey usually defined by ftok, 2222 here is jusk simplifyint shmkey = 2222;int main() { // create shared memory int shmid = shmget(shmkey, 1024, IPC_CREAT | 0777); if (shmid < 0) { perror("shmget faild"); exit(1); } // attach the shared memory void* shm = shmat(shmid, NULL, 0); char* message = "hello world"; printf("write message to shared message: %s\n", message); memcpy(shm, message, strlen(message)); // wait another process to read shared memory int wait = 30; while (1) { sleep(1); wait--; if (wait < 0) { // delete shared memory and exit printf("it's time to say good bye...\n"); shmctl(shmid, IPC_RMID, NULL); return 0; } }}
- 程序2
#include <sys/ipc.h>#include <sys/shm.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>// shmkey usually defined by ftok, 2222 here is jusk simplifyint shmkey = 2222;int main() { // create shared memory int shmid = shmget(shmkey, 1024, 0777); if (shmid < 0) { perror("shmget faild"); exit(1); } // attach the shared memory void* shm = shmat(shmid, NULL, 0); printf("read message to shared message: %s\n", shm); shmdt(shm);}
发表评论
最新留言
很好
[***.229.124.182]2025年04月05日 18时01分30秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux探测工具BCC(可观测性)
2021-05-09
Opentelemetry Metrics SDK
2021-05-09
流量控制--2.传统的流量控制元素
2021-05-09
SNMP介绍及使用,超有用,建议收藏!
2021-05-09
SDUT2161:Simple Game(NIM博弈+巴什博弈)
2021-05-09
51nod 1596 搬货物(二进制处理)
2021-05-09
来自星星的祝福(容斥+排列组合)
2021-05-09
Hmz 的女装(递推)
2021-05-09
HDU5589:Tree(莫队+01字典树)
2021-05-09
不停机替换线上代码? 你没听错,Arthas它能做到
2021-05-09
sharding-jdbc 分库分表的 4种分片策略,还蛮简单的
2021-05-09
分库分表的 9种分布式主键ID 生成方案,挺全乎的
2021-05-09
MySQL不会丢失数据的秘密,就藏在它的 7种日志里
2021-05-09
Python开发之序列化与反序列化:pickle、json模块使用详解
2021-05-09
回顾-生成 vs 判别模型-和图
2021-05-09
采坑 - 字符串的 "" 与 pd.isnull()
2021-05-09
无序列表 - 链表
2021-05-09
SQL 查询强化 - 数据准备
2021-05-09
SQL 强化练习 (四)
2021-05-09