#include
#include
#include
#include
#include
#include
typedef struct
{
char name[4];
int age;
}person;
int main (){
pid_t pid;
int i;
person *p_map;
char temp;
p_map = (person*)mmap(NULL,sizeof(person)*10,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0);
pid = fork();
if(pid<0)
{
perror("fork failed!\n");
return 0;
}else if(pid == 0)
{
sleep(2);
for(i = 0;i < 5;i++)
printf("子进程读取:第%d个人的年龄是:%d\n",i+1,(*(p_map+i)).age);
(*(p_map)).age = 110;
munmap(p_map,sizeof(person)*10);
return 0;
}else
{
temp = 'a';
for(i = 0; i<5; i++)
{
temp += 1;
memcpy((*(p_map+i)).name, &temp, 2);
(*(p_map+i)).age = 30 +i;
}
sleep(5);
printf("父进程读取:五个人的年龄和是:%d\n",(*p_map).age);
printf("解除内存映射....\n");
munmap(p_map,sizeof(person)*10);
printf("解除内存映射成功\n");
}
}
阅读(1064) | 评论(0) | 转发(0) |