Linux下C编程

利用linux下的文件内存映射可以实现进程共享数据,我们可以把一个文件映射到虚拟内存中使多个进程进行共享,

到这里我们大概能想到他能应用到的领域 是很广泛的

主要涉及到 mmap munmap msync 三个函数的应用

下面贴代码

下面一段代码是为文件建立一个简单的记录存储,并且通过内存映射修改文件内容

/*************************************************************************> File Name: memdb.c> Author: > Mail: > Created Time: Fri 13 Feb 2015 03:46:11 AM PST ************************************************************************/#include<stdio.h>#include<sys/mman.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#define RECORD_NUM 100#define DATA_FILE  "./db.dat"//定义数据记录typedef struct {   int index ;   char str[20];}RecordData;int main(){    FILE*pFile ;    void*pMap=NULL;    int fdFIle;    int i;    RecordData record,*pMappedRecord;      pFile=fopen(DATA_FILE,"w+") ;    if(pFile==NULL)    {        printf("Create File Error\n") ;        return 0;    }   for(i=0;i<RECORD_NUM;i++)   {      record.index=i ;      sprintf(record.str,"Record:%d",i);      fwrite((void*)&record,sizeof(record),1,pFile);   }   fclose(pFile) ;   fdFIle =open(DATA_FILE,O_RDWR) ;    if(fdFIle==-1)    {        printf("Open DataFile Error!\n");        return 0 ;    }    //map file to memory  from file offset 0 to end     pMap=mmap(NULL,sizeof(RecordData)*RECORD_NUM,PROT_READ|PROT_WRITE,MAP_SHARED,fdFIle,0)  ;    if(pMap==MAP_FAILED)    {        printf("Map file to Virtual Memory Error!\n");        close(fdFIle);        return 0;    }    ///get  thirty  record     pMappedRecord=(struct RecordData*)pMap ;    printf("%d:%s\n",pMappedRecord[29].index,pMappedRecord[29].str);        //modify thirty data     sprintf(pMappedRecord[29].str,"%s","Hello,Usher");    //update mapped memory info to file     msync(pMap,sizeof(RecordData)*RECORD_NUM,MS_ASYNC);    munmap(pMap,sizeof(RecordData)*RECORD_NUM);}
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">我们在另一个进程中可以通过程序查看共享文件内容</span>
</pre><pre code_snippet_id="604510" snippet_file_name="blog_20150213_6_8975773" name="code" class="cpp">/*************************************************************************> File Name: memdb.c> Author: > Mail: > Created Time: Fri 13 Feb 2015 03:46:11 AM PST ************************************************************************/#include<stdio.h>#include<sys/mman.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#define RECORD_NUM 100#define DATA_FILE  "./db.dat"//定义数据记录typedef struct {   int index ;   char str[20];}RecordData;int main(){      int i;    void*pMap=NULL;    int fdFIle;    RecordData record,*pMappedRecord;     fdFIle =open(DATA_FILE,O_RDWR) ;    if(fdFIle==-1)    {        printf("Open DataFile Error!\n");        return 0 ;    }    //map file to memory  from file offset 0 to end     pMap=mmap(NULL,sizeof(RecordData)*RECORD_NUM,PROT_READ|PROT_WRITE,MAP_SHARED,fdFIle,0)  ;    if(pMap==MAP_FAILED)    {        printf("Map file to Virtual Memory Error!\n");        close(fdFIle);        return 0;    }    ///get  thirty  record     pMappedRecord=(struct RecordData*)pMap ;    for(i=0;i<RECORD_NUM;i++)     printf("%d:%s\n",pMappedRecord[i].index,pMappedRecord[i].str);         munmap(pMap,sizeof(RecordData)*RECORD_NUM);}

追寻爱情,然后发现,爱,从来就是一件千回百转的事。

Linux下C编程

相关文章:

你感兴趣的文章:

标签云: