使用c++代码进行内存共享操作,内存共享可以通过key value的形式来保存内存,后面可以使用key值来直接读取内存,效率会很高/

函数说明:

shmget(key_t key, size_t size, int shmflg)(得到一个共享内存标识符或创建一个共享内存对象)

shmflg参数为模式标志参数,使用时需要与IPC对象存取权限(如0600)进行|运算来确定信号量集的存取权限

shmat(int shmid, const void *shmaddr, int shmflg)(把共享内存区对象映射到调用进程的地址空间)

shmdt(const void *shmaddr)(断开共享内存连接)

shmctl(int shmid, int cmd, struct shmid_ds *buf)(共享内存管理)

cmd

IPC_RMID:删除这片共享内存

把内容写到内存代码:

write.cpp文件:

#include

//#include

//#include

//#include

//#include

#include

#include

//#include

//#include

//#include

//#include

//#include

//#include

//#include

#include

#include

#include

//#include

//#include

//#include

using namespace std;

typedef struct{

char name[8];

int age;

} people;

#define IPCKEY 0x11

#define SHMNAME "shm_ram"

#define OPEN_FLAG O_RDWR|O_CREAT

#define OPEN_MODE 00777

#define FILE_SIZE 4096 * 4

#define LINE_SIZE 1024 * 3

int main() {

//char buff[LINE_SIZE];

//

//FILE* fp = fopen("sightline.xml", "r");

//if (NULL == fp) {

//cout << "open failure" << endl;

//return 0;

//}

//

//while(!feof(fp)){

//memset(buff, 0, LINE_SIZE);

//fgets(buff, 1024, fp);

//cout << buff;

//}

//

//fclose(fp);

cout << "Content-type: text/html; charset=\"utf-8\" \n\n";

int shm_id, i;

key_t key;

char temp[8];

people *p_map;

char pathname[30];

strcpy(pathname, ".");

key = ftok(pathname, 0);

if (key == -1) {

//perror("ftok error");

cout << "ftok error" << endl;

return 0;

}

cout << "key=" << key << endl;

//printf("key=%d\n", key);

shm_id = shmget(key, 1024 * 4, IPC_CREAT | 0666);

if (shm_id == -1) {

//perror("shmget error");

cout << "shmget error" << endl;

return 0;

}

//delete

shmctl(shm_id, IPC_RMID, NULL) ;

shm_id = shmget(key, 1024 * 4, IPC_CREAT | 0666);

if (shm_id == -1) {

//perror("shmget error");

cout << "shmget error" << endl;

return 0;

}

cout << "shm_id=" << shm_id << endl;

//printf("shm_id=%d\n", shm_id);

p_map = (people*)shmat(shm_id, NULL, 0);

memset(temp, 0x00, sizeof(temp));

strcpy(temp, "test");

temp[4] = '0';

for (i = 0; i < 7; i++) {

temp[4] += 1;

strncpy((p_map + i)->name, temp, 5);

(p_map + i)->age = 0 + i + 1;

}

shmdt(p_map);

cout << "success" << endl;

//int shm_id, i;

//key_t key;

//people *p_map;

char pathname[30];

strcpy(pathname, "/tmp");

//key = ftok("../test", IPCKEY);

//if (key == -1) {

cout << "ftok error");

//cout << "ftok error" << endl;

//return -1;

//}

printf("key=%d\n", key);

//cout << "key=" << key << endl;

//shm_id = shmget(key, 0, 0);

//if (shm_id == -1) {

cout << "shmget error");

//cout << "shmget error" << endl;

//return -1;

//}

printf("shm_id=%d\n", shm_id);

//cout << "shm_id=" << shm_id << endl;

//p_map = (people*) shmat(shm_id, NULL, 0);

//for (i = 0; i < 3; i++) {

printf("name:%s\n", (*(p_map + i)).name);

printf("age %d\n", (*(p_map + i)).age);

//cout << "name=" << (*(p_map + i)).name << endl;

//cout << "age=" << (*(p_map + i)).age << endl;

//}

//if (shmdt(p_map) == -1) {

//cout << "detach error");

//return -1;

//}

return 0;

}

编译  g++  write.cpp  -o write

执行 ./write

read.cpp

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

//#include

#include

#include

#include

#include

//#include

//#include

#include

#include

#include

#include

using namespace std;

typedef struct{

char name[8];

int age;

} people;

#define IPCKEY 0x11

#define SHMNAME "shm_ram"

#define OPEN_FLAG O_RDWR|O_CREAT

#define OPEN_MODE 00777

#define FILE_SIZE 4096 * 4

#define LINE_SIZE 1024 * 3

int main() {

//char buff[LINE_SIZE];

//

//FILE* fp = fopen("sightline.xml", "r");

//if (NULL == fp) {

//cout << "open failure" << endl;

//return 0;

//}

//

//while(!feof(fp)){

//memset(buff, 0, LINE_SIZE);

//fgets(buff, 1024, fp);

//cout << buff;

//}

//

//fclose(fp);

cout << "Content-type: text/html; charset=\"utf-8\" \n\n";

int shm_id, i;

key_t key;

people *p_map;

char pathname[30];

strcpy(pathname, ".");

key = ftok(pathname, 0);

if (key == -1) {

cout << "ftok error" << endl;

return 0;

}

cout << "key=" << key << endl;

shm_id = shmget(key, 0, 0);

if (shm_id == -1) {

cout << "shmget error" << endl;

return -1;

}

cout << "shm_id=" << shm_id << endl;

p_map = (people*) shmat(shm_id, NULL, 0);

for (i = 0; i < 3; i++) {

cout << "name=" << (*(p_map + i)).name << endl;

cout << "age=" << (*(p_map + i)).age << endl;

}

if (shmdt(p_map) == -1) {

cout << "detach error" << endl;

return -1;

}

return 0;

}

