



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




First copy data to a kernel's intermediary buffer Then transfer data to the physical file or the process
Eliminate intermediary buffering Significantly improve performance
Memory Mapped File In Blocks VM of User 1 mmap requests Disk File Blocks of data From file mapped To VM VM of User 2 Blocks of data From file mapped To VM
Map executable files and shared libraries into address space Programs can load and unload executable code sections dynamically
#include <sys/mman.h> void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off); Memory map a file Establish a mapping between the address space of the process to the memory object represented by the file descriptor Parameters: addr: the address to map the file into len: the length of the data to map into memory prot: the kind of access to the memory mapped region flags: flags that can be set for the system call fd: file descriptor off: the offset in the file to start mapping from
#include <sys/mman.h> void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off); Memory map a file Establish a mapping between the address space of the process to the memory object represented by the file descriptor Return value On success, Implementation-defined function of addr and flags On failure, MAP_FAILED
#include <sys/mman.h> int msync(void *addr, size_t len, int flags); Write all modified data to permanent storage locations Return value 0 on success - 1 on error, sets errno Parameters: addr: returned from mmap() len: same as the len passed to mmap() flags: MS_ASYNC = Perform asynchronous writes MS_SYNC = Perform synchronous writes MS_INVALIDATE = Invalidate cached data
#include <unistd.h> long sysconf(int name); Determine the current value of a configurable system variable Return value 0 on success - 1 on error, sets errno Parameters: name: the system variable to be queried _SC_PAGESIZE
#include <errno.h > #include <fcntl.h > #include <unistd.h > #include <stdio.h > #include <stdlib.h > #include <string.h > #include <sys/types.h > #include <sys/mman.h > main(void) { size_t bytesWritten =0; int fd; int PageSize; const char text = "This is a test";
if ((PageSize = sysconf(_SC_PAGE_SIZE)) < 0) { perror("sysconf() Error="); return - 1; } fd = open("/tmp/mmsyncTest", (O_CREAT | O_TRUNC | O_RDWR), (S_IRWXU | S_IRWXG | S_IRWXO)); if (fd < 0) { perror("open() error"); return fd; } off_t lastoffset = lseek(fd, PageSize, SEEK_SET); bytesWritten = write(fd, " ", 1 ); if (bytesWritten != 1 ) { perror("write error. "); return - 1; }
/* mmap the file. */ void address; int len; off_t my_offset = 0; len = PageSize; / Map one page */ address = mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, my_offset); if (address == MAP_FAILED) { perror("mmap error. “); return - 1; }
/* Move some data into the file using memory map. / (void) strcpy((char) address, text); /* use msync to write changes to disk. */ if (msync(address, PageSize , MS_SYNC) < 0 ) { perror("msync failed with error:"); return - 1; } else (void) printf("%s","msync completed successfully."); close(fd); unlink("/tmp/msyncTest"); }