1. 程式人生 > >進程,線程,協程,異步IO知識點

進程,線程,協程,異步IO知識點

variables down mas sock pipe unique soc read 但是

進程: qq 要以一個整體的形式暴露給操作系統管理,裏面包含對各種資源的調用,內存的管理,網絡接口的調用等。。。
對各種資源管理的集合 就可以成為 進程

線程: 是操作系統最小的調度單位, 是一串指令的集合


進程 要操作cpu , 必須要先創建一個線程 ,
all the threads in a process have the same view of the memory
所有在同一個進程裏的線程是共享同一塊內存空間的


A thread線程 is an execution執行 context上下文, which is all the information a CPU needs to execute a stream of instructions.

線程就是cpu執行時所需要的
Suppose you‘re reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.
假設
If you have a roommate, and she‘s using the same technique, she can take the book while you‘re not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.

Threads work in the same way. A CPU is giving you the 幻覺illusion that it‘s doing multiple多 computations運算 at the same time. It does that by spending花費 a bit點 of time on each computation運算. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.

On a more technical level, an execution context (therefore a thread) consists組合 of the values of the CPU‘s registers 寄存器.

Last: threads are different from 進程processes. A thread is a context of execution, while a process is a bunch一簇,一堆 of resources資源 associated相關的 with a computation. A process can have one or many threads.

Clarification: the resources associated with a process include memory pages (all the threads in a process have the same view of the memory), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).



什麽是進程(process)?
An executing instance of a program is called a process.

Each process provides the resources needed to execute a program.
A process has a virtual虛擬 address space, executable code,
open handles to system objects, a security context,
a unique唯一的 process進程標識符,pid identifier, environment variables,
a priority 優先級類class, minimum and maximum working set sizes,
and at least至少 one thread線程 of execution.
Each process is started with a single thread,
often called the primary主 thread, but can create additional額外的
threads from any of its threads.


進程與線程的區別?
Threads share the address space of the process that created it; processes have their own address space.
線程共享內存空間,進程的內存是獨立的
Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.

Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
同一個進程的線程之間可以直接交流,兩個進程想通信,必須通過一個中間代理來實現

New threads are easily created; new processes require duplication of the parent process.
創建新線程很簡單, 創建新進程需要對其父進程進行一次克隆

Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
一個線程可以控制和操作同一進程裏的其他線程,但是進程只能操作子進程

線程 內存共享
線程同時修改同一份數據時必須加鎖,mutex互斥鎖
遞歸鎖

def run(n):
print(‘run thread...‘)




for i in range(10):
t = threading.Thread(target=run, args=(n,))
t.setDaemon(True)
t.start()


print ‘master is done....‘

守護線程(slave) 服務與非守護線程(master)


進程 至少包含一個線程


queue
解耦,使程序直接實現松耦合,
提高處理效率 ,

FIFO = first in first out
LIFO = last in first out


io 操作不占用cpu

計算占用cpu , 1+1

python多線程 不適合cpu密集操作型的任務,適合io操作密集型的任務


multiprocess
Queue \ Pipe 只是實現進程間數據的傳遞
Manager 實現了進程間數據的共享,即多個進程可以修改同一份數據


IO 多路復用

進程,線程,協程,異步IO知識點