網絡編程基礎【day10】:進程與線程介紹(一 )

本節內容

一、概述編程

二、什麼是進程?安全

三、什麼是線程?網絡

四、什麼是攜程?併發

五、存在的疑問socket

六、總結ide

1、概述

  咱們知道,全部的指令的操做都是有CPU來負責的,cpu是來負責運算的。OS(操做系統) 調度cpu的最小單位就是線程。程序啓動後,從內存中分一塊空間,把數據臨時存在內存中,由於內存比較快,內存比磁盤快,而CPU又比內存還要快不少。進程以前的的內存是不能訪問的,默認是要隔離的。每個程序的內存是獨立的,互相是不能訪問的。ui

  進程:是以一個總體的形式暴露給操做系統管理,裏面包含對各類資源的調用,內存的管理,網絡接口的調用等等。。。。對各類資源管理的集合,就能夠成爲進程。spa

  線程:是操做系統的最小的調度單位,是遺傳指令的集合。操作系統

2、什麼是進程(Process)?

2.一、英文解釋

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,

每一個進程提供提供此程序的所須要的資源。一個進程有一個虛擬地址空間,執行代碼,操做系統的系統接口,一個安全的上線文,一個惟一的進程標識符(PID),

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.二、進程概念

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

在多道編程中,咱們容許多個程序同時加載到內存中,在操做系統的調度下,能夠實現併發地執行。這是這樣的設計,大大提升了CPU的利用率。進程的出現讓每一個用戶感受到本身獨享CPU,所以,進程就是爲了在CPU上實現多道編程而提出的。

2.三、有了進程爲何還要線程?

進程有不少優勢,它提供了多道編程,讓咱們感受咱們每一個人都擁有本身的CPU和其餘資源,能夠提升計算機的利用率。不少人就不理解了,既然進程這麼優秀,爲何還要線程呢?其實,仔細觀察就會發現進程仍是有不少缺陷的,主要體如今兩點上:

  • 進程只能在一個時間幹一件事,若是想同時幹兩件事或多件事,進程就無能爲力了。

  • 進程在執行的過程當中若是阻塞,例如等待輸入,整個進程就會掛起,即便進程中有些工做不依賴於輸入的數據,也將沒法執行。

例如,咱們在使用qq聊天, qq作爲一個獨立進程若是同一時間只能幹一件事,那他如何實如今同一時刻 即能監聽鍵盤輸入、又能監聽其它人給你發的消息、同時還能把別人發的消息顯示在屏幕上呢?你會說,操做系統不是有分時麼?但個人親,分時是指在不一樣進程間的分時呀, 即操做系統處理一會你的qq任務,又切換到word文檔任務上了,每一個cpu時間片分給你的qq程序時,你的qq仍是隻能同時幹一件事呀。

再直白一點, 一個操做系統就像是一個工廠,工廠裏面有不少個生產車間,不一樣的車間生產不一樣的產品,每一個車間就至關於一個進程,且你的工廠又窮,供電不足,同一時間只能給一個車間供電,爲了能讓全部車間都能同時生產,你的工廠的電工只能給不一樣的車間分時供電,可是輪到你的qq車間時,發現只有一個幹活的工人,結果生產效率極低,爲了解決這個問題,應該怎麼辦呢?。。。。沒錯,你確定想到了,就是多加幾個工人,讓幾我的工人並行工做,這每一個工人,就是線程!

3、什麼是線程(Thead) ?

3.一、線程概念

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

3.二、英文解釋

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

4、進程與線程的區別?

4.一、英文解釋

  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.

4.二、中文解釋

  1. 線程是共享內存空間的;進程的內存是獨立的。
  2. 線程能夠直接訪問此進程中的數據部分;進程有他們獨立拷貝本身父進程的數據部分,每一個進程是獨立的
  3. 同一進程的線程之間直接交流(直接交流涉及到數據共享,信息傳遞);兩個進程想通訊,必須經過一箇中間代理來實現。
  4. 建立一個新的線程很容易;建立新的進程須要對其父進程進行一次克隆。
  5. 一個線程能夠控制和操做同一進程裏的其餘線程;可是進程只能操做子進程。
  6. 對主線程的修改,可能會影響到進程中其餘線程的修改;對於一個父進程的修改不會影響其餘子進程(只要不刪除父進程便可)

5、存在的疑問

5.一、進程和線程那個運行快?

注意了,它倆是沒有可比性的,線程是寄生在進程中的,你問它倆誰快。說白了,就是問在問兩個線程誰快。由於進程只是資源的集合,進程也是要起一個線程的,它倆沒有可比性。

5.二、進程和線程那個啓動快?

答案是:線程快。由於進程至關於在修一個屋子。線程只是一下把一個來過來就好了。進程是一堆資源的集合。它要去內存裏面申請空間,它要各類各樣的東西去跟OS去申請。可是啓動起來一運行,它倆是同樣的,由於進程也是經過線程來運行的。

6、總結

  1. 線程是操做系統最小的調度單位,是一串指令的集合。
  2. 進程要操做CPU,必須先建立一個線程。
  3. 進程自己是不能夠執行的,操做cpu是經過線程實現的,由於它是一堆執行,而進程是不具有執行概念的。就像一個屋子,屋子就是進程,可是屋子裏面的每個人就是線程,屋子就是內存空間。
  4. 單核CPU只能同時幹一件事,可是爲何給咱們的感受是在幹了不少件事?由於上線的切換,剛纔也說了跟讀書那個例子同樣。由於CPU太快了,能夠有N屢次切換,其實它都是在排着隊吶。
  5. 寄存器是存上下文關係的。
  6. 進程是經過PID來區分的,並非經過進程名來區分的。進程裏面的第一個線程就是主線程,父線程和子線程是相互獨立的,只是父線程建立了子線程,父線程down了,子線程不會受到影響的。
  7. 主線程修改影響其餘線程的行文,由於它們是共享數據的。
相關文章
相關標籤/搜索