Analysis of Golang Runtime

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

From the Goroot code, there are a lot of code very complex, a little bit to see it. The most important concept is that Runtime,golang programs are run on a runtime basis (except for syscall that interact directly with the underlying).

Runtime

There are three files in $goroot/pkg/runtime/that are very important:

Proc.c

Stack.h

Runtime.h

You can see a lot of data structures and interfaces in runtime.h

The data structure here is the underlying implementation of the various specific structures in go, such as slice:

structslice{//must not move anythingbyte*array;//actual datauint32len;//number of elementsuint32cap;//allocated Numbe R of elements};

There are two important structures as well:

G

G represents the Goroutine. Opening a goroutine is actually instantiating a G

M

M stands for machine. The data structure in M that holds the Go program and machine CPU interaction

For example, a dual-core CPU, open 4 Goroutine outside the main routine, then there are actually 2 m structures, 6 g structures (one is the main routine,4 open routine, the last is idle routine)

Runtime and the C standard library play the same role. is for the cross-platform of language. Runtime can run on Windows and UNIX platforms and can run on Intel or ARM processors.

A go program comes with a runtime,runtime responsible for interacting with the underlying operating system.

This article gives a clear concept of runtime: Http://pastebin.com/LEsB8FVW

Start process

Back to $GOROOT/PKG/RUNTIME/PROC.C

Inside this comment:

The bootstrap sequence is:

//

Call Osinit

Call Schedinit

Make & Queue New G

Call Runtime Mstart

//

The new G calls runtime main.

Tell us explicitly that the GO program start-up process is:

1 call Osinit, OS-level initialization

2 Calling Runtime Schedinit

A lot of pre-operations are done within this function

Get program Run parameters

Get Program Environment variables

(mainly there is an environment variable gomaxprocs, you can use runtime.) Gomaxprocs (int) or set the environment variable directly $gomaxprocs change the number of CPUs used by the program)

3 Call runtime Mstart start M

4 Calling Runtime Main

There are two lines in Runtime.main:

Main Init (); Call the INIT function in the main package

Main main (); Call the main function in the main package

See the call stack with GDB debug See trace

About the startup process recommend this article: http://www.cnblogs.com/genius0101/archive/2012/04/16/2447147.html

Call C program in Go

There are two ways to call C programs in go:

1 Go program uses import "C"

2 working with files. GOC (formerly known as CGO)

The first of these methods

Example:

Package main/* #include 
 
  
   
  */import "C" import "FMT" Func Main () {    fmt. PRINTLN (int (c.random ()))}func Seed (i int) {    c.srandom (C.uint (i))}
 
  

Run

Very simple to use, after the import "C" There is a global variable capital C contains the C library functions, including the C library as a comment on the import "C" above

For more information, refer to:

Http://golang.org/doc/articles/c_go_cgo.html

http://golang.org/cmd/cgo/

The second method of

Create GOC files directly, GOC files are files written in C and go mix

Reference $GOROOT/SRC/PKG/RUNTIME/SYSCALL_WINDOWS.GOC

The standard library of include C is not allowed in this manner, and only the custom header file can be referenced. This method is seldom used, basically just need to know a bit better.

Reference articles

I wrote an article that listed all the Golang's underlying related articles.

Http://www.cnblogs.com/yjf512/archive/2012/07/17/2595689.html

If you have more good information, please post it to the next ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.