Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process management(程序管理)

Similar presentations


Presentation on theme: "Process management(程序管理)"— Presentation transcript:

1 Process management(程序管理)
Computer Science & Information Management Process management(程序管理)

2 程式與程序 「執行一個程式或指令」就可以『觸發』 事件而取得PID。

3 程式與程序 程式 (program):通常為 binary program , 放置在儲存媒體中 (如硬碟、光碟、軟碟、 磁帶等)。
程序 (process):程式被觸發後,執行者的 權限與屬性、程式的程式碼與所需資料等 都會被載入記憶體中, 作業系統並給予這 個記憶體內的單元一個識別碼 (PID),可以 說,程序就是一個正在運作中的程式。

4 查看process狀態 指令ps可查看process狀態 ps –l可查看更進階的狀態內容。(如父程序)

5 process專用函式 函式 用途 getpid() 取得pid getppid() 取得父程序(parent)的pid fork()
產生子程序(child)

6 練習 撰寫p1.c程式,印出執行程式的pid以及父 程序pid(ppid)。 #include<stdio.h> main(){
printf(“PID: %d, PPID: %d”, , ); } 編譯:gcc p1.c (-o 檔名) 執行:a.out (執行檔檔名)

7 Child 1 (PID=4) Child 2 (PID=5) Child 3 (PID=6)
產生子程序(child process) Unix啟動時剛開始只有一個程序(init),其 pid為1。 使用fork指令可以產生子程序 Parent, init (PID 1) Child 1 (PID=4) Child 2 (PID=5) Child 3 (PID=6)

8 產生子程序(child process) fork 重製一份程序(process) 當fork執行成功
回傳子程序(child)的PID給父程序(parent)。 回傳數值0給子程序(child)。

9 練習 撰寫p2.c程式並在程式中fork出子程序。 利用if判斷該程序為parent 或 child,並印出 其pid。
#include<stdio.h> main(){ printf(“PID: %d”, getpid()); int child = fork(); if(…) }

10 process排程 若有兩個以上的程序(parent, child)在執行, 且皆會使用大量的電腦資源(CPU運算、IO)。 則電腦會按照作業系統的排程法則來運作。 試撰寫大量迴圈運算,並利用父子程序搶 奪資源,查看執行狀態。


Download ppt "Process management(程序管理)"

Similar presentations


Ads by Google