linux 进程和线程
进程与线程
进程
进程包含线程
进程生命周期
线程
进程中被执行的最小单元
CPU调度的基本单位
常用进程管理命令
1. ps 查看进程列表
2.top 交互式进程观测
3. kill
4. fg 进程切换到前台
5. bg 进程切换到后台
6. ctrl z 挂起进程
ps命令:
非交互式,查看进程快照
ps -ef 展示所有进程信息
ps -o uname,pid,ppid,psr,thcount,tid,pcpu,pmem,cmd -m
top命令:
实时监控进程
实战:
run demo.py
多进程与多线程的示例代码
if __name__ == '__main__':
process_count = int(sys.argv[1])
thread_count = int(sys.argv[2])
multi_process(process_count)
multi_thread(thread_count)
input()
窗口敲入运行该py文件
Dashuo:$ python3 demo.py 2 3 -->创建2个进程,3个线程
thread_demo 0 pid=16651 thread_name=Thread-1
thread_demo 2 pid=16651 thread_name=Thread-3
thread_demo 1 pid=16651 thread_name=Thread-2
process_demo 0 pid=16652 thread_name=MainThread
process_demo 1 pid=16653 thread_name=MainThread
进程默认在前台
ctrl z 停止进程
查看进程
Dashuo:$ ps -o uname,pid,ppid,psr,thcount,tid,pcpu,pmem,cmd -m
USER PID PPID PSR THCNT TID %CPU %MEM CMD
...
manshuo 16651 (进程号) 8137(父进程号) - 4 - 0.0 0.0 python3 demo.py 2 3
manshuo - - 2 - 16651 0.0 - - -->四个线程
manshuo - - 1 - 16663 0.0 - -
manshuo - - 0 - 16664 0.0 - -
manshuo - - 3 - 16675 0.0 - -
manshuo 16652 16651 - 1 - 0.0 0.0 python3 demo.py 2 3
manshuo - - 0 - 16652 0.0 - -
manshuo 16653 16651 - 1 - 0.0 0.0 python3 demo.py 2 3
manshuo - - 6 - 16653 0.0 - -
#
PID 进程号
PPID 父进程号
PSR 当前进程运行在哪个CPU
thcount 线程数量
tid 每个线程唯一标记
再加个参数stat
ps -o uname,pid,ppid,psr,thcount,tid,pcpu,pmem,stat,cmd -m
USER PID PPID PSR THCNT TID %CPU %MEM STAT CMD
manshuo 8137 8130 - 1 - 0.0 0.0 - bash
manshuo - - 2 - 8137 0.0 - Ss -
manshuo 13251 8137 - 1 - 0.0 0.0 - less
manshuo - - 3 - 13251 0.0 - T -
manshuo 16651 8137 - 4 - 0.0 0.0 - python3 demo.py 2 3
manshuo - - 2 - 16651 0.0 - Tl -
manshuo - - 3 - 16663 0.0 - Tl -
manshuo - - 4 - 16664 0.0 - Tl -
manshuo - - 1 - 16675 0.0 - Tl -
manshuo 16652 16651 - 1 - 0.0 0.0 - python3 demo.py 2 3
manshuo - - 0 - 16652 0.0 - T -
manshuo 16653 16651 - 1 - 0.0 0.0 - python3 demo.py 2 3
manshuo - - 6 - 16653 0.0 - T -
进程状态
D uninterruptible sleep (usually IO)
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped by job control signal
t stopped by debugger during the tracing
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent

浙公网安备 33010602011771号