linux ps pcpu,使用gdb和ps定位程序cpu占用问题_linux 应用_黑光技术

本文分析了一段导致CPU占用率异常升高的代码片段,该问题源于一个无限循环中对waitpid函数的不当使用。通过详细解释waitpid和sigwait的作用及正确用法,指出在子进程结束后未能正确退出循环的问题所在。

ps -m -o tid,cpu,time -p 6321

TID CPU     TIME

-   - 00:30:50

6321   - 00:00:00

6322   - 00:30:50

6323   - 00:00:00

6324   - 00:00:00

6325   - 00:00:00

6326   - 00:00:00

6327   - 00:00:00

6328   - 00:00:00

在用gdb attach到进程上去看这个具体的线程信息

gdb -p  6321

info thread

8 Thread 0x7fbfd14cf700 (LWP 6322)  0x00007fbfd221303d in waitpid () from /lib64/libpthread.so.0

7 Thread 0x7fbfd0cce700 (LWP 6323)  0x00007fbfd220f75b in pthread_cond_timedwait@@GLIBC_2.3.2 ()

from /lib64/libpthread.so.0

6 Thread 0x7fbfcbfff700 (LWP 6324)  0x00007fbfd15b62c3 in epoll_wait () from /lib64/libc.so.6

5 Thread 0x7fbfd2a45700 (LWP 6325)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

4 Thread 0x7fbfd04cd700 (LWP 6326)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

3 Thread 0x7fbfd03cc700 (LWP 6327)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

2 Thread 0x7fbfd02cb700 (LWP 6328)  0x00007fbfd220f3dc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0

* 1 Thread 0x7fbfd2a47720 (LWP 6321)  0x00007fbfd15b62c3 in epoll_wait () from /lib64/libc.so.6

(gdb) thread 8

[Switching to thread 8 (Thread 0x7fbfd14cf700 (LWP 6322))]#0  0x00007fbfd221303d in waitpid ()

from /lib64/libpthread.so.0

(gdb) bt

#0  0x00007fbfd221303d in waitpid () from /lib64/libpthread.so.0

#1  0x00000000005ac003 in idata::factory::processor::task_executor::CTaskMonitor::signal_thread (this=0x882eb1)

at ./task_monitor.cpp:46

经过查看是一个死循环对waitpid进行不断调用,致使cpu奇高。

程序中是由一个线程去wait子进程的结束,先是使用sigwait去等待子进程结束,等有子进程结束的信号发生后,在使用waitpid获取子进程的id和返回码,进行记录。在使用waitpid的时候做了一个循环把所有子进程的信息都取出来。这里这个循环没有再取没有数据之后跳出导致了重复的获取。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值