⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6-2.c

📁 这是Linux应用开发技术详解上的源码
💻 C
字号:
/* 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -