Process, thread, lightweight process, coroutine, and Goroutine in go

Source: Internet
Author: User

In the telephone interview, I was asked about the go coroutine, and Jun Wei once asked me about the coroutine. Although I have learned about coroutine in Eurasia and eventlet when using python, but I have a concept of coroutine, that is, lightweight threads. There is also a popular saying that threads should abide by rules, coroutine can see a red light but no car can still pass. Now, I will summarize all the information and explain from my personal understanding the goroutine in the process thread Lightweight Process coroutine go.

I. Process

Processes are the core concept in the operating system, and inter-process communication is the most important issue in distributed systems.

ProcessIt is an instance of program execution and acts as the entity for allocating system resources. A complete independent address space must be allocated for Process Creation.

Process SwitchingOnly occurs in the kernel state. Two steps: 1. Switch the global directory of the page to install a new address space. 2. Switch the kernel state stack and hardware context. Another statement is similar: 1. Save the CPU environment (register value, program counter, and stack pointer) 2. Modify register 3 of Memory Management Unit MMU. The address translation cache content in the buffer TLB is marked as invalid.

Ii. Thread

Definition in the book: A thread is an execution stream of a process that executes its own program code independently.

Wikipedia:Thread(English:Thread) Is the smallest unit that the operating system can schedule operations.

Generally, the thread context only contains the CPU context and its thread management information. The overhead of thread creation mainly depends on the overhead of memory allocated for the establishment of the thread stack, which is not large. Thread context switching occurs when two threads need to be synchronized, such as entering the shared data segment. Switch only the CPU register value needs to be stored, and then re-Load it to the CPU register with the value originally stored in the thread to be switched.

The main disadvantage of a user-level thread is that calling a system call that causes blocking immediately blocks the entire process of the thread. The kernel implementation thread will cause the thread context switching overhead to be as large as the process, so the compromise is the Lightweight Process (Lightweight ). In linux, a thread group is basically a set of lightweight processes that implement multi-threaded applications. I understand that there are user threads, lightweight processes, and kernel threads in the process.

Few lightweight processes are implemented at the language level. stackless python, erlang, and java are not supported.

Iii. coroutine

Definition of coroutine? Yan Kai and Xu Shiwei both said that coroutine is a lightweight thread, and a process can easily create coroutine of hundreds of thousands. After careful research, I personally feel that these are all true and false. From Wikipedia, we can see from Knuth's basic algorithm volume that "subprograms are actually exceptions of coroutine ". What is a subroutine?Subroutine(Subroutine, procedure, function, routine, method, subprogram) is a function! So coroutine is nothing remarkable, that is, a more general program component. Then, if your memory space is large enough, isn't it up to you to create many functions?

You can use yield to call other coroutines. The coroutine for transferring the execution right through yield is not the relationship between the caller and the called, but symmetric and equal to each other. The starting point of the coroutine is the first entry point. In the coroutine, the return point is the next entry point. The life cycle of the child routine follows the following steps: (the last called child routine returns the result first); on the contrary, the life cycle of the coroutine is completely determined by the needs of their use.

Differences between threads and threads:

Once a thread is created, you cannot decide when to obtain the time slice, and when to give up the time slice. You have handed it to the kernel. The coroutine compiler can have a controllable switching time and a low switching cost. From the perspective of whether the operating system has the scheduling permission, coroutine is used because kernel state switching is not required. I think the definition of Lai Yonghao and dccmx is relatively accurate-a lightweight thread in the user State. Http://blog.dccmx.com/2011/04/coroutine-concept)

Why coroutine:

Coroutine helps achieve:

  • State Machine: implements a state machine in a subroutine. The State is determined by the exit/entry point of the process. This produces code with higher readability.
  • Role Model: A parallel role model, such as a computer game. Each role has its own process (this logically separates the Code ), however, they voluntarily hand over control to the central scheduler that executes the process of each role in sequence (a form of cooperative multitasking ).
  • Generator: It facilitates input/output and general traversal of data structures.

 

Yan Kai summarized the common languages and platforms that support coroutine, which can be used as a reference, but should be investigated in depth.

 

 

4. Goroutine in go

Goroutine in go is generally considered to be the go language implementation of coroutine. In Go language programming, goroutine is a lightweight thread (that is, coroutine, 90 pages in the original book ). in chapter 9 advanced topics, the author once again mentioned that "Basically speaking, goroutine is a coroutine of the go language version" (204 pp. Of the original book ). but the author Rob Pike does not.

"A Goroutine is a Go function or method that runs concurrently in the same address space as other goroutines. A running program consists of one or more goroutines.It is different from threads, coroutines, processes, and so on. It is a goroutine ."

In stack implementation, gccgo under its compiler branch is the thread pthread, 6g is a multiplexing threads (6g/8g/5g represents 64-bit, 32-bit, and Arm architecture compiler respectively)

An article on infoQ also said: goroutine is a function of the Go Language Runtime Library, not provided by the operating system, but not implemented by a thread. For details, see pkg/runtime/proc. c In the Go language source code.

Lao Zhao believes that goroutine puts the class library function into the language.

Goroutine concurrency problems: goroutine runs in the shared memory, the communication network may be deadlocked, and the debugging of multithreading problems may be poor. A good suggestion rule: do not share the memory through shared memory. On the contrary, share the memory through communication.

Difference in concurrency:

Parallelism refers to the running state of the program. Two threads are being executed to be considered Parallelism; Concurrency refers to the logic structure of the program, and Concurrency means that as long as two or more threads are still being executed. To put it simply, Parallelism can only be achieved in multi-core or multi-processor scenarios, while Concurrency does not. Http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference)

 

References:

Modern Operating System, distributed system principle and model, deep understanding of Linux kernel, go Programming Language

Lai Yong Hao coroutine three article only one http://blog.csdn.net/lanphaday/article/details/5397038

Yan Kai http://qing.blog.sina.com.cn/tj/88ca09aa33002ele.html

Go programming language (Chinese http://tonybai.com/2012/08/28/the-go-programming-language-tutorial-part3)

Go programming language http://go.googlecode.com/hg-history/release-branch.r60/doc/GoCourseDay3.pdf

Go language initial experience http://blog.dccmx.com/2011/01/go-taste/

Https://zh.wikipedia.org/wiki/Go

Https://zh.wikipedia.org/wiki/process

Https://zh.wikipedia.org/wiki/thread

Http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference

Http://www.infoq.com/cn/articles/knowledge-behind-goroutine

Go programming book reviews: http://book.douban.com/review/5726587/

Why do I think goroutine and channel are built-in functions of class libraries on other platforms in languages?

Http://blog.zhaojie.me/2013/04/why-channel-and-goroutine-in-golang-are-buildin-libraries-for-other-platforms.html

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.