为 Linux 进程分配的堆栈内存在哪里? [英] Where is the stack memory allocated from for a Linux process?

查看:14
本文介绍了为 Linux 进程分配的堆栈内存在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,当创建一个进程时,会为该进程分配一个堆栈.堆栈的大小在 linux 中通常为 8 Mb.我的问题是,这个堆栈是从哪里分配的??从用户空间还是从系统空间?

We know that when a process is created,one stack is allocated for this process.The size of the stack is typically 8 Mb in linux.My question is that,from where this stack is allocated??From user space or from system space?

推荐答案

首先要了解什么是分页和页面错误:x86 分页是如何工作的?

First you must understand what paging and page faults are: How does x86 paging work?

内核与进程内存

Linux 内核保留两个虚拟内存区域:

The Linux Kernel reserves two zones of virtual memory:

  • 一个用于内核内存
  • 一个用于程序

确切的拆分由 CONFIG_VMSPLIT_... 配置.默认情况下:

The exact split is configured by CONFIG_VMSPLIT_.... By default:

  • 在 32 位上:

  • on 32-bit:

  • 底部3/4是程序空间:00000000BFFFFFFF
  • 前1/4是内核内存:C0000000FFFFFFFF

像这样:

------------------ FFFFFFFF
Kernel
------------------ C0000000
------------------ BFFFFFFF


Process


------------------ 00000000

  • 在 64 位上:当前仅实际使用 48 位,分成两个大小相等的不相交空间.Linux 内核只是分配:

  • on 64-bit: currently only 48-bits are actually used, split into two equally sized disjoint spaces. The Linux kernel just assigns:

    • 底部处理00000000 00000000008FFFFF FFFFFFFF
    • 内核的顶部:FFFF8000 00000000FFFFFFFF FFFFFFFF

    像这样:

    ------------------ FFFFFFFF FFFFFFFF
    Kernel
    ------------------ FFFF8000 00000000
    
    
    (not addressable)
    
    
    ------------------ 008FFFFF FFFFFFFF
    Process
    ------------------ 00000000 00000000
    

  • 进程地址空间

    简化程序虚拟进程的内存:

    ------------------ <--- Top of the process address space
    Stack (grows down)
    v v v v v v v v v
    ------------------
    
    (unmapped)
    
    ------------------ <--- Maximum stack size.
    
    
    (unmapped)
    
    
    -------------------
    mmap
    -------------------
    
    
    (unmapped)
    
    
    -------------------
    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
    brk (grows up)
    -------------------
    BSS
    -------------------
    Data
    -------------------
    Text
    -------------------
    
    ------------------- <--- Bottom or process address space.
    

    堆栈分配

    内核维护一个属于每个进程的页面列表,并将其与分页同步.

    The kernel maintains a list of pages that belong to each process, and synchronizes that with the paging.

    如果程序访问了不属于它的内存,内核会处理一个页面错误,并决定做什么:

    If the program accesses memory that does not belong to it, the kernel handles a page-fault, and decides what to do:

    • 如果超过最大堆栈大小,则将这些页面分配给进程
    • 否则,向进程发送 SIGSEGV,这通常会杀死它

    更多信息请访问:https://unix.stackexchange.com/questions/145557/how-does-stack-allocation-work-in-linux/239323#239323

    brkmmap

    brk and mmap

    这些系统调用允许进程显式地向内核请求内存块,而不是仅仅进入堆栈和段错误.

    Those system calls allow processes to explicitly request chunks of memory to the kernel instead of just going down the stack and segfaulting.

    这是 brk 的实际示例:brk( ) 系统调用有什么作用?

    这个答案解释了在可能的情况下使用堆栈的优势:x86汇编中寄存器上使用的push/pop指令的作用是什么?

    This answer explains the advantage of using the stack when that is possible: What is the function of the push / pop instructions used on registers in x86 assembly?

    物理记忆

    内核和用户空间内存之间没有明确的划分:Linux x86-64 上物理内存中的用户空间和内核之间是否存在显式拆分?

    There is no clear split between kernel and userspace memory: Is there an explict split between userspace and kernel in physical memory on Linux x86-64?

    这篇关于为 Linux 进程分配的堆栈内存在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