#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static int alloc_size;
static char *memory;
int main()
{
int fd;
fd = open("/dev/zero",O_RDONLY);
alloc_size = getpagesize(); //mmap必须以PAGE_SIZE()为单位映射
memory = mmap(NULL,alloc_size,PROT_WRITE,MAP_PRIVATE,fd,0); //可以在两个进程都映射该文件时实现共享内存
close(fd);
memory[0] = 1;
//mprotect(memory,alloc_size,PROT_NONE);
memory[0] = 0;
printf("all done\n");
munmap(memory,alloc_size);
return 0;
}
mprotect
最新推荐文章于 2022-10-13 13:47:39 发布