Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the address space in Go(lang)?

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:

  • http://en.wikipedia.org/wiki/Virtual_memory
  • http://en.wikipedia.org/wiki/Memory_segmentation
  • http://en.wikipedia.org/wiki/Page_(computer_memory)
  • ...

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.

like image 518
Timur Fayzrakhmanov Avatar asked Dec 28 '14 10:12

Timur Fayzrakhmanov


People also ask

Is Go called Golang?

Go (also called Golang or Go language) is an open source programming language used for general purpose. Go was developed by Google engineers to create dependable and efficient software. Most similarly modeled after C, Go is statically typed and explicit.

What are channels in Golang?

In Golang, or Go, channels are a means through which different goroutines communicate. Think of them as pipes through which you can connect with different concurrent goroutines. The communication is bidirectional by default, meaning that you can send and receive values from the same channel.

Is there a Go runtime?

The Go runtime in the flexible environment is the software stack responsible for building and running your code. To choose the Go runtime in the flexible environment, add two lines to your app. yaml file.

Why does Go have a runtime?

Go does have an extensive library, called the runtime, that is part of every Go program. The runtime library implements garbage collection, concurrency, stack management, and other critical features of the Go language. Although it is more central to the language, Go's runtime is analogous to libc , the C library.


2 Answers

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

http://dave.cheney.net/wp-content/uploads/2014/06/Gocon-2014-39.jpg

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:

http://dave.cheney.net/wp-content/uploads/2014/06/Gocon-2014-41.jpg

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:

http://dave.cheney.net/wp-content/uploads/2014/06/Gocon-2014-45.jpg

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.

like image 179
VonC Avatar answered Oct 05 '22 00:10

VonC


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.

like image 41
Ganesh Kamath - 'Code Frenzy' Avatar answered Oct 04 '22 23:10

Ganesh Kamath - 'Code Frenzy'