- 基本概念
- 进程
- 线程
- 进程的生命周期
- 常用进程管理命令
- 常用命令
- 进程命令练习
- 可执行程序的运行态
- 操作系统调度的基本单位
- 线程的容器 进
- 程本身包含指令、数据等资源
- 进程中被执行的最小单元
- CPU调度的基本单位
- 线程带有指令、数据等资源
ps : 进程列表快照 ps -ef : 获取所有进程列表 ps aux : 获取所有进程列表,并可提供更多可用数据 ps -o pid,ppid,psr,thcount,tid,cmd -M : 自定义输出指标 top : 交互式进程观测 kill : 结束进程 fg : 进程切换到前台 bg : 进程切换到后台 ctrl+z : 挂起进程,并且让进城转到后台
进程切换到前后台:
前台:界面上进行交互的进程
后台:进程脱离了界面
执行一个命令就是创建一个进程。
进程命令练习测试脚本demo.py
import multiprocessing
import os
import sys
import threading
from time import sleep
def thread_demo(index):
while True:
print(f"thread_demo {index} pid={os.getpid()} thread_name={threading.current_thread().name}")
sleep(1)
def process_demo(index):
while True:
print(f"process_demo {index} pid={os.getpid()} thread_name={threading.current_thread().name}")
sleep(1)
def multi_thread(count):
for index in range(count):
thread = threading.Thread(target=thread_demo, args=[index])
thread.start()
sleep(1)
def multi_process(count):
for index in range(count):
process = multiprocessing.Process(target=process_demo, args=(index,))
process.start()
sleep(1)
if __name__ == '__main__':
process_count = int(sys.argv[1])
thread_count = int(sys.argv[2])
multi_process(process_count)
multi_thread(thread_count)
input()
运行文件python demo.py 3 4 &,其中&符号表示进程后台运行。
执行后可用ps命令查看进程。
进程的状态:
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



