zoukankan      html  css  js  c++  java
  • Linux进程虚拟地址空间

    在linux中,各个进程的虚拟地址空间是独立的。各个进程的虚拟地址空间起始于0,延伸至TASK_SIZE -1.

    在32 bit系统中,内核分配1GB,而各个用户空间进程可用的部分为3GB。

    进程虚拟地址空间由若干个区域组成,

    1.当前运行代码的二进制代码.text段。

    2.程序使用的动态库代码。

    3.存储区局变量和静态变量的数据段,bss,data段

    4.保存动态分配数据的堆

    5.保存局部变量和实现函数 调用的栈

    6.环境变量和命令行参数。

    7.文件内容映射到虚拟地址空间的内存映射。

    具体布局如下图所示,

    如果全局变量randomize_va_space设置为1,那么启用地址空间随机化机制(上图的ramdom xxx offset)。用户可以通过/proc/sys/kernel/randomize_va_space停用该特性。

    每个进程都有mm_struct(linux/mm_types.h)的实例,保存进程虚拟内存管理信息。

    struct mm_struct {
    struct vm_area_struct *mmap; /* list of VMAs */
    struct rb_root mm_rb;
    #ifdef CONFIG_MMU
    unsigned long (*get_unmapped_area) (struct file *filp,unsigned long addr, unsigned long len,unsigned long pgoff, unsigned long flags);
    #endif
    unsigned long mmap_base; /* base of mmap area */虚拟地址空间中用于内存映射的起始地址。
    unsigned long mmap_legacy_base; /* base of mmap area in bottom-up allocations */
    unsigned long task_size; /* size of task vm space */进程地址空间的size.

    struct list_head mmlist; /* List of maybe swapped mm's. These are globally strung

    unsigned long start_code, end_code, start_data, end_data;
    unsigned long start_brk, brk, start_stack;//堆首地址,堆尾地址,栈首地址。
    unsigned long arg_start, arg_end, env_start, env_end;

    ....

    };

    进程虚拟地址空间由多个VMA组成。有两种组织VMA的方式,链表(mmap)和红黑树(mm_rb)

    VMA结构体如下:

    struct vm_area_struct {
      /* The first cache line has the info for VMA tree walking. */

      unsigned long vm_start; /* Our start address within vm_mm. */
      unsigned long vm_end; /* The first byte after our end address within vm_mm. */

      /* linked list of VM areas per task, sorted by address */
      struct vm_area_struct *vm_next, *vm_prev;

      struct rb_node vm_rb;

      struct mm_struct *vm_mm; /* The address space we belong to. */

      /* Function pointers to deal with this struct. */
      const struct vm_operations_struct *vm_ops;

      struct file * vm_file; /* File we map to (can be NULL). */
      void * vm_private_data; /* was vm_pte (shared mem) */

    };

    VMA链表组织形式如下图:

     VMA红黑树组织形式如下:

     

  • 相关阅读:
    游戏运营-三节课
    游戏运营-游戏付费
    游戏运营--资深
    django 导出csv
    django-分页
    django-中间件
    django-缓存
    django post 与get请求理解
    django 中遇到的问题
    Linux下使用XAMPP
  • 原文地址:https://www.cnblogs.com/fellow1988/p/6220710.html
Copyright © 2011-2022 走看看