c++从内存中读取文件内容,内容写到内存 实现文件的内存共享代码实例

使用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   <string.h>
//#include   <math.h>
//#include   <climits>
//#include   <regex.h>
//#include   <sstream>
#include   <iostream>
#include   <stdio.h>
//#include   <list>
//#include   <vector>
//#include   <time.h>
//#include   <set>
//#include   <algorithm>
//#include   <iterator>
//#include   <sys/time.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/types.h>
//#include 	<unistd.h>
//#include 	<string.h>
//#include   <io.h>


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   <string.h>
#include   <math.h>
#include   <climits>
#include   <regex.h>
#include   <sstream>
#include   <iostream>
#include   <stdio.h>
#include   <list>
#include   <vector>
#include   <time.h>
#include   <set>
#include   <algorithm>
#include   <iterator>
#include   <sys/time.h>
#include	<fstream>
#include	<iomanip>
//#include 	<stdio.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/types.h>
#include 	<unistd.h>
//#include 	<string.h>
//#include   <io.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>


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

来源://作者:/更新时间:2014-03-18
相关文章
评论:
验证码:
匿名评论: