Go语言中的地址空间是指程序可以访问的内存范围。

huangapple go评论95阅读模式
英文:

What is the address space in Go(lang)?

问题

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

我试图从维基百科上理解以下主题,但没有成功:

然而,目前对我来说很难理解,因为我在内存管理和并发编程等领域的知识非常有限。有很多未知的词汇,比如段、页、相对/绝对地址、虚拟地址空间等。

有人可以向我解释一下这个问题的基础知识吗?也许有一些有用的文章,我找不到。

英文:

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.

答案1

得分: 20

Golang规范:

> “go”语句在同一地址空间内启动一个独立的并发线程或goroutine,作为函数调用的执行。

> 有人能解释一下这个问题的基本概念吗?

“地址空间”是一个通用术语,可以适用于许多上下文:

> 通过组合足够唯一标识的限定符来创建地址空间,以使地址在特定地址空间内不含糊。

Dave Cheney的演讲“让Go快速的五个要素”说明了在同一进程地址空间内使用goroutine所解决的主要问题:堆栈管理

Dave首先将“地址空间”限定为线程:

> 因为进程在执行过程中可以在任何点发生切换,操作系统需要存储所有这些寄存器的内容,因为它不知道哪些寄存器当前正在使用。

> 这导致了线程的发展,它们在概念上与进程相同,但共享相同的内存空间

(所以这是关于内存的)

然后,Dave说明了进程地址空间内的堆栈(进程管理的地址):

Go语言中的地址空间是指程序可以访问的内存范围。

> 传统上,在进程的地址空间内,

> - 堆位于内存底部,紧挨着程序(文本)并向上增长。

  • 栈位于虚拟地址空间的顶部,并向下增长。

还可以参考“栈和堆是什么,它们在哪里?”。

问题是:

> 由于堆和栈互相覆盖将是灾难性的,操作系统通常会安排在栈和堆之间放置一个不可写的内存区域,以确保如果它们发生碰撞,程序将中止。

对于线程,这可能会限制进程的堆大小:

Go语言中的地址空间是指程序可以访问的内存范围。

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

goroutine采用了一种不同的方法,同时仍然共享相同的进程地址空间:

> 那么这些goroutine的堆栈要求如何?

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

> 由于这个检查,goroutine的初始堆栈可以变得更小,这反过来使得Go程序员可以将goroutine视为廉价资源。

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

Go语言中的地址空间是指程序可以访问的内存范围。

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

> 将旧堆栈的内容复制到新堆栈,然后goroutine继续使用新的更大堆栈。

> 在第一次调用H之后,堆栈将足够大,以至于始终可以成功检查可用的堆栈空间。

英文:

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):

Go语言中的地址空间是指程序可以访问的内存范围。

> 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:

Go语言中的地址空间是指程序可以访问的内存范围。

> 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:

Go语言中的地址空间是指程序可以访问的内存范围。

> 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.

答案2

得分: 3

当你的应用程序在RAM上运行时,RAM中的地址由内存管理器分配给你的应用程序。这被称为地址空间。

概念:

处理器(CPU)通过取指-译码-执行的循环来执行指令。它通过将指令从磁盘获取到RAM中来执行应用程序。这样做是因为从磁盘获取指令非常低效。有人需要跟踪内存使用情况,因此操作系统实现了一个内存管理器。你的应用程序由一些程序组成,对于你来说,这些程序是用Go编程语言编写的。当你执行你的脚本时,操作系统以上述方式执行指令。

阅读你的帖子,我能够理解你的感受。随着你编写的程序越来越多,你提到的这些术语会变得越来越熟悉。

我最初从操作系统书籍中遇到了这些术语,也被称为恐龙书。

希望这对你有帮助。

英文:

When you application runs on the RAM, addresses in RAM are allocated to your application by the memory manager. This is refered to as address space.

Concept:

> the processor (CPU) executes instructions in a Fetch-Decode-Execute
> cycle. It executes instructions in an applicaiton by fetching it to
> the RAM (Random Acces Memory). This is done because it is very
> in-efficient to get it all the way from disk. Some-one needs to keep
> track of memory usage, so the operating system implements a memory
> manager. Your appication, consists of some program, in your case this
> is written in Go programming language. When you execute your script,
> the OS executes the instructions in the above mentioned fashion.

Reading your post i can empathize. The terms you mentioned will become familiar to you as program more and more.

I first encountered these terms from the operating systems book, a.k.a the dinosaur book.

Hope this helps you.

huangapple
  • 本文由 发表于 2014年12月28日 18:28:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/27675550.html
  • concurrency
  • go
  • memory
  • memory-management
  • parallel-processing
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定