Presentation is loading. Please wait.

Presentation is loading. Please wait.

NCHU System & Network Lab Lab 11 Memory Mapped File.

Similar presentations


Presentation on theme: "NCHU System & Network Lab Lab 11 Memory Mapped File."— Presentation transcript:

1 NCHU System & Network Lab Lab 11 Memory Mapped File

2 NCHU System & Network Lab Introduction Previously, when we need to access a file –fopen(), fread(), fseek(), fwrite() Another promising approach –Memory mapped file Advantages –Easier to share file between two processes fopen( ) will lock the file, another process can’t access it. –More efficient Bypass the file system interface

3 NCHU System & Network Lab Memory Mapped File Mapped memory forms an association between a file and a process’s memory. The process can read and write the file's contents with ordinary memory access. In Linux, all device drivers appear as normal files.

4 NCHU System & Network Lab Memory Mapped File process memory file Memory Map Access memory

5 NCHU System & Network Lab Memory Mapping #include void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off); Symbolic ConstantDescription PROT_READData can be read. PROT_WRITEData can be written. PROT_EXECData can be executed. PROT_NONEData cannot be accessed. addr : start address, usually be 0. len : mapped memory length, usually equal to file length. prot: flags: Symbolic ConstantDescription MAP_SHAREDChanges are shared. MAP_PRIVATEChanges are private. MAP_FIXEDInterpret addr exactly. fildes : a file descriptor. off : the offset from the beginning of the file. return : the address at which the mapping was placed. a value of MAP_FAILED and set errno to indicate the error.

6 NCHU System & Network Lab Memory Mapping file mapped part start addr = 0, offset = 0, length = file size start addr = 0, offset = d, length = l d l

7 NCHU System & Network Lab unmapping #include int munmap(void *addr, size_t len); addr: start address. len : length of the mapped memory region. return 0 for success, -1 for error

8 NCHU System & Network Lab Example – mmap()

9 NCHU System & Network Lab

10

11

12

13 Example – fopen()

14 NCHU System & Network Lab

15

16

17

18

19 Lab I A program that –user can input messages continuously –Program should record these messages into a file –Use mmap() approach instead of fopen() –Be care : The memory point that is returned by mmap() will always point to the start of mapped memory region. You should change it’s position by yourself.

20 NCHU System & Network Lab Read file by mmap()

21 NCHU System & Network Lab

22

23

24 Lab II Write two program use mmap() that –Consist of two concurrent program –Program 1 : user can input messages continuously and write the messages to a file. –Program 2 : read the messages from that file and show it on the screen continuously. –Be care: race condition

25 NCHU System & Network Lab Reference Advanced Programming in the UNIX Environment 2nd Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley Beginning Linux Programming Author : Richard Stones, Neil MatthewPublisher : Wrox Operating System Concepts 6th edition Author : Abraham Silberschatz, Peter Baer Galvin, Greg Gagne Publisher : John Wiley & Sons, inc. Google LINUX MAN PAGES ONLINE


Download ppt "NCHU System & Network Lab Lab 11 Memory Mapped File."

Similar presentations


Ads by Google