Using the file memory mapping under Linux can realize the process sharing data, we can map a file to virtual memory to enable multiple processes to share,
We can probably think of a wide range of areas that he can apply to here.
It mainly involves the application of three functions of Mmap Munmap Msync
Paste the following code
The following code establishes a simple record store for a file and modifies the contents of the file through a memory map
/*************************************************************************> File name:memdb.c> Author: > Mail: > Created time:fri 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"//define data record 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 the 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; " > We can view the contents of shared files in another process </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 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"//define data record 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 the 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 under C programming-----io/file operation/memory mapping for simple record storage (3)