1. 程式人生 > >Linux下C程式設計-----IO/檔案操作/記憶體對映 實現簡單記錄儲存(3)

Linux下C程式設計-----IO/檔案操作/記憶體對映 實現簡單記錄儲存(3)

利用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);
}