#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

int main(int argc,char *argv[])
{

  if(argc != 2)
  {
    printf("error args\n");
    return -1;
  }
  int fd = open(argv[1],O_RDWR);
  struct stat s;
  fstat(fd,&s);
  char *p;
  p = (char*)mmap(NULL,s.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
  if((char*)-1 == p)
  {
    perror("mmap");
    return -1;
  }
  while(1);
  p[0] = 'H';
  int ret = munmap(p,s.st_size); 
  if(-1 == ret)
  {
    perror("mnumap");
  }
  

  return 0;
}

演示:映射1个g的文件,程序异常退出,并没有运行munmap函数,文件映射的内存会一直存在,删除此文件后,内存会被清理

参考:https://www.v2ex.com/t/278921

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2021-11-04
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2020-10-08
  • 2022-12-23
  • 2021-06-05
相关资源
相似解决方案