虚拟内存中的各个段详解

虚拟内存的一个基本“法则”是:所有潜在可寻址内存在一个盒子里; 也就是说,它是沙盒的。 我们将这个“盒子”视为process image或进程的虚拟地址空间。 查看盒子以外的空间是不被允许的。

进程的虚拟地址被划分为相同的内存区域称为段,或者更专业地称为映射。 每个 Linux 进程至少有这些映射(或段):

 让我们快速分析一下这些段或映射:

Text segment: This is where the machine code is stored; static (mode: rx).

//文本段:存放的是进程的代码;所在的虚拟内存空间为读和执行。

Data segment(s): This is where the global and static data variables are stored (mode: rw-). It is internally divided into three distinct segments(全局变量和静态变量存储的位置,被继续划分为以下三段):

  • Initialized data segment: Pre-initialized variables are stored here; static(提前初始化好的变量存放的位置).
  • Uninitialized data segment: Uninitialized variables are stored here (they are auto-initialized to 0 at runtime; this region is sometimes called the bss); static(未初始化的变量存放的位置,这个区域也叫bss).
  • Heap segment: The library APIs for memory allocation and freeing (the familiar malloc(3) family of routines) get memory from here. That's also not completely true. On modern systems, only malloc() instances below MMAP_THRESHOLD (128 KB by default) get their memory from the heap. Any higher and it's allocated as a separate 'mapping' in the process VAS (via the powerful mmap(2) system call). It is a dynamic segment (it can grow/shrink). The last legally referenceable location on the heap is referred to as the program break(用于内存分配和释放的库 API 例如常用的malloc()函数就是从这里获取内存,但也不完全正确,在现代操作系统中,只有在malloc()函数申请的内存小于MMAP_THRESHOLD(默认128KB)的时候才从heap中获取,在进程的虚拟内存中申请更多分开进行映射的的内存,会通过mmap()系统调用来完成,Heap这是一个可伸缩的段,Heap上最后一个合法可引用的位置称为程序中断).

Libraries (text, data): All shared libraries that a process dynamically links into are mapped (at runtime, via the loader) into the process VAS (mode: r-x/rw-)(进程运行时动态加载的库所在的段).

Stack: A region of memory that uses the Last In, First Out (LIFO) semantics; the stack is used for the purpose of implementing a high-level language's function-calling mechanism. It includes parameter passing, local variable instantiation (and destruction), and return value propagation. It is a dynamic segment. On all modern processors (including the x86 and ARM families), the stack 'grows' toward lower addresses (called a fully descending stack). Every time a function is called, a stack frame is allocated and initialized as required; the precise layout of a stack frame is very CPU dependent (you must refer to the respective CPU Application Binary Interface (ABI) document for this; see the Further reading section for references). The SP register (or equivalent) always points to the current frame, the top of the stack; as stacks grow towards lower (virtual) addresses, the top of the stack is actually the lowest (virtual) address! It's non-intuitive but true (mode: rw-)(用于高级语言的函数调用的段,包含参数传递,变量初始化,返回参数等)

进程至少要包含一个线程,如果只有一个线程,通常就是指的main()函数,如上图所示有main、thrd2、thrd3三个线程,这三个线程共享除stack段以外的所有内容,每个线程都拥有专属的stack段,如上图显示的main线程用户虚拟内存最顶端的stack段,thrd2、thrd3拥有stack段与Libraries段中间蓝色方框的stack段。

进程虚拟内存查看方法

[root@ct7_node02]# cat /proc/2421/maps
00400000-00401000 r-xp 00000000 fd:01 7316117                            /tmp/a.out
00600000-00601000 r--p 00000000 fd:01 7316117                            /tmp/a.out
00601000-00602000 rw-p 00001000 fd:01 7316117                            /tmp/a.out
7f5604f92000-7f5605156000 r-xp 00000000 fd:01 62061                      /usr/lib64/libc-2.17.so
7f5605156000-7f5605355000 ---p 001c4000 fd:01 62061                      /usr/lib64/libc-2.17.so
7f5605355000-7f5605359000 r--p 001c3000 fd:01 62061                      /usr/lib64/libc-2.17.so
7f5605359000-7f560535b000 rw-p 001c7000 fd:01 62061                      /usr/lib64/libc-2.17.so
7f560535b000-7f5605360000 rw-p 00000000 00:00 0 
7f5605360000-7f5605382000 r-xp 00000000 fd:01 62054                      /usr/lib64/ld-2.17.so
7f56054b2000-7f5605579000 rw-p 00000000 00:00 0 
7f5605580000-7f5605581000 rw-p 00000000 00:00 0 
7f5605581000-7f5605582000 r--p 00021000 fd:01 62054                      /usr/lib64/ld-2.17.so
7f5605582000-7f5605583000 rw-p 00022000 fd:01 62054                      /usr/lib64/ld-2.17.so
7f5605583000-7f5605584000 rw-p 00000000 00:00 0 
7ffd3ce15000-7ffd3ce36000 rw-p 00000000 00:00 0                          [stack]
7ffd3cf50000-7ffd3cf54000 r--p 00000000 00:00 0                          [vvar]
7ffd3cf54000-7ffd3cf56000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0                  [vsyscall]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值