mprotect

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
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;
}

你可能感兴趣的:(踩内存)