
On Linux, all running process have "metadata" stored in the /proc filesystem .
# man ps
# ps -C sshd --no-header
# ps -C dockerd --no-header
-C cmdlist
Select by command name. This selects the processes whose executable name is given in cmdlist.
you can use the long options --headers and --no-headers to enable printing headers each page or disable headers entirely, respectively.
# ps -eo nlwp,pcpu,user,pid,ppid,args --sort nlwp
注:以 nlwp排序,默认正序
LWP -- 轻量级进程,即线程
NLWP --线程数,即 number of threads in process
# ps -eo pid,cmd,%mem,%cpu --sort=-%mem
Thecommand is highly valuable for system admins during troubleshooting the system.
You can display the processes list in a sorted manner to find out the highest memory usage processes.
# ps -mp ${pid} -o THREAD,tid,time
参数解释:
-m:显示所有的线程
-p:pid进程使用CPU的时间
-o:该参数后是用户自定义格式。
如:THREAD,tid,time表示线程、线程ID号、线程占用的时间。
$ ps -Hp ${pid}
ps(1) — Linux manual page
https://man7.org/linux/man-pages/man1/ps.1.html
Linux PS command with examples
https://www.fosslinux.com/43296/linux-ps-command-with-examples.htm
How to get all process ids without ps command on Linux?
https://stackoverflow.com/questions/32913424/how-to-get-all-process-ids-without-ps-command-on-linux
What is the difference between ps and top command?
https://unix.stackexchange.com/questions/62176/what-is-the-difference-between-ps-and-top-command
显示其他用户启动的进程(a)查看系统中属于自己的进程(x)
启动这个进程的用户和它启动的时间(u)
使用“date -s”命令来修改系统时间
比如将系统时间设定成1996年6月10日的命令如下。
#date -s 06/10/96
将系统时间设定成下午1点12分0秒的命令如下。
#date -s 13:12:00
------------------------------------------------------
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 1340 440 ? S Nov05 0:04 init
root 2 0.0 0.0 0 0 ? SW Nov05 0:00 [keventd]
root 3 0.0 0.0 0 0 ? SW Nov05 0:00 [keventd]
...
------------------------------------------------------
USER域指明了是哪个用户启动了这个命令
用户可以查看某个进程占用了多少CPU
内存使用及其VSZ(虚拟内存大小)和RSS(常驻集大小):
VSZ表示如果一个程序完全驻留在内存的话需要占用多少内存空间
RSS指明了当前实际占用了多少内存
STAT显示了进程当前的状态:
"S":进程处在睡眠状态,表明这些进程在等待某些事件发生--可能是用户输入或者系统资源的可用性
last命令可以有效的查看系统登录事件
在一个进程调用了exit之后,该进程并非马上就消失掉,而是留下一个称为僵尸进程(Zombie)的数据结构。在Linux进程的5种状态中,僵尸进程是非常特殊的一种,它已经放弃了几乎所有内存空间,没有任何可执行代码,也不能被调度,仅仅在进程列表中保留一个位置,记载该进程的退出状态等信息供其他进程收集,除此之外,僵尸进程不再占有任何内存空间。
系统调用exit的作用是使进程退出,但也仅仅限于将一个正常的进程变成一个僵尸进程,并不能将其完全销毁。
进程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出,如果让它找到了这样一个已经变成僵尸的子进程,wait 就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。
转载 ps aux 中STAT 解释 收藏
运行 ps aux 的到如下信息:
ps aux
USERPID %CPU %MEM VSZ RSS TTYSTAT START TIME COMMAND
smmsp 3521 0.00.7 6556 1616 ?Ss 20:40 0:00 sendmail: Queue runner@01:00:00 f
root35320.0 0.2 2428452 ?Ss 20:40 0:00 gpm -m /dev/input/mice -t imps2
htt35630.0 0.02956196 ?Ss 20:41 0:00 /usr/sbin/htt -retryonerror 0
htt35640.0 1.7 29460 3704 ?Sl20:41 0:00 htt_server -nodaemon
root 35740.0 0.4 5236 992 ?Ss 20:41 0:00 crond
xfs36170.0 1.3 13572 2804 ?Ss 20:41 0:00 xfs -droppriv -daemon
root 3627 0.0 0.2 3448552 ?SNs 20:41 0:00 anacron -s
root 36360.0 0.1 2304420 ?Ss 20:41 0:00 /usr/sbin/atd
dbus36550.0 0.5 13840 1084 ?Ssl 20:41 0:00 dbus-daemon-1 --system
....................................
stat 中的参数意义如下:
D 不可中断 Uninterruptible(usually IO)
R 正在运行,或在队列中的进程
S 处于休眠状态
T 停止或被追踪
Z 僵尸进程
W 进入内存交换(从内核2.6开始无效)
X 死掉的进程
<高优先级
n 低优先级
s 包含子进程
+ 位于后台的进程组
======================================
ps命令
要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而ps命令就是最基本同时也是非常强大的进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵尸、哪些进程占用了过多的资源等等.总之大部分信息都是可以通过执行该命令得到的.
ps命令最常用的还是用于监控后台进程的工作情况,因为后台进程是不和屏幕键盘这些标准输入/输出设备进行通信的,所以如果需要检测其情况,便可以使用ps命令了.
1)ps a 显示现行终端机下的所有程序,包括其他用户的程序。
2)ps -A 显示所有程序。
3)ps c 列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。
4)ps -e 此参数的效果和指定"A"参数相同。
5)ps e 列出程序时,显示每个程序所使用的环境变量。
6)ps f 用ASCII字符显示树状结构,表达程序间的相互关系。
7)ps -H 显示树状结构,表示程序间的相互关系。
8)ps -N 显示所有的程序,除了执行ps指令终端机下的程序之外。
9)ps s 采用程序信号的格式显示程序状况。
10)ps S 列出程序时,包括已中断的子程序资料。
11)ps -t<终端机编号> 指定终端机编号,并列出属于该终端机的程序的状况。
12)ps u 以用户为主的格式来显示程序状况。
13)ps x 显示所有程序,不以终端机来区分。
最常用的方法是ps -aux,然后再利用一个管道符号导向到grep去查找特定的进程,然后再对特定的进程进行 *** 作。
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
注: 其它状态还包括W(无驻留页), <(高优先级进程), N(低优先级进程), L(内存锁页).
使用ps格式输出来查看进程状态:
ps -eo user,stat..,cmd
user 用户名
uid 用户号
pid 进程号
ppid 父进程号
size 内存大小, Kbytes字节.
vsize 总虚拟内存大小, bytes字节(包含code+data+stack)
share 总共享页数
nice 进程优先级(缺省为0, 最大为-20)
priority(pri) 内核调度优先级
pmem 进程分享的物理内存数的百分比
trs 程序执行代码驻留大小
rss 进程使用的总物理内存数, Kbytes字节
time 进程执行起到现在总的CPU暂用时间
stat 进程状态
cmd(args) 执行命令的简单格式
例子:
查看当前系统进程的uid,pid,stat,pri, 以uid号排序.
ps -eo pid,stat,pri,uid –sort uid
查看当前系统进程的user,pid,stat,rss,args, 以rss排序.
ps -eo user,pid,stat,rss,args –sort rss
名称:ps
使用权限:所有使用者
使用方式:ps [options] [--help]
说明:显示瞬间行程 (process) 的动态
参数:
ps 的参数非常多, 在此仅列出几个常用的参数并大略介绍含义
-A 列出所有的行程
-w 显示加宽可以显示较多的资讯
-au 显示较详细的资讯
-aux 显示所有包含其他使用者的行程
au(x) 输出格式 :
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
USER: 行程拥有者
PID: pid
%CPU: 占用的 CPU 使用率
%MEM: 占用的记忆体使用率
VSZ: 占用的虚拟记忆体大小
RSS: 占用的记忆体大小
TTY: 终端的次要装置号码 (minor device number of tty)
STAT: 该行程的状态:
D: 不可中断的静止
R: 正在执行中
S: 静止状态
T: 暂停执行
Z: 不存在但暂时无法消除
W: 没有足够的记忆体分页可分配
<: 高优先序的行程
N: 低优先序的行程
L: 有记忆体分页分配并锁在记忆体内
START: 行程开始时间
TIME: 执行的时间
COMMAND:所执行的指令
【缓冲区溢出的处理】你屋子里的门和窗户越少,入侵者进入的方式就越少……
由于缓冲区溢出是一个编程问题,所以只能通过修复被破坏的程序的代码而解决问题。如果你没有源代码,从上面“堆栈溢出攻击”的原理可以看出,要防止此类攻击,我们可以:
① 开放程序时仔细检查溢出情况,不允许数据溢出缓冲区。由于编程和编程语言的原因,这非常困难,而且不适合大量已经在使用的程序;
② 使用检查堆栈溢出的编译器或者在程序中加入某些记号,以便程序运行时确认禁止黑客有意造成的溢出。问题是无法针对已有程序,对新程序来讲,需要修改编译器;
③ 经常检查你的 *** 作系统和应用程序提供商的站点,一旦发现他们提供的补丁程序,就马上下载并且应用在系统上,这是最好的方法。但是系统管理员总要比攻击者慢 一步,如果这个有问题的软件是可选的,甚至是临时的,把它从你的系统中删除。举另外一个例 子,你屋子里的门和窗户越少,入侵者进入的方式就越少。
----------------------------------------------------------------------------------------------------------------------------------------
char buf[3]
memset(buf,0x55,10)
这个程序就存在溢出
对数据块的访问超出该数据块的地址范围
===================================================================================
【一个检测工具】
Valgrind 是一款 Linux下(支持 x86、x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的 malloc 和 free,以及 C++ 中的 new 和 delete),找出内存泄漏问题。
Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误:
使用未初始化的内存 (Use of uninitialised memory)
使用已经释放了的内存 (Reading/writing memory after it has been free’d)
使用超过 malloc 分配的内存空间(Reading/writing off the end of malloc’d blocks)
对堆栈的非法访问(Reading/writing inappropriate areas on the stack)
申请的空间是否有释放(Memory leaks – where pointers to malloc’d blocks are lost forever)
malloc/free/new/delete 申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])
src 和 dst 的重叠(Overlapping src and dst pointers in memcpy() and related functions)
重复 free
① 编译安装 Valgrind:
# wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2
# tar xvf valgrind-3.4.1.tar.bz2
# cd valgrind-3.4.1/
# ./configure
…………
Primary build target: X86_LINUX
Secondary build target:
Default supp files: exp-ptrcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.5.supp
# make
# make install
# whereis valgrind
valgrind:
/usr/bin/valgrind
/usr/lib/valgrind
/usr/local/bin/valgrind
/usr/local/lib/valgrind
/usr/include/valgrind
/usr/share/man/man1/valgrind.1.gz
运行程序
使用示例:对“ls”程序进程检查,返回结果中的“definitely lost: 0 bytes in 0 blocks.”表示没有内存泄漏。
# /usr/local/bin/valgrind --tool=memcheck --leak-check=full ls /
==29801== Memcheck, a memory error detector.
==29801== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==29801== Using LibVEX rev 1884, a library for dynamic binary translation.
==29801== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==29801== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==29801== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==29801== For more details, rerun with: -v
==29801==
bin etc lost+found mnt proc selinuxsys usr
boot home media net root smokeping tftpboot var
dev lib miscopt sbin srvtmp
==29801==
==29801== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 1)
==29801== malloc/free: in use at exit: 14,744 bytes in 32 blocks.
==29801== malloc/free: 162 allocs, 130 frees, 33,758 bytes allocated.
==29801== For counts of detected errors, rerun with: -v
==29801== searching for pointers to 32 not-freed blocks.
==29801== checked 139,012 bytes.
==29801==
==29801== LEAK SUMMARY:
==29801==definitely lost: 0 bytes in 0 blocks.
==29801== possibly lost: 0 bytes in 0 blocks.
==29801==still reachable: 14,744 bytes in 32 blocks.
==29801== suppressed: 0 bytes in 0 blocks.
==29801== Reachable blocks (those to which a pointer was found) are not shown.
==29801== To see them, rerun with: --leak-check=full --show-reachable=yes
----------------------------------------------------------------------------------------------------------------------------------------
# /usr/local/bin/valgrind --tool=memcheck --leak-check=full ps /
==29898== Memcheck, a memory error detector.
==29898== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==29898== Using LibVEX rev 1884, a library for dynamic binary translation.
==29898== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==29898== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==29898== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==29898== For more details, rerun with: -v
==29898==
ERROR: Garbage option.
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full--Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra fullX registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum-y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
==29898==
==29898== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 14 from 1)
==29898== malloc/free: in use at exit: 16 bytes in 2 blocks.
==29898== malloc/free: 20 allocs, 18 frees, 2,344 bytes allocated.
==29898== For counts of detected errors, rerun with: -v
==29898== searching for pointers to 2 not-freed blocks.
==29898== checked 263,972 bytes.
==29898==
==29898== 8 bytes in 1 blocks are definitely lost in loss record 2 of 2
==29898==at 0x4005A88: malloc (vg_replace_malloc.c:207)
==29898==by 0xBFF6DF: strdup (in /lib/libc-2.5.so)
==29898==by 0x804A464: (within /bin/ps)
==29898==by 0x804A802: (within /bin/ps)
==29898==by 0x804964D: (within /bin/ps)
==29898==by 0xBA5E8B: (below main) (in /lib/libc-2.5.so)
==29898==
==29898== LEAK SUMMARY:
==29898==definitely lost: 8 bytes in 1 blocks.
==29898== possibly lost: 0 bytes in 0 blocks.
==29898==still reachable: 8 bytes in 1 blocks.
==29898== suppressed: 0 bytes in 0 blocks.
==29898== Reachable blocks (those to which a pointer was found) are not shown.
==29898== To see them, rerun with: --leak-check=full --show-reachable=yes
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)