当前位置: 技术问答>linux和unix
一个小问题:关于共享内存的例子中的一个用法
来源: 互联网 发布时间:2016-03-24
本文导语: #include #include #include #include typedef struct{ char name[4]; int age; }people; int main( int argc, char * argv[] ) // map a normal file as shared mem: { int fd,i; people *p_map; char temp; fd = open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777); ...
#include
#include
#include
#include
typedef struct{
char name[4];
int age;
}people;
int main( int argc, char * argv[] ) // map a normal file as shared mem:
{
int fd,i;
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(i=0; i
#include
#include
#include
typedef struct{
char name[4];
int age;
}people;
int main( int argc, char * argv[] ) // map a normal file as shared mem:
{
int fd,i;
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(i=0; i