1. 程式人生 > >線程與進程

線程與進程

top needed info 線程與進程 directly 空間 其他 裝載 orm

什麽是線程?

程序並不能單獨運行,只有將程序裝載到內存中,系統為它分配資源才能運行,而這種執行的程序就稱之為進程。程序和進程的區別就在於:程序是指令的集合,它是進程運行的靜態描述文本;進程是程序的一次執行活動,屬於動態概念。

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.

每個進程的啟動都是從一個線程開始的,這個線程被稱為主線程。進程可以從一個線程中創建多個線程。

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.

一個進程有多個線程

進程的優點:

  • 多道編程,允許多個程序同時加載到內存,實現多進程的並發執行,用戶感覺自己獨享CPU

進程的缺點:

  • 在進程內部,只能在一個時間幹一件事。

  • 進程在執行的過程中如果阻塞,例如等待輸入,整個進程就會掛起,即使進程中有些工作不依賴於輸入的數據,也將無法執行。

什麽是線程?

線程是操作系統能夠進行運算調度的最小單位。它被包含在進程之中,是進程中的實際運作單位。一條線程指的是進程中一個單一順序的控制流,一個進程中可以並發多個線程,每條線程並行執行不同的任務

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.

線程與進程的區別:

  1. Threads share the address space of the process that created it; processes have their own address space.

  同一個進程創建的多個進程,共享著這個進程的地址空間。而每個進程,有自己獨立的地址空間

  2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.

線程可以直接訪問其進程中的其他數據區。進程有其父進程數據區的專屬拷貝。

  3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.

線程能直接與其進程下的其他線程通信。進程必須使用中間進程來與它的兄弟進程通信。

  4. New threads are easily created; new processes require duplication of the parent process.

  可以很容易地創建一個新的線程。而創建新的進程,需要其父進程的完整拷貝

  5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.

  線程可以控制其所在進程的其他線程。進程只能控制其子進程。

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

  對主線程的修改可能影響其所在進程的其他線程的行為。對父進程的修改不能影響子進程

線程與進程