当前位置:网站首页>The difference between process, thread and coroutine

The difference between process, thread and coroutine

2020-11-08 08:08:27 Wang Zebin

In the interview , There are often questions from the interviewer “ process 、 The difference between threads and coroutines ” This problem , This is also the most basic knowledge in the university course operating system . We usually say a few words about it , But the details are not particularly clear . I've sorted out the relevant content , Add your own understanding , Share with you .

1. process

1.1 Definition

​ A process is a running activity of a program in a computer , It is the basic unit of resource allocation and scheduling in the operating system . Each process has its own address space , It usually includes code snippets 、 Data segment 、 Heap and stack . among , The code segment is used to store the code executed by the processor ; Data segments hold global and static variables ; The heap is used to store dynamically allocated memory ; Stacks are used to hold local variables 、 Function parameters and register values, etc .

1.2 Process switching

​ When multiple processes are running at the same time , In order to ensure that all processes have access to execution opportunities , We need to switch the process according to a certain algorithm . The so-called process switch is to recover the processor from the running process , Then the process to be run will occupy the processor . Reclaim the processor from a process , In essence, it is to store the intermediate data of registers in the process of running to the process stack . Let a process occupy the processor , In essence, it is to restore the register data stored in the stack to the processor's register , And send the breakpoint of the process to be run into the program counter of the processor .

​ The intermediate data that a process stores in the registers of the processor is called the process context , So process switch is the context switch between the aborted process and the process to be run . When the process switches context , You need to get in and out of the operating system kernel , And register data switching and other work , It takes a certain amount of time .

1.3 Process function
  • pid_t fork(void)

    function : Create a subprocess

  • int clone(int (*fn)(void ), voidchild_stack, int flags, void *arg)

    function : Copy a child process

  • pid_t getpid(void)

    function : Get your own process id

  • pid_t wait(int *status) or pid_t waitpid(pid_t pid, int *status, int options)

    function : Blocking parent process , Until the child process ends or receives the specified signal

2. Threads

2.1 Definition

​ A thread is an entity in a process , Is the basic unit of processor scheduling and dispatching , It's a smaller unit than a process that can run independently . Threads basically don't have independent system resources , It shares the resources owned by the process with other threads belonging to the same process , Threads have a small number of program counters on their own 、 Data registers and stacks are essential private resources in operation . therefore , The cost of the operating system scheduling thread is much less than that of the process , Using threads can effectively improve the concurrent efficiency of the system .

​ All in all Process is the basic unit of resource allocation , Thread is the basic unit of scheduling .

2.2 Shared resources

​ Threads share the resources of the process they belong to , Include :

  • Memory address space
  • Basic process information
  • Open file
  • signal processing
  • Current working directory
  • User and user group properties
2.3 Thread functions
  • pthread_create(tid,&attr,func,&arg) 

    function : Create a new thread .

  • pthread_join(tid,void **retval)

    function : Wait for thread number to be tid After the thread execution is finished, the thread resource is recycled , Similar to the process of wait() function , Blocking process .

  • pthread_exit(void *retval)

    function : End thread , And return the end code

  • pthread_cancel(tid)

    function : Cancel thread

  • pthread_self(void)

    function : Get thread id Number

3. coroutines

3.1 Definition

​ Coprogramming is essentially a kind of user ecothread , It doesn't require an operating system to schedule , Instead, it is managed and scheduled by the user program itself . It's hosted in the thread , Minimal system overhead , Can significantly improve performance and concurrency . The advantage of using coprocessor is high efficiency 、 Programming is simple 、 The structure is clear ; The disadvantage is the need for programming language support , If not , You need to implement the scheduler on your own . at present , There are not many native languages that support coroutines .

​ A coroutine has its own context , Co programs belonging to the same process share system resources owned by processes . The switch of coprocess is controlled by itself , Switch from the to other coroutines is controlled by the current coroutine . Compared with threads and processes , The biggest advantage of coprocessor is that it “ Lightweight ”, You can easily create millions of them without exhausting system resources .

3.2 go Language Association

   go Languages support coroutines at the language level ,go It's called goroutine.go All system call operations provided by the language standard library , The city will be sold Processors give other goroutine, This makes the coprocess switch management no longer dependent on the threads and processes of the system .

​ go Compare the performance of coroutines and threads :

  • Memory consumption

    goroutine:2KB

    Threads :8MB

  • Scheduling handoff overhead

    goroutine: Modify only PC/SP/DX Register value

    Threads : Mode switch mode switch between user mode and kernel mode )、 Refresh all registers

    By keyword go It starts a goroutine.

package main

import (	·
	"fmt"
	"time"
)

func echo(s string)  {
	fmt.Println(s)
}

func main() {
	//  Start two collaborations (goroutine)
	go echo("Hello world 1")
	go echo("Hello world 2")

	//  Blocking 3 second , Wait for the execution of the cooperation process 
	time.Sleep(3*time.Second)
}

The development tool used in this paper is :goland come from Swish Download

版权声明
本文为[Wang Zebin]所创,转载请带上原文链接,感谢