Python-線程和進程之初解

第一:進程

1.官方解釋:

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 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.

2.從程序角度理解進程

程序(也就是上文說的execute a program),不能單獨運行,必須將程序裝載到內存中(a virtual address space),系統爲之分配資源才能運行,這樣執行的的程序就稱爲進程,所以進程是程序的一次執行活動,故進程是對各種資源的集合

在多道編程中,我們允許多個程序同時加載到內存中,在操作系統的調度下,可以實現併發地執行(以進程爲單位,一個時間進程只能做一件事)。這是這樣的設計,大大提高了CPU的利用率。進程的出現讓每個用戶感覺到自己獨享CPU

第二:線程

1.官方解釋:

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(CPU寄存器).

Last: threads are different from processes. A thread is a context of execution(一個執行上下文-CPU寄存器的值組合), 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).

2.從進程的角度理解線程:

    (1).線程是操作系統能夠進行運算調度的最小單位,被包含在進程中,是進程中實際運作單位(可以理解爲進程中實際活動的人),一個進程中可以併發多個線程,每個線程可以執行不同的任務

    (2).進程與線程區別

  • 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.

        同進程中的線程共享所屬進程的志願,進程是直接copy父進程的數據段,也就是自己造一個一樣的

  • 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.

        同進程中的線程可以相互控制,而進程只能對子進程控制

  • Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.

第三:看圖理解進程與線程

    (1)

    (2)

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章