6-2.c

来自「linux下一些命令的c语言的实现」· C语言 代码 · 共 46 行

C
46
字号
/* This program locks the virtual memory address that */
/* was returned from the mmap() function into memory. */
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
void  main()
{
int fd;
caddr_t pg_addr;
int size = 5000;
int mode =  S_IRWXO|S_IRWXG|S_IRWXU;
     /* Create a file */
fd = shm_open("example", O_RDWR|O_CREAT, mode);
if(fd < 0){
   perror("open error ");
   exit();
}
   	/* Set the size */
if((ftruncate(fd, size)) == -1){
     perror("ftruncate failure");
     exit();
}
   /* Map the file into the address space of the process */
pg_addr = (caddr_t) mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED,fd, 0);
if(pg_addr == (caddr_t) -1){
  perror("mmap failure");
  exit();
}
/* Lock the mapped region into memory */
if(mlock(pg_addr,size) != 0){
  perror("mlock failure");
  exit();
}
     /* Unmap of the address region removes the memory lock */
 /* established on the address region by this process   */

if(munmap(pg_addr, size) < 0)
   perror("unmap error");
close(fd);
shm_unlink("example");
exit();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?