how to debug thread cpu 100%

本文介绍了一种简单的方法来调试导致CPU使用率达到100%的程序问题。通过一个示例程序,演示了如何使用top工具定位问题线程,使用pstack查看堆栈信息,以及如何生成和利用core dump文件进行更深入的问题定位。

when we write a program, cpu and memory usages are very important to indicate the stability of the program. Once the cpu usage reached 90%, there are some bugs in your program, and you must find the problem. Here is a simply guide to debug with cpu 100%. For example:

void *first_routine (void * args)
{
    while(1)
    {
        int a = 1;
        usleep(1);
    }

    return NULL;
}

int main()
{
    pthread_t thread_first, thread_second;
    pthread_create(&thread_first, NULL, &first_routine, NULL);

    while(1)
    {
        sleep(4);
    }
    return 0;
}

then complile it : gcc debug.c -o debug -lpthread , and run it .  

iii) use top tool find the bug thread,  top -p $pid -H

iv) use pstack : pstack $tid(bug thread), to look the stack of the bug thread. but pstack can't see the paraments. if you still can't determine the bug line, user gcore to get the program core.  (gcore $pid)

v)  gdb -c  $core.file  ./exe , and the use  command  bt(back trace), then use the paraments to determine the bug code.

 

转载于:https://www.cnblogs.com/memoryh/p/4104590.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值