Mmap system call operation that is able to access memory locations
问题 I am writing a program that allocates huge chunks of memory using mmap and then accesses random memory locations to read and write into it. I just tried out the following code: #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> int main() { int fd,len=1024*1024; fd=open("hello",O_READ); char*addr=mmap(0,len,PROT_READ+PROT_WRITE,MAP_SHARED,fd,0); for(fd=0;fd<len;fd++) putchar(addr[fd]); if (addr==MAP_FAILED) {perror("mmap"); exit(1);} printf("mmap returned %p, which seems readable