当前位置:网站首页>Basic concepts of program, process, thread, coprocess, single thread and multi thread
Basic concepts of program, process, thread, coprocess, single thread and multi thread
2022-07-27 06:55:00 【THER1881】
One 、 Program
A program is a set of operable working steps designed to achieve a specific goal , For computers , A program is a set of ordered instructions that the system can recognize . The program can direct the computer to perform the actions we want it to do . The program is stored on disk , During execution, from disk to memory to register , Finally being CPU perform . A program is a language that communicates with a computer , Composed of specific syntax and keywords , From the entry point , The principle is from top to bottom , From left to right , Line by line .
Two 、 Processes and threads
1、 Definition
A process is a program with some independent functions. It is a running activity on a data set , A process is an independent unit of the system for resource allocation and scheduling .
A thread is an entity of a process , yes CPU Basic unit of dispatch and dispatch , It's a smaller, independent, basic unit than a process . The thread itself basically does not have system resources , Have only a few essential resources in operation ( Such as program counter , A set of registers and stacks ), But it can share all the resources owned by a process with other threads belonging to the same process .
2、 Relationship between them
One thread can create and undo another ; Multiple threads in the same process can execute concurrently .
Relative to the process , Thread is a concept closer to the body of execution , It can share data with other threads in the same process , But have your own stack space , Have a separate execution sequence .
3、 Difference between them
The main difference between processes and threads is that they are different ways of operating system resource management . The process has a separate address space , After a process crashes , No impact on other processes in protected mode , Threads are just different execution paths in a process . Threads have their own stack and local variables , But there is no separate address space between threads , If one thread dies, the whole process will die , So a multiprocess program is more robust than a multithreaded program , But when the process switches , High cost of resources , Less efficient . But for some concurrent operations that require simultaneous and shared variables , Only threads can be used , Can't use process .
3.1、 In short , A program has at least one process , A process has at least one thread .
3.2、 Thread partition scale is smaller than process , Make multithreaded programs have high concurrency .
3.3、 in addition , Processes have independent memory units during execution , Multiple threads share memory , Thus greatly improving the efficiency of the program .
3.4、 Threads are different from processes in execution . Each independent thread has an entry for the program to run 、 Exit of sequential execution sequence and program . But threads can't execute independently , Must exist in the application , Multiple thread execution control provided by the application .
3.5、 From a logical point of view , The meaning of multithreading is in an application , There are multiple execution parts that can be executed at the same time . But the operating system does not regard multiple threads as multiple independent applications , To achieve process scheduling and management and resource allocation . This is the important difference between processes and threads .
3、 ... and 、 coroutines
1、 Definition
Coroutine is a lightweight thread in user mode , The scheduling of the process is completely controlled by the user . A coroutine can have , The process is not managed by the operating system kernel , And it's completely controlled by the program . Instead of letting the operating system schedule , Why don't I do it myself , This is Xiecheng .
2、 Use scenarios of coroutine
Multiple coprocesses in a thread are executed serially , Can't use multi-core , therefore , obviously , Co process is not suitable for computing intensive scenarios . Coprocessor fit I/O Blocking type .
I/O Itself is blocking ( Compare with CPU For the world of time ). For now , No matter what I/O How fast , It's no match for CPU The speed of , So a I/O Related procedures , When it's going on I/O In operation ,CPU It's actually free .
Let's assume this scenario , Here's the picture :1 Threads have 5 individual I/O Things about ( Subroutines ) To deal with . If we absolutely serialize , So... Be one of them I/O Blocking time , other 4 individual I/O Can't be implemented , Because the program is absolutely serial ,5 individual I/O You have to queue up one by one for processing , When one I/O Blocking time , Other 4 One has to wait .
Four 、 Single thread and multi thread
1、 Single thread definition :
Single thread : A process has only one thread . Single thread in program execution , The program paths are arranged in a sequential order , The front has to be dealt with , Later will be implemented .
2、 Multithreading definition : Multithreading refers to the program contains multiple execution flows , That is, you can run multiple different threads to perform different tasks in one program at the same time , That is to say, a single program is allowed to create multiple threads executing in parallel to complete their own tasks .
3、 Advantages and disadvantages of single thread :
(1)、 Advantages of single threading :
For single threads and many threads , System stability 、 It's very scalable 、 Rich software . More for point-to-point services .
(2) Single thread disadvantage :
(1)、 When there are multiple processes , Now CPU No resources will be allocated to other processes , The processing time will be longer and slower .
(2)、 When the program is running, the window cannot be dragged , You must wait until the program is finished .
4、 Advantages and disadvantages of multithreading :
advantage :
Can improve CPU Utilization ratio . In multithreaded programs , When a thread has to wait ,CPU You can run other threads instead of waiting , This greatly improves the efficiency of the program .
shortcoming :
(1)、 Threads are also programs , So threads need to take up memory , More threads occupy more memory ;
(2)、 Multithreading needs coordination and management , So we need to CPU Time tracking thread ;
(3)、 Access to shared resources between threads will affect each other , We must solve the problem of competing for shared resources ;
(4)、 Too many threads lead to too complex control , In the end, it may cause a lot of problems Bug
边栏推荐
猜你喜欢

Add virtual network card and configure OP route in win10

MySql数据库

regular expression

For redis under windows, it can only read but not write

最新!国资委发布国有企业数字化转型新举措

一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告

FTX US launched FTX stocks, striding forward to the mainstream financial industry

What is the reason why dragging the timeline is invalid when playing device videos on the easycvr platform?

Fix the problem that the paging data is not displayed when searching the easycvr device management list page

Raid explanation and configuration
随机推荐
GoLand writes Go program
deepsort源码解读(一)
MangoDB
如何让最小 API 绑定查询字符串中的数组
gin-vue-admin 使用docker容器中的数据库
NFS introduction and configuration
Deepsort工作原理分析
ES6 new features (getting started)
Customer cases | focus on process experience to help bank enterprise app iteration
When a subclass calls the constructor of its parent class
Go语言学习
For redis under windows, it can only read but not write
Fix the problem that the paging data is not displayed when searching the easycvr device management list page
采用QT进行OpenGL开发(一)绘制平面图形
Add virtual network card and configure OP route in win10
FTX Foundation funded 15million to help covid-19 clinical trials, which will affect global public health
如何删除或替换EasyPlayer流媒体播放器的loading样式?
Numpy array and image conversion
Raid explanation and configuration
一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告诉你一键修复漏洞可行吗?向日葵来告