linux 可视化进程管理,Linux实践 | 进程管理杂记

分别用fork、vfork创建进程

fork

e8d601a7b8c325aa66dab0a1ad0da1e9.png

编辑文件

gedit fork.c

#include

#include

#include

int main(void){

pid_t pid;

pid = fork();

if(pid<0){

printf("fail to fork\n");

}else if(pid==0){

printf("this is the child, pid is : %u\n", getpid());

}else if(pid>0){

printf("this is the parent\n");

}

exit(0);

return 0;

}

编译文件

gcc fork.c -o fork

运行进程

./fork

./ 表示当前目录,如果没有就会在PATH找前缀路径

vfork

3447a0b8673f2e998bc041c56c7ad7da.png

#include

#include

#include

int global; //global variable in date paragraph

int main(){

pid_t pid;

int stack = 1; //local variable in stack

int *heap;

heap = (int *)malloc(sizeof(int)); //in heap

*heap = 2; //pid = fork();

pid = vfork();

if(pid < 0){

printf("fail to fork\n");

exit(1);

}else if(pid == 0){

global++;

stack++;

(*heap)++;

printf("the child, data : %d, stack : %d, heap : %d\n", global, stack, *heap);

exit(0);

}

printf("the child, data : %d, stack : %d, heap : %d\n", global, stack, *heap);

return 0;

}

步骤同上

显示进程的相关信息(pid,ppid,state,parent,real_parent,….)并解释

PID — 进程id

USER — 进程所有者

PR — 进程优先级

NI — nice值。负值表示高优先级,正值表示低优先级

VIRT — 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES

RES — 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA

SHR — 共享内存大小,单位kb

S — 进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程

%CPU — 上次更新到现在的CPU时间占用百分比

%MEM — 进程使用的物理内存百分比

TIME+ — 进程使用的CPU时间总计,单位1/100秒

COMMAND — 进程名称(命令名/命令行)

ps显示所有运行中的进程

ps aux | less

其中,

-A:显示所有进程

a:显示终端中包括其它用户的所有进程

x:显示无控制终端的进程

任务:查看系统中的每个进程。

ps -A

ps -e

c17d8910508f7307bbc541aaf170f8b8.png

任务:查看非root运行的进程

ps -U root -u root -N

任务:查看用户vivek运行的进程

ps -u jackherrick

* top命令提供了运行中系统的动态实时视图。*

top

e6760e9a69fed1da3d48823b61e679de.png

任务:显示进程的树状图。

pstree

d6ca38f14a113f01c335e735ead2e125.png

查看PID PPID

ps -ef

7352c14e517ca5af4bd946b7cf807610.png

进阶版,查看指定进程

ps -ef | grep firefox

相当于在 ps -df 基础上加一个管道符“|”,搜索firefox

杀死进程

kill -s 9 1827

Linux下ps -ef和ps aux的区别

ps aux 是用BSD的格式来显示 java这个进程

显示的项目有:USER , PID , %CPU , %MEM , VSZ , RSS , TTY , STAT , START , TIME , COMMAND

ps -ef是用标准的格式显示java这个进程

显示的项目有:UID , PID , PPID , C , STIME , TTY , TIME , CMD

linux上进程有5种状态:

1.运行(正在运行或在运行队列中等待)

2.中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)

3.不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生)

4.僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放)

5.停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行)

ps工具标识进程的5种状态码:

D 不可中断 uninterruptible sleep (usually IO)

R 运行 runnable (on run queue)

S 中断 sleeping

T 停止 traced or stopped

Z 僵死 a defunct (”zombie”) process

grep

grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

ps aux、ps –aux 、ps -ef

1. ps aux和ps –aux

man ps 之后得到的结果:

ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top(1) instead.

Note that “ps -aux” is distinct from “ps aux”. The POSIX and UNIX standards require that “ps -aux” print all processes owned by a user named “x”, as well as printing all processes that would be selected by the -a option. If the user named “x” does not exist, this ps may interpret the command as “ps aux” instead and print a warning.

This behavior is intended to aid in transitioning old scripts and habits. It is fragile, subject to change, and thus should not be relied upon.

意思是:

请注意”ps -aux”不同于”ps aux”。POSIX和UNIX的标准要求”ps -aux”打印用户名为”x”的用户的所有进程,以及打印所有将由-a选项选择的过程。如果用户名为”x”不存在,ps的将会解释为”ps aux”,而且会打印一个警告。这种行为是为了帮助转换旧脚本和习惯。它是脆弱的,即将更改,因此不应依赖。

如果你运行ps -aux >/dev/null,那么你就会得到下面这行警告信息

Warning: bad ps syntax, perhaps a bogus ‘-‘? See http://procps.sf.net/faq.html

ps aux 和ps -ef

两者的输出结果差别不大,但展示风格不同。aux是BSD风格,-ef是System V风格。这是次要的区别,一个影响使用的区别是aux会截断command列,而-ef不会。当结合grep时这种区别会影响到结果。

举例请参考:http://www.2cto.com/os/201303/197697.html

综上:以上三个命令推荐使用:ps –ef

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值