编译  g++  read.cpp  -o read

执行 ./read

c语言中读取内存的文件,c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例...相关推荐

  1. c++语言循环读写文件夹,在C++中逐行读取文件

    file.txt的内容包括: 5 3 6 4 7 1 10 5 11 6 12 3 12 4 其中,5 3是一个坐标对.如何在C++中逐行处理此数据? 我可以得到第一行,但如何得到文件的下一行? if ...

  2. c 语言如何处理表格文件中的数据库,C#程序从Excel表格中读取数据并进行处理

    今天做了一个Excel表格数据处理的事情,因为数据量表较大(接近7000条)所以处理起来有点麻烦,于是写了一个程序, 先将程序记下以便将来查找. using System; using System. ...

  3. R语言-批量读取数据文件以及提取字符串中的数字

    #第一部分 #先将当前文件夹下的所有以csv结尾的文件名读进来 filelist <- list.files(pattern=".*.csv") #文件个数 m<-le ...

  4. ccs读取dat文件c语言程序,TMS320DM642学习----第六篇(CCS中.dat文件类型详解)

    1.如下为.dat文件中文件头的基本格式: MagicNumber Format StartingAddress PageNum Length [NewFormat] 下面是分别的解释: MagicN ...

  5. 用c语言实现存储和读取图片文件,C++实现单张图片读取和保存

    使用C++实现对单张图片的读取和保存,C语言可以参考,比较简单. #include using namespace std; void main(void) { //保存输入图像文件名和输出图像文件名 ...

  6. POI:从Excel文件中读取数据,向Excel文件中写入数据,将Excel表格中的数据插入数据库,将数据库中的数据添加到Excel表

    POI 简介: POI是Apache软件基金会用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能. ...

  7. java使用缓冲区读取文件_在Java中使用Google的协议缓冲区

    java使用缓冲区读取文件 最近发布了 有效的Java第三版 ,我一直对确定此类Java开发书籍的更新感兴趣,该书籍的最新版本仅通过Java 6进行了介绍 . 在此版本中,显然存在与Java 7 , ...

  8. QFile和C语言对文件操作的性能比较.--读取double型二进制数据文件

    关键问题在于:QFile读取double型二进制数据流,只有两种方法处理数值. 一是通过QDataStream一个一个double读取,存储在一个QVector<double>中 二是通过 ...

  9. python让词向量一直在内存里_python读取大文件踩过的坑——读取txt文件词向量

    在读取https://github.com/Embedding/Chinese-Word-Vectors中的中文词向量时,选择了一个有3G多的txt文件,之前在做词向量时用的是word2vec,所以直 ...

最新文章

  1. Chemical Science | 基于金属的片段分子库用于筛选候选药物
  2. Linux Tensorflow2.0安装
  3. 听说过代码洁癖,还没听说过有 Bug 洁癖的?
  4. 经典面试题:用typeof来判断对象的潜在陷阱
  5. 【OpenCV入门教程之二】 一览众山小:OpenCV 2.4.8 or OpenCV 2.4.9组件结构全解析(转)...
  6. uniapp app中导出手机号码到通讯录
  7. Java mail 接受网易126和163邮件时数目不全
  8. 控制理论PID的理解
  9. 如何用cocos2d-x来开发简单的Uphone游戏:(三) 射击子弹 碰撞检测
  10. 四位共阳极数码管显示函数_74hc573可以驱动几位共阴数码管?74hc573驱动数码管应用解析...
  11. 本科英语计算机,计算机本科生英语简历范文
  12. Biotin-PEG2k-NHS,Biotin-PEG2000-NHS,PEG衍生物
  13. 电信客户流失数据分析(二)
  14. php常用函数wps,[WPSEC]——WP
  15. 如何获取显示器的EDID信息
  16. 解决myeclipse中 保存代码时resetting selection耗时操作
  17. LyX 发布撑持 CJK 的 1.5 正式版
  18. MASK_RCNN与YOLO系列算法初学者笔记(摘抄自其他)
  19. def fasterrcnn_resnet50_fpn()实例测试
  20. db2数据库date和timestamp日期转换to_char()函数

热门文章

  1. 如何在多台web服务器上共享session?
  2. c语言输出参数是out,关于C语言中的输出输入流
  3. 讲真,MySQL索引优化看这篇文章就够了
  4. es 精确查询不模糊_ES系列17:Terms聚合结果不精确,怎么破?
  5. linux系统udp通信程序,Linux UDP socket编程(UDP通讯模型) | C/C++程序员之家
  6. GSON序列化时,日期格式问题处理
  7. 【linux】 redhat中设置时区
  8. 【SSL】使用Keytool工具生成证书及签名完整步骤
  9. router-link标签学习
  10. 安装qgis显示python错误_ArcGIS 与 QGIS 3 冲突的解决方案