2.2.1 进程状态命令
查看与设置进程资源限制
$ ulimit -a // 查看全部进程资源限制
// About more of shell command: "ulimit --help" or "help ulimit"
// About more of linux api: "man 2 getrlimit" or "man 2 setrlimit"
查看进程状态快照
$ ps aux // 或 ps ajx
-
(默认)
:显示当前终端进程及其子进程的粗略信息。 -
a
:显示终端上的所有进程,包括其他用户进程。 -
x
:显示无终端的所有进程,不包括其他用户进程。 -
u
:显示进程详细信息。多为用户关注信息,如:CPU
占用、内存占用、STAT
状态。 -
j
:显示进程详细信息。多为控制相关信息,如:PPID
、GID
、STAT
状态。
STAT
状态:
状态:
R 运行或就绪
S 可中断的睡眠
D 不可中断的睡眠 (usually IO)
Z 僵尸进程 (nearly dead)
X 已死 (should never be seen)
I 空闲的内核线程
T 停止或被追踪
t 追踪时被调试器停止
其他:
< 高优先级 (not nice to other users)
N 低优先级 (nice to other users)
s 拥有子进程
l 拥有多线程
+ 前台进程
监视实时进程状态
$ top
$ top -d 1 // 设置刷新时间间隔(秒)
按键操作:
P
:按CPU
占用排序。M
:按内存占用排序。T
:按运行时长排序。U
:按用户名筛选。K
:杀死进程,输入无效数据退出。q
:退出top
。
杀死进程
kill <pid> // 杀死进程(默认通过 15 号信号)
kill -9 <pid> // 通过 9 号信号强制杀死进程(自杀, 杀死暂停进程), "kill -l" 查看所有信号。
查看与设置进程号
getpid
,getppid
,getpgid
,setpgid
#include <sys/types.h>
#include <unistd.h>
// get process id
// return value:
// return process id of current process, or -1 for error
pid_t getpid(void);
// get parent process id
// return value:
// return parent process id of current process, or -1 for error
pid_t getppid(void);
// About more:
// $ man 2 getpid
#include <sys/types.h>
#include <unistd.h>
// get group id
// pid:
// process id
// return value:
// return group id of the process, or -1 for error
pid_t getpgid(pid_t pid);
// set group id
// pid:
// process id
// pgid:
// group id
// return value:
// return 0 on success, -1 for error
// set the group id of the process to the given group id
int setpgid(pid_t pid, pid_t pgid);
// About more:
// $ man 2 getpgid