Linux進程及管理(1)
內核的功用:進程管理、文件系統、網絡功能、內存管理、驅動程序、安全功能
Process: 運行中的程序的一個副本;
存在生命周期Linux內核存儲進程信息的固定格式:task struct
多個任務的的task struct組件的鏈表:task list
進程創建:
init
父子關系
進程:都由其父進程創建
fork(), clone()
進程優先級:
0-139:
1-99:實時優先級;
100-139:靜態優先級;
數字越小,優先級越高;
Nice值:
-20,19
Big O
O(1), O(logn), O(n), O(n^2), O(2^n)
進程內存:
Page Frame: 頁框,用存儲頁面數據
存儲Page
MMU:Memory Management Unit
IPC: Inter Process Communication
同一主機上:
signal
shm: shared memory
semerphor
不同主機上:
rpc: remote procecure call
socket:
Linux內核:搶占式多任務
進程類型:
守護進程: 在系統引導過程中啟動的進程,跟終端無關的進程;
前臺進程:跟終端相關,通過終端啟動的進程
注意:也可把在前臺啟動的進程送往后臺,以守護模式運行;
進程狀態:
運行態:running
就緒態:ready
睡眠態:
可中斷:interruptable
不可中斷:uninterruptable
停止態:暫停于內存中,但不會被調度,除非手動啟動之;stopped
僵死態:zombie
進程的分類:
CPU-Bound
IO-Bound
Linux進程查看及管理的工具:pstree, ps, pidof, pgrep, top, htop, glance, pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup
pstree命令:
pstree – display a tree of processes
例一:查看文件pstree命令以樹狀圖的方式展現進程之間的派生關系,顯示效果比較直觀。
[root@localhost~`]# pstree
ps: process state
ps – report a snapshot of the current processes
Linux系統各進程的相關信息均保存在/proc/PID目錄下的各文件中;
ps [OPTION]…
選項:支持兩種風格
常用組合:aux
u: 以用戶為中心組織進程狀態信息顯示
a: 與終端相關的進程;
x: 與終端無關的進程;
~]# ps aux
例一:顯示所有進程
[root@localhost ~]# ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.7 128356 7064 ? Ss 02:42 0:09 /usr/lib/systemd/systemd --swit root 2 0.0 0.0 0 0 ? S 02:42 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 02:42 0:00 [ksoftirqd/0] root 7 0.0 0.0 0 0 ? S 02:42 0:00 [migration/0]
USER 進程屬組 PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
USER 進程屬組
VSZ: Virtual memory SiZe,虛擬內存集
RSS: ReSident Size, 常駐內存集
STAT:進程狀態
R:running
S: interruptable sleeping
D: uninterruptable sleeping
T: stopped
Z: zombie
+: 前臺進程
l: 多線程進程
N:低優先級進程
<: 高優先級進程
s: session leader
常用組合:-ef
-e: 顯示所有進程
-f: 顯示完整格式程序信息
例一:顯示所有進程進信息
[root@localhost ~]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 02:42 ? 00:00:09 /usr/lib/systemd/systemd --switched-root --syst root 2 0 0 02:42 ? 00:00:00 [kthreadd] root 3 2 0 02:42 ? 00:00:00 [ksoftirqd/0]
常用組合:-eFH
-F: 顯示完整格式的進程信息
-H: 以進程層級格式顯示進程相關信息
例一:顯示所有信息以層級格式顯示
[root@localhost ~]# ps -eFH |head UID PID PPID C SZ RSS PSR STIME TTY TIME CMD root 2 0 0 0 0 0 02:42 ? 00:00:00 [kthreadd] root 3 2 0 0 0 0 02:42 ? 00:00:00 [ksoftirqd/0] root 7 2 0 0 0 0 02:42 ? 00:00:00 [migration/
常用組合:-eo, axo
-eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,comm
axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
o:指明要顯示的字段
ni: nice值
pri: priority,優先級
psr: processor, CPU
rtprio: 實時優先級
例一:指明要顯示的字段信息
[root@localhost ~]# ps -axo pid,psr,stat,comm | head PID PSR STAT COMMAND 1 0 Ss systemd 2 0 S kthreadd 3 0 S ksoftirqd/0 7 0 S migration/0 8 0 S rcu_bh 9 0 R rcu_sched 10 0 S watchdog/0 12 0 S< khelper 13 0 S kdevtmpfs
pgrep, pkill:過濾信息
pgrep [options] pattern
pkill [options] pattern
-u uid: effective user
-U uid: real user
-t terminal: 與指定終端相關的進程
-l: 顯示進程名
-a: 顯示完整格式的進程名
-P pid: 顯示其父進程為此處指定的進程的進程列表
例一:顯示root進程號信息
[root@localhost ~]# pgrep -u root -l 1 systemd 2 kthreadd 3 ksoftirqd/0 7 migration/0 8 rcu_bh 9 rcu_sched
pidof:
根據進程名獲取其PID;
例一:根據進程名獲取PID
[root@localhost ~]# pidof sshd 9168 1560
top:top命令可以實時動態地查看系統的整體運行情況
有許多內置命令:
排序:
P:以占據的CPU百分比;
M:占據內存百分比;
T:累積占據CPU時長;
首部信息顯示:
uptime信息:l命令
tasks及cpu信息:t命令
cpu分別顯示:1 (數字)
memory信息:m命令
退出命令:q
修改刷新時間間隔:s
終止指定進程:k
例一:修改刷新時間5秒
Change delay from 5.0 to
選項:
-d #: 指定刷新時間間隔,默認為3秒;
-b: 以批次方式;
-n #: 顯示多少批次;
htop命令:交互式進程查看器,查看關鍵性能數據
例一:查看系統進程信息
[root@localhost ~]# htop
選項:
-d #: 指定延遲時間;
-u UserName: 僅顯示指定用戶的進程;
-s COLOMN: 以指定字段進行排序;
命令:
s: 跟蹤選定進程的系統調用;
l: 顯示選定進程打開的文件列表;
a:將選定的進程綁定至某指定CPU核心;
t: 顯示進程樹
注意:Fedora-EPEL源
進程查看工具:pstree, ps, pgrep, pidof, top, htop
原創文章,作者:kang,如若轉載,請注明出處:http://www.www58058.com/77034