Chapter 8 Controlling Processes(控制行程)

What is Process
   
UNIX中Pocess代表一個具有獨立記憶空間、可單獨運作的”工作程式”(Program),每一件系統或User的工作均由各種不同的process完成之

Why Process
 
   UNIX中,很多resource的管理,必須藉由process的控制來完成

Process ID

   UNIX每一個程序都有一個唯一的識別代號,我們稱之為程序代號(process id or pid)

How Process Works
  
每一process在必要情況之下由其Parent Process產生,完成工作之後會自動釋放所有佔用的系統資源,結束並離開系統。
 

 

Process的產生


fork-and-exec是UNIX中所有process運作的模式。系統bootup的時候,第一個被執行的process(程式),其pid為1;經由fork-and-exec程序,啟動各個必要的process,而形成一個功能完整的作業系統。
±
fork是UNIX一個系統呼叫(system call),process fork時,會複製一個跟自己完全一模一樣的process (with different pid),並利用系統呼叫完成之傳回值,來區分parent process 與child process,而分別賦予child process不同的功能。
process於程式執行過程中,利用exec執行另一不同的程式,這個程式並且會完全取代原有程式(with the same pid)。

 

 


process及child process。
如果因某些原因,parent process在其結束前即已不存在,此 process即成為所謂的Zombie process(or defunct process),無法正常結束。

Child process通常會隨著parent process結束而結束,因此手動結束process “init”(系統第一個啟動的process, pid為1),將會造成系統當機。(目前大部分的UNIX作業系統會禁止你手動停止init的執行)

 

 

Process的管理相關指令

 1. Monitor Proces with Command : ps

 

2. pstree -- 顯示執行程序的樹狀結構

3. kill pid -- 結束 Process 的執行

4. nice/renice -- Process Priority的改變

背景執行程式

程式執行不一定要在螢幕前進行,可以用背景程式管理的指令來達成目的,如 &, bg, fg, jobs等.

1. &

   語法: [root@test/root]# command &

  Example: [root @test /root]#find / -name test & <==將該執行程序丟到背景執行

2. jobs

   說明 使用 jobs 就可以知道背景中執行的工作項目有那些.
      Example: [root @test /root]#jobs
            [1]+  Stopped    find / -name test &  <==顯示有一個工作在背景下,狀態為停止。

3. fg

  說明將背景中的指令移到螢幕前來動作

     Example: [root @test /root]# jobs
           [1]-  Running                 find / -name test &
           [2]+  Stopped                 vi test
           [root @test /root]# fg %1

4. bg

  說明:將背景中的程式由 stopped 變成 Running

     Example: [root @test /root]#jobs
           [1]-  Stopped                 find / -name test &
           [root @test /root]# bg %1
           [root @test /root]# jobs
           [1]-  Running                 find / -name test &
 

Monitor Proces with Command Utility : "top" - 一個免費的工具軟體
    (top is not a standard UNIX Command)

 

[root@nmc ~]# top

3:56pm up 53 days, 22:03, 1 user, load average: 0.00, 0.00, 0.00
60 processes: 59 sleeping, 1 running, 0 zombie, 0 stopped
CPU states: 0.7% user, 1.9% system, 0.0% nice, 97.2% idle
Mem: 128144K av, 124852K used, 3292K free, 60620K shrd, 82020K buff
Swap: 104412K av, 452K used, 103960K free15784K cached
 
PID USER PRI NI SIZE RSS SHARE STAT LIB %CPU %MEM TIME COMMAND
19512 root 15 0 860 860 664 R 0 2.9 0.6 0:00 top
1 root 0 0 472 472 400 S 0 0.0 0.3 0:04 init
2 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 kflushd
3 root 0 0 0 0 0 SW 0 0.0 0.0 0:10 kupdate
4 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 kpiod
5 root 0 0 0 0 0 SW 0 0.0 0.0 0:01 kswapd
319 bin 0 0 412 400 320 S 0 0.0 0.3 0:00 portmap
333 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 lockd
334 root 0 0 0 0 0 SW 0 0.0 0.0 0:00 rpciod
343 root 0 0 520 516 428 S 0 0.0 0.4 0:00 rpc.statd
396 root 0 0 472 468 376 S 0 0.0 0.3 0:05 syslogd
405 root 0 0 616 612 304 S 0 0.0 0.4 0:00 klogd
419 nobody 0 0 552 540 428 S 0 0.0 0.4 0:00 identd
422 nobody 0 0 552 540 428 S 0 0.0 0.4 0:00 identd
423 nobody 0 0 552 540 428 S 0 0.0 0.4 0:00 identd
425 nobody 0 0 552 540 428 S 0 0.0 0.4 0:00 identd
426 nobody 0 0 552 540 428 S 0 0.0 0.4 0:00 identd