閑話Linux進程

來源:互聯網
上載者:User

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">進程是作業系統啟動並執行基礎,本文以比較粗獷的角度閑侃linux進程方面的知識。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">使用者態的每個進程在核心中對應一個task_struct結構的進程描述符,描述符中包含進程的狀態和執行資訊、虛擬記憶體資訊、開啟檔案資訊、以及檔案檔案系統資訊,訊號相關資訊等(進程使用的資源時有限制的,如開啟檔案數,使用的物理頁,檔案大小等,其值包含在描述符中task_struct->signal->rlim欄位中,也可通過ulimit
–a進行查看)。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">每個進程由一個ID來標示,稱為進程號,核心通過位元影像的管理方式來為新建立的進程分配進程號。核心將所有進程描述符使用鏈錶鏈接起來以進行統一管理(可通過ps命令查看進程的資訊),進程鏈表的頭式init_task描述符,即0進程或稱swapper進程的描述符,同時為了方便通過進程號擷取進程描述符,核心將所有的進程按照其PID(PGID,SID)鏈入到不同散列表。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">進程可通過fork系統調用進行建立,新建立的進程成為子進程,在其建立時,其複製了其父進程的所有資源,是父進程的副本。execve系統調用可以執行新的應用程式,通常fork與execve系統調用配合起來使用,通過fork建立子進程,然後在子進程中調用execve執行新的應用程式(用應用程式的虛擬位址空間替換子進程的虛擬位址空間)。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">所有進程的祖先叫做進程0,idle進程或swapper進程,它是在linux的初始化階段從無到有建立的一個核心線程,該進程使用了一系列的靜態分配的資料結構進行初始化(INIT_MM, INIT_FS等)。進程0執行cpu_idle函數,本質上是在開中斷的情況下重複執行hlt指令,只有當沒有其他進程處於可運行狀態時,發送器才選擇進程0。進程0初始化核心需要的所有資料結構後,建立一個叫進程1的核心線程(init進程),通過execve系統調用裝入/sbin/init執行,在系統關閉之前,init進程一直存活,因為它建立和監控在作業系統外層執行的所有進程的活動。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">為了方便調度,核心為進程設定了多種狀態,可運行狀態,可中斷等待狀態,不可中斷等待狀態,暫停狀態等,根據不同的條件,進程可以在不同的狀態下進行轉換。另外考慮到任務性質不同,進程具有不同的優先順序,每個可運行狀態的進程描述符同時被連結到相應的優先順序鏈表中(優先順序鏈表共140個,對應的優先順序從0-139)。而對於處於等待狀態的進程,其被劃分為若干的類別,每個類別代表一個特定的事件,並通過等待隊列來組織進程。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:
minor-fareast">調度器的主要工作包括選擇下一個要啟動並執行進程並進行環境切換(與體繫結構相關的操作,由switch_to完成)。核心的發送器主要由schedule函數完成,schedule()函數可以通過兩種方式調用:

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">1.  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">主動調度,直接調用函數schedule(),如進程退出,或者進入睡眠狀態等。

宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast;
mso-bidi-font-family:宋體;mso-bidi-theme-font:minor-fareast">2.  宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-theme-font:minor-fareast">強制性調度。置位當前進程task_struct裡面的need_resched。當是從核心態返回使用者態的時候將檢查這個位,如果發現已經被置位,會調用schedule(),和主動調度相比,強制性調度有一定的調度延時;有以下三種情況可能會置位need_resched:
a. 時鐘中斷服務程式中,發現進程已經用完自己的時間片,需要被切出CPU;
b. 當喚醒一個睡眠進程時,發現該進程比當前佔有CPU的font-family:宋體;mso-ascii-theme-font:minor-fareast;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast;mso-bidi-font-family:" times new roman mso-bidi-theme-font:minor-bidi zh-cn>進程優先順序高;
c. 一個進程通過系統調用改變調度策略、優先順序等。

minor-fareast;mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;
mso-hansi-theme-font:minor-fareast"> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.