jck28-lucio-linux进程与线程

进程

  • 可执行程序的运行态
  • 操作系统调度的基本单位
  • 线程容器
  • 进程本身包含指令、数据等资源

线程

  • 进程中被执行的最小单元
  • cpu 调度的基本单位
  • 线程带有指令、数据等资源

进程的生命周期

  • created sleep 100 ./demo.py
  • ready
  • running
  • waiting
  • terminated kill killall
    image

常用进程管理命令

  • ps 进程列表快照
  • top 交互式进程观测
  • kill killall 结束进程
  • fg 进程切换到前台
  • bg 进程切换到后台
  • ctrl z 挂起进程

ps 命令

unix 风格参数 ps -ef

[jck287213@shell.ceshiren.com ~]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 2020 ? 12:27:25 /usr/lib/systemd/systemd --system –
root 2 0 0 2020 ? 00:00:14 [kthreadd]
root 3 2 0 2020 ? 05:32:42 [ksoftirqd/0]
root 5 2 0 2020 ? 00:00:00 [kworker/0:0H]
root 7 2 0 2020 ? 00:06:45 [migration/0]
root 8 2 0 2020 ? 00:00:00 [rcu_bh]
root 9 2 0 2020 ? 06:15:29 [rcu_sched]
root 12 2 0 2020 ? 00:06:48 [migration/1]

bsd 风格参数 ps aux

[jck287213@shell.ceshiren.com ~]$ ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 52052 3240 ? Ss 2020 747:25 /usr/lib/systemd/sy
root 2 0.0 0.0 0 0 ? S 2020 0:14 [kthreadd]
root 3 0.0 0.0 0 0 ? S 2020 332:42 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 2020 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 2020 6:45 [migration/0]
root 8 0.0 0.0 0 0 ? S 2020 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S 2020 375:29 [rcu_sched]
root 12 0.0 0.0 0 0 ? S 2020 6:48 [migration/1]
root 13 0.0 0.0 0 0 ? S 2020 334:39 [ksoftirqd/1]

[jck287213@shell.ceshiren.com ~]$ ps -o pid,ppid,psr,thcount,tid,cmd -M
PID PPID PSR THCNT TID CMD
20993 20992 - 1 - -bash
- - 0 - 20993 -
21372 20993 - 1 - ps -o pid,ppid,psr,thcount,tid,cmd -M
- - 0 - 21372 -

ps -o uname,pid,ppid,psr,thcount,tid,pcpu,pmem,cmd -M
[jck287213@shell.ceshiren.com ~]$ ps -o uname,pid,ppid,psr,thcount,tid,pcpu,pmem,cmd -M
USER PID PPID PSR THCNT TID %CPU %MEM CMD
jck2872+ 20993 20992 - 1 - 0.0 0.0 -bash
jck2872+ - - 1 - 20993 0.0 - -
jck2872+ 23023 20993 - 1 - 0.0 0.0 ps -o uname,pid,ppid,psr,thcount,tid,
jck2872+ - - 0 - 23023 0.0 - -
[jck287213@shell.ceshiren.com ~]$

gnu 风格参数 ps --pid pidlist

[jck287213@shell.ceshiren.com ~]$ ps --pid 1
PID TTY TIME CMD
1 ? 12:27:25 systemd

Linux 进程运行情况

[jck287213@shell.ceshiren.com ~]$ ps -o pid,ppid,psr,thcount,tid,cmd -M
PID PPID PSR THCNT TID CMD
20993 20992 - 1 - -bash
- - 0 - 20993 -
22428 20993 - 1 - ps -o pid,ppid,psr,thcount,tid,cmd -M
- - 1 - 22428 -

MAC 进程运行情况

[jck287213@shell.ceshiren.com ~]$ ps -M -o ppid
PPID
20992
-
20993
-

进程状态

  • 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
    image

进程知识练习

  • 查看每一步进程状态
#创建2个子进程,4个子线程
python demo.py 2 4 &
jobs
fg
ctrl z
bg