标签:
#include <unistd.h>
#include <sys/mman.h>
void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
值 |
说明 |
PROT_NONE |
不允许访问 |
PROT_READ |
内存可读 |
PROT_WRITE |
内存可写 |
PROT_EXEC |
内存可执行 |
值 |
说明 |
MAP_FIXED |
如果start无效或者正在使用则失败 |
MAP_PRIVATE |
对映像内存区域的写入操作是进程私有的 |
MAP_SHARED |
对映像内存区域的写入操作也被复制到文件 |
int munmap(void *start, size_t length)
int main() { int fdin = open("aaa.txt", O_RDONLY); struct stat statbuf; fstat(fdin, &statbuf); off_t len = statbuf.st_size; char *s = mmap(0, len, PROT_READ, MAP_SHARED, fdin, 0); printf(s); munmap(s, len); close(fdin); return 0; }
int msync(const void *start, size_t length, int flags)
int main() { int fdin = open("aaa.txt", O_RDWR); struct stat statbuf; fstat(fdin, &statbuf); off_t len = statbuf.st_size; char *s = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED, fdin, 0); printf(s); strcpy(s, "hello world"); msync(s, len, MS_SYNC); munmap(s, len); close(fdin); return 0; }
int mlock(const void *start, size_t len);
int munlock(void *start, size_t len);
int mlock(const void *start, size_t len);
int munlock(void *start, size_t len);
标签:
原文地址:http://www.cnblogs.com/shichuan/p/4496182.html