Goroutine in processes, threads, lightweight processes, co-routines, and go

Source: Internet
Author: User

Processes, threads, lightweight processes, goroutine, and go in the phone interview was asked to go to the association, once the military Wei also asked me to the association process. Although in Python time in Eurasia and Eventlet understand the association, but their concept of the association is a lightweight thread, there is a very popular traffic lights saying: thread to keep the rules, the co-process see red light but no car can still pass. Now summarize each of the data, from the personal understanding of the process thread lightweight process goroutine go of those things. First, the core concept of process operating system is the process, the most important problem in Distributed system is interprocess communication. A process is "an instance of program execution" that acts as an entity that allocates system resources. Process creation must be assigned a complete, independent address space. Process switching occurs only in the kernel state, two steps: 1 switch the page global catalog to install a new address space 2 toggles the kernel-state stack and the hardware context. Another way to say something like: 1 Save the CPU environment (register value, program counter, stack pointer) 2 Modify memory management Unit MMU's Register 3 conversion fallback buffer the address translation cache content in the TLB is marked as invalid. The definition in the thread book: A thread is an execution stream of a process that executes its own program code independently. Wikipedia: Threading (English: Thread) is the smallest unit that the operating system can perform operations on. The thread context typically contains only the CPU context and other thread management information. The overhead of thread creation depends primarily on the overhead of allocating memory for the thread stack to be built, which is not very expensive. A thread context switch occurs when two threads need to be synchronized, such as entering a shared data segment. Switching only the CPU register value needs to be stored and then reloaded into the CPU register with the previously stored value of the thread that will be switched to. The main disadvantage of a user-level thread is that the call to a system call that causes blocking immediately blocks the entire process that the thread belongs to. A kernel implementation thread can cause thread context switching to be as expensive as a process, so the tradeoff is a lightweight process (lightweight). In Linux, a thread group is basically a lightweight set of processes that implement multithreaded applications. I understand that there are user threads, lightweight processes, kernel threads in the process. The language level of lightweight processes is less, stackless Python,erlang support, Java is not supported. Third, the definition of Cheng? Outskirts, Xu Xiwei are only said to be a lightweight thread, a process can easily create a hundreds of thousands of-meter association. Careful study, the personal feeling that these are the myth of people. From Wikipedia, the "subroutine is actually a special case of the association" from Knuth's basic algorithm volume. What is a subroutine? Subroutines (English: subroutine, procedure, function, routIne, method, subprogram) is a function! So the process is not great, is a more general meaning of the program components, then you have large memory space, how many functions to create it is not with you? The co-process can invoke other processes through yield. The process of transferring execution through yield is not the relationship between the caller and the callee, but the symmetry and equality between them. The start of the process is the first entry point, and in the process, the return point is followed by the entry point. The lifetime of the subroutine follows a LIFO (the last called subroutine returns first); instead, the lifetime of the process is determined entirely by the needs of their use. The difference between a thread and a co-worker: Once you've created a thread, you can't decide when he gets the time slice, when to make the time slice, and you give it to the kernel. And the co-process writer can have a controllable switching time, and the second is a small switching cost. From the operating system has no scheduling rights, the process is because it does not need to switch the kernel state, so will use it, there will be such a thing. Rai Yonghao and dccmx This definition I feel relatively accurate in the process-user-state of the lightweight thread. (http://blog.dccmx.com/2011/04/coroutine-concept/) Why to use the co-process: The process helps to achieve: State machine: In a subroutine to implement the state machine, where the state is determined by the current exit/entry point of the procedure This can result in more readable code. Role model: A parallel role model, such as a computer game. Each role has its own process (which logically separates the code), but they voluntarily hand over control to the central scheduler that performs each role process sequentially (this is a form of cooperative multitasking). Generator: It facilitates input/output and general traversal of data structures. Outskirts summarizes the support of the common language and platform, can do reference, but should be in-depth investigation under the good. Go language concurrency of the United States four, go in the Goroutinego in the Goroutine, is generally considered to be the association of the Go Language implementation. "Go language Programming" says Goroutine is a lightweight thread (that is, the Coroutine, the original book 90 pages). In the Nineth chapter of the Advanced topic, the author once again mentions, "fundamentally speaking, Goroutine is a go language version of the Association (coroutine)" (Original book 204 pages). But Rob Pike, the author, does not say so. "A goroutine is a go function or method that runs concurrently with other goroutines in the same address space. A running program consists of one or more goroutine. It differs from thread, association, process, and so on. It is a goroutine. "On the implementation of the stack, its implementation under the compiler branch is GCCGO the thread pthread,6g is multiplexedThreads (6g/8g/5g for 64-bit, 32-bit, and arm architecture compilers) Infoq an article about the feature also said: Goroutine is the function of the go language runtime, not the functionality provided by the operating system, Goroutine is not implemented with threads. See the PKG/RUNTIME/PROC.C in the Go language source of the old Zhao think Goroutine is to put the class library function into the language. Concurrency problems with Goroutine: Goroutine runs in shared memory, communication networks can deadlock, multithreaded problems are poorly debugged, and so on. A good recommendation rule: Do not communicate through shared memory, instead, share memory through communication. Parallel concurrency differences: Parallel refers to the running state of the program, to have two threads executing in order to be considered parallelism, concurrency refers to the logical structure of the program, concurrency as long as more than two threads are still in the process of execution. Simply put, parallelism is required to be done in multicore or multiprocessor cases, while concurrency does not. (http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-difference) Reference: "Modern operating System" Principles and paradigms of distributed systems "in-depth understanding of the Linux kernel" Go programming language Rai Yonghao three only one http://blog.csdn.net/lanphaday/article/details/5397038 Yan Kai Qing.blog.sina.com.cn/tj/88ca09aa33002ele.htmlgo programming language Chinese http://tonybai.com/2012/08/28/ the-go-programming-language-tutorial-part3/(a concurrency is missing from the Chinese translation definition) Go programming language English http://go.googlecode.com/hg-history/ Release-branch.r60/doc/gocourseday3.pdfgo Language First Experience http://blog.dccmx.com/2011/01/go-taste/https://zh.wikipedia.org/ wiki/gohttps://zh.wikipedia.org/wiki/Process https://zh.wikipedia.org/wiki/Thread Http://stackoverflow.com/questions/1050222/concurrency-vs-parallelism-what-is-the-differencehttp://www.infoq.com/cn/ Articles/knowledge-behind-goroutinego Language Programming book review: http://book.douban.com/review/5726587/ Why do I think that goroutine and channel are built into the language of the functions of the class library on other platforms http://blog.zhaojie.me/2013/04/ Why-channel-and-goroutine-in-golang-are-buildin-libraries-for-other-platforms.html

Goroutine in processes, threads, lightweight processes, co-routines, and go

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.