A, processes,

Process is an abstraction of the operating system to the running program, is the basic unit of the operating system to allocate resources

Features:

  • Processes have independent stacks, stack resources are not shared between processes, and are scheduled by the operating system.
  • A process can contain multiple threads,
  • Memory is hard to share between different processes,
  • In the same process, data of threads can be shared
  • Process switching consumes more computer resources because there is more stack information and context to save
  • Processes use preemptive scheduling,

Process status:

  • The initial state
  • The ready state
  • Wait for (blocking) loading
  • Execution status
  • Termination status

Second, the thread

In modern operating systems, a process actually consists of several units of execution called threads, which are the basic unit of scheduling in the operating system

Thread features:

  • The line layer is the basic unit of operating system scheduling
  • Multiple threads can exist in a process,
  • Threads in the same process can share virtual memory
  • Because of shared memory, most of the time threads have synchronized mutex locks
  • There are two thread scheduling methodsTime-sharing scheduling (CPU rotation)andPreemptive scheduling (give priority to the threads with the highest priority using the CPU, and randomly select one with the same priority)

Thread state

Thread state is similar to process stateCopy the code
  • The initial state
  • The ready state
  • Wait for (blocking) loading
  • Execution status
  • Termination status

Third, coroutines

Coroutine (coroutine) is also called micro thread, a lightweight thread, also known as user state thread, coroutine is not managed by the operating system kernel, completely controlled by the program

Coroutines are not processes or threads. Their execution process is similar to subroutines. Coroutines are relatively independent and have their own context.Copy the code

Coroutine characteristics:

  • Coroutines can be viewed as ordinary objects, and if the current coroutine blocks, the current thread can execute other coroutines
  • Coroutines are transparent to the operating system, and the operating system cannot directly perceive and schedule coroutines, so threads are the smallest unit in which the operating system performs scheduling
  • The overhead of coroutine switching is much less than that of threads

Four, goroutine

Goroutine is the go language implementation of coroutines, equivalent to the functionality provided by other language libraries with built-in language keyword support.

Goroutine is essentially a coroutine that costs less to create (2 to 4KB)Copy the code

5. Process and thread comparison

  1. Process threads are scheduled by the operating systemCPU time-sharing systemThe impact of preemptive scheduling
  2. Switching: Process switching is involvedVirtual memory spaceSwitch as in processThread shared memory, so thread switching does not involve virtual memory address switching, so process switching time is longer than thread switching time

Sixth, thread, coroutine comparison

  1. Coroutines are lightweight threads in user mode, transparent to the operating system kernel, so the scheduling and switching of coroutines are controlled by the user.
  2. Switching: Coroutine switching is complete inThe user spaceTo proceed, thread switch involves privilege mode switch involvingThe kernel space
  3. Coroutine switching involves only basic CPU context switching, i.e. state information stored in registers. Coroutine switching is the transfer of the currentRegister stateSave the coroutine that will be switched inRegister state is loaded into the CPU register, this process is completely in user mode
  4. Thread scheduling is only for those with higher privilegesThe kernel spaceCan be done, so thread switching is involvedSwitching between user space and kernel space, more context than coroutines

Coroutine, goroutine

Goroutine is the Go language implementation of coroutinesCopy the code
  1. Go encapsulates goroutine scheduling in runtime, system call, etc., that is, Goroutine is not completely controlled by the user, but to some extent managed by go Runtime. The advantage is that when goroutine is blocked, CPU is released to other Goroutine

  2. By default, all goroutines run in the same native thread, using only one CPU core,