mmap示例_mmap教程,带有C和C ++编程语言中的示例

这篇博客介绍了mmap()函数的功能,它在Linux系统中用于虚拟内存映射。mmap()允许程序员创建映射到资源的地址空间,可用于共享或非共享内存映射。文章提供了相关库的引用、语法说明,并给出了一个映射4096字节内存页面的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mmap示例

mmap示例

As programmers we generally use malloc() , free() and similar functions in order to allocate memory. They are provided by glibc() library. The actual work is done by mmap() and munmap()which is a Linux systemcall.

作为程序员,我们通常使用malloc()free()和类似函数来分配内存。 它们由glibc()库提供。 实际工作由mmap()munmap() ,这是Linux系统调用。

mmap()有什么功能? (What Does mmap() Function?)

mmap() function or system call will create a mapping in the virtual meory of the current process.The address space consist of multiple pages and each page can be mapped some resource. We can create this mapping for a resources we want to use.

mmap()函数或系统调用将在当前进程的虚拟内存中创建一个映射。地址空间由多个页面组成,每个页面都可以映射一些资源。 我们可以为要使用的资源创建此映射。

图书馆 (Library)

mmap() and munmap() functions are provided by sys/mman.h library. so in order to use we need to include them like below.

mmap()munmap()函数由sys/mman.h库提供。 因此,要使用它,我们需要像下面这样包含它们。

#include <sys/mman.h>

句法 (Syntax)

As mmap() provides flexible memory mapping it has a lot of parameters to use.

由于mmap()提供了灵活的内存映射,因此它具有许多要使用的参数。

void *mmap(void *addr, size_t lengthint " prot ", int " flags ,
           int fd, off_t offset)
  • void *addr is the address we want to start mapping

    void *addr是我们要开始映射的地址

  • size_t lengthint is the size we want to map in as integer

    size_t lengthint是我们要映射为整数的大小

  • PROT_READ|PROT_WRITE|PROT_EXEC options about page

    关于页面的PROT_READ|PROT_WRITE|PROT_EXEC选项

  • MAP_ANON|MAP_PRIVATE options about page

    关于页面的MAP_ANON|MAP_PRIVATE选项

内存映射类型(Memory Mapping Types)

We have two option about memory mapping for sharing.

关于共享的内存映射,我们有两个选择。

  • MAP_SHARED will map given page and this will be also visible by other processes.

    MAP_SHARED将映射给定的页面,其他进程也将看到它。

  • MAP_PRIVATE will map given page and this will not visible to other processes.

    MAP_PRIVATE将映射给定的页面,而这对于其他进程将不可见。

(Example)

Here is an example which takes a page from start of 2^20 . The default size of the page is 4096 byte so we will map a page with 4096 byte memory.

这是一个从2^20开始的页面示例。 页面的默认大小是4096字节,因此我们将映射一个具有4096字节内存的页面。

#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

int main(void) {
  size_t pagesize = getpagesize();

  printf("System page size: %zu bytes\n", pagesize);

  char * region = mmap(
    (void*) (pagesize * (1 << 20)),   // Map from the start of the 2^20th page
    pagesize,                         // for one page length
    PROT_READ|PROT_WRITE|PROT_EXEC,
    MAP_ANON|MAP_PRIVATE,             // to a private block of hardware memory
    0,
    0
  );
  if (region == MAP_FAILED) {
    perror("Could not mmap");
    return 1;
  }

  strcpy(region, "Hello, poftut.com");

  printf("Contents of region: %s\n", region);

  int unmap_result = munmap(region, 1 << 10);
  if (unmap_result != 0) {
    perror("Could not munmap");
    return 1;
  }
  // getpagesize
  return 0;
}

When we compile with the following command the a.out executable will be generated.

当我们使用以下命令编译时,将生成a.out可执行文件。

$ gcc main.c
mmap() Example
mmap() Example
mmap()示例
LEARN MORE  How To Download, Compile and Install Custom Linux Kernel Manually In Ubuntu, Debian, Mint, Kali, CentOS?
了解更多如何在Ubuntu,Debian,Mint,Kali,CentOS中手动下载,编译和安装自定义Linux内核?

翻译自: https://www.poftut.com/mmap-tutorial-with-examples-in-c-and-cpp-programming-languages/

mmap示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值