在我的博客(
传送门)里详细描述了mmap实现内存共享的手段,这里只是贴出需要的代码,谢谢:
normalfile1.c
/*
* Copyright (c) 2010-~ zhouyongfei
*
* The source code is released for free distribution under the terms of the GNU General Public License
*
*
* Author: alen Chou
* Created Time: 2010年08月15日 星期日 11时24分36秒
* File Name: normalfile.c
* Description:
* 使用mmap映射文件到内存空间,向内存空间写入数据,同时也会对相应的文件进行相应的操作。
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
typedef struct{
char name[4];
int age;
}people;
int main(int argc, char *argv[])
{
int fd,i;
people *p_map;
char temp;
fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,0777);
lseek(fd,sizeof(people)*5 - 1,SEEK_SET);//设置文件的大小
write(fd,"",1);
p_map = (people *)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
//注意,mmap只能在进程中映射这么大的空间,而不能改变文件的大小
close(fd);
temp = 'a';
for(i =0;i<10;i++){
temp += 1;
memcpy((*(p_map+i)).name,&temp,1);
(*(p_map+i)).age = 20 + i;
}
printf("initialize over\n");
sleep(10);
munmap(p_map,sizeof(people)*10);
printf("unmap OK\n");
return 0;
}
|
normalfile2.c
/*
* Copyright (c) 2010-~ zhouyongfei
*
* The source code is released for free distribution under the terms of the GNU General Public License
*
*
* Author: alen Chou
* Created Time: 2010年08月15日 星期日 11时33分35秒
* File Name: normalfile2.c
* Description:
* 使用mmap映射shmtmp文件到内存区域,并读取这块内存上的东西,相当于读取文件中的东西。
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct {
char name[4];
int age;
}people;
int main(int argc, char *argv[])
{
int fd,i;
people *p_map;
fd = open(argv[1],O_CREAT|O_RDWR,00777);
p_map = (people *)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
for(i = 0;i < 10;i++){
printf("name:%s age: %d;\n ",(*(p_map+i)).name,(*(p_map+i)).age);
}
munmap(p_map,sizeof(people)*10);
return 0;
}
|
normalfile4.c
/*
* Copyright (c) 2010-~ zhouyongfei
*
* The source code is released for free distribution under the terms of the GNU General Public License
*
*
* Author: alen Chou
* Created Time: 2010年08月15日 星期日 15时30分39秒
* File Name: normalfile4.c
* Description:
* 父子进程间通过匿名映射实现共享内存
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
typedef struct{
char name[4];
int age;
}people;
main(int argc, char** argv)
{
int i;
people *p_map;
char temp;
p_map=(people*)mmap(NULL,sizeof(people)*10,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);//fd = -1 表示只是创建内存区域,不对文件进行映射。
if(fork() == 0)
{
sleep(2);
for(i = 0;i<5;i++)
printf("child read: the %d people's age is %d\n",i+1,(*(p_map+i)).age);
(*p_map).age = 100;
munmap(p_map,sizeof(people)*10); //实际上,进程终止时,会自动解除映射。
exit(0);
}
temp = 'a';
for(i = 0;i<5;i++)
{
temp += 1;
memcpy((*(p_map+i)).name, &temp,2);
(*(p_map+i)).age=20+i;
}
sleep(5);
printf( "parent read: the first people,s age is %d\n",(*p_map).age );
printf("umap\n");
munmap( p_map,sizeof(people)*10 );
printf( "umap ok\n" );
}
|
阅读(2901) | 评论(0) | 转发(0) |