Go(lang)中的地址空间是什么? [英] What is the address space in Go(lang)?

查看:286
本文介绍了Go(lang)中的地址空间是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了解Go中并发编程的基础知识。几乎所有文章都使用术语地址空间,例如:所有goroutines共享相同的地址空间。这是什么意思?



我试图从维基中了解以下主题,但并不成功:






  • 问题:


    因为堆和堆栈相互覆盖会是灾难性的,系统通常安排在堆栈和堆之间放置一个不可写存储器的区域,以确保如果它们发生冲突,程序将中止。


    使用线程,这可能会导致限制进程的堆大小:




    随着程序中的线程数量的增加,可用的地址空间量减少。


    goroutine使用不同的方法,但仍然共享相同的进程地址空间:


    goroutines?



    Go编译器不是使用保护页,而是在每个函数调用中插入一个检查,以检查函数是否有足够的堆栈来运行。如果没有,运行时可以分配更多的堆栈空间。



    由于这个检查,goroutines初始堆栈可以做得更小,这反过来允许Go程序员


    Go 1.3引入了一种管理这些堆栈的新方法:




    而不是添加和删除额外的堆栈段,如果goroutine的堆栈太小,新的,更大的,堆栈将被分配。



    旧堆栈的内容被复制到新堆栈,然后goroutine继续它的新的更大堆栈。



    在第一次调用H之后,堆栈将足够大,以便检查可用的堆栈空间总是成功。



    I try to understand the basics of concurrent programming in Go. Almost all articles use the term "address space", for example: "All goroutines share the same address space". What does it mean?

    I've tried to understand the following topics from wiki, but it wasn't successful:

    However at the moment it's difficult to understand for me, because my knowledges in areas like memory management and concurrent programming are really poor. There are many unknown words like segments, pages, relative/absolute addresses, VAS etc.

    Could anybody explain to me the basics of the problem? May be there are some useful articles, that I can't find.

    解决方案

    Golang spec:

    A "go" statement starts the execution of a function call as an independent concurrent thread of control, or goroutine, within the same address space.

    Could anybody explain to me the basics of the problem?

    "Address space" is a generic term which can apply to many contexts:

    Address spaces are created by combining enough uniquely identified qualifiers to make an address unambiguous (within a particular address space)

    Dave Cheney's presentation "Five things that make Go fast" illustrates the main issue addressed by having goroutine within the same process address space: stack management.

    Dave's qualifies the "address space", speaking first of thread:

    Because a process switch can occur at any point in a process’ execution, the operating system needs to store the contents of all of these registers because it does not know which are currently in use.

    This lead to the development of threads, which are conceptually the same as processes, but share the same memory space.

    (so this is about memory)

    Then Dave illustrates the stack within a process address space (the addresses managed by a process):

    Traditionally inside the address space of a process,

    • the heap is at the bottom of memory, just above the program (text) and grows upwards.
    • The stack is located at the top of the virtual address space, and grows downwards.

    See also "What and where are the stack and heap?".

    The issue:

    Because the heap and stack overwriting each other would be catastrophic, the operating system usually arranges to place an area of unwritable memory between the stack and the heap to ensure that if they did collide, the program will abort.

    With threads, that can lead to restrict the heap size of a process:

    as the number of threads in your program increases, the amount of available address space is reduced.

    goroutine uses a different approach, while still sharing the same process address space:

    what about the stack requirements of those goroutines ?

    Instead of using guard pages, the Go compiler inserts a check as part of every function call to check if there is sufficient stack for the function to run. If there is not, the runtime can allocate more stack space.

    Because of this check, a goroutines initial stack can be made much smaller, which in turn permits Go programmers to treat goroutines as cheap resources.

    Go 1.3 introduces a new way of managing those stacks:

    Instead of adding and removing additional stack segments, if the stack of a goroutine is too small, a new, larger, stack will be allocated.

    The old stack’s contents are copied to the new stack, then the goroutine continues with its new larger stack.

    After the first call to H the stack will be large enough that the check for available stack space will always succeed.

    这篇关于Go(lang)中的地址空间是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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