我尝试了一个简单的mmap
相关 C 代码段:
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int fd;
char* mapped_mem;
int flength = 1024;
void* start_addr = 0;
if (argc < 2) {
printf("usage: %s filename\n", argv[0]);
exit(1);
}
fd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
flength = lseek(fd, 1, SEEK_END);
write(fd, "\0", 1);
lseek(fd, 0, SEEK_SET);
printf("fd=%d, flength=%d\n", fd, flength);
mapped_mem = mmap(start_addr, flength, PROT_READ, MAP_PRIVATE, fd, 0);
printf("%s\n", mapped_mem);
close(fd);
munmap(mapped_mem, flength);
return 0;
}
但是我发现每次在数据文件上执行它时,都会^@^@
附加到这个文件上。发生什么了?