#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct{
char name[4];
int age;
}people;
main(int argc,char** argv){
int fd,j;
people *p_map;
char temp;
fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
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);
close(fd);
temp = 'a';
for(j=0;j<10;j++){
temp += 1;
memcpy((*(p_map+j)).name,&temp,2);
(*(p_map+j)).age = 10 +j;
}
printf("initialize over \n");
sleep(10);
munmap(p_map,sizeof(people)*10);
printf("umap ok \n");
}
============================================
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
type struct{
char name[4];
int age;
}people;
main(int argc,char** argv){
int fd,j;
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(j=0,j<10;j++){
printf("name: %s age %d ;\n",(*(p_map+j)).name,(*(p_map+j)).age);
}
munmap(p_map,sizeof(people)*10);
}
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct{
char name[4];
int age;
}people;
main(int argc,char** argv){
int fd,j;
people *p_map;
char temp;
fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
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);
close(fd);
temp = 'a';
for(j=0;j<10;j++){
temp += 1;
memcpy((*(p_map+j)).name,&temp,2);
(*(p_map+j)).age = 10 +j;
}
printf("initialize over \n");
sleep(10);
munmap(p_map,sizeof(people)*10);
printf("umap ok \n");
}
============================================
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
type struct{
char name[4];
int age;
}people;
main(int argc,char** argv){
int fd,j;
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(j=0,j<10;j++){
printf("name: %s age %d ;\n",(*(p_map+j)).name,(*(p_map+j)).age);
}
munmap(p_map,sizeof(people)*10);
}