怎么查看嵌入式Linux开发中各个线程的运行情况

嵌入式技术

1343人已加入

描述

在嵌入式Linux开发中,有时候为了定位问题,需要查看某个进程的各个线程的运行情况。

例子

multi_thread.c:

左右滑动查看全部代码>>>

#define _GNU_SOURCE
#include 
#include 
#include 
#include 

// 线程名称最大长度
#define APP_THREAD_NAME_MAX_LEN     32

// 线程索引
typedef enum _app_thread_index
{
    APP_THREAD_INDEX_TEST0,
    APP_THREAD_INDEX_TEST1,
    APP_THREAD_INDEX_TEST2,
    APP_THREAD_INDEX_TEST3,
    APP_THREAD_INDEX_TEST4,
    APP_THREAD_INDEX_TEST5,
    APP_THREAD_INDEX_MAX
}app_thread_index_e;

// 线程入口函数指针类型
typedef void *(*p_thread_fun)(void *param);

// 线程数据表
typedef struct _app_thread
{
    pthread_t thread_handle;
    p_thread_fun thread_entry;
    char name[APP_THREAD_NAME_MAX_LEN];
}app_thread_s;

static void *test0_thread_entry(void *param);
static void *test1_thread_entry(void *param);
static void *test2_thread_entry(void *param);
static void *test3_thread_entry(void *param);
static void *test4_thread_entry(void *param);
static void *test5_thread_entry(void *param);

// 线程表
app_thread_s s_app_thread_table[APP_THREAD_INDEX_MAX] =
{
    {0, test0_thread_entry, "test0_thread"},
    {0, test1_thread_entry, "test1_thread"},
    {0, test2_thread_entry, "test2_thread"},
    {0, test3_thread_entry, "test3_thread"},
    {0, test4_thread_entry, "test4_thread"},
    {0, test5_thread_entry, "test5_thread"}
};

static void *test0_thread_entry(void *param)
{
    printf("test0_thread running...
");

    while (1)
    {
        usleep(2 * 1000);
    }
    
    return NULL;
}

static void *test1_thread_entry(void *param)
{
    printf("test1_thread running...
");

    while (1)
    {
        usleep(2 * 1000);
    }
    
    return NULL;
}

static void *test2_thread_entry(void *param)
{
    printf("test2_thread running...
");

    while (1)
    {
        usleep(2 * 1000);
    }
    
    return NULL;
}

static void *test3_thread_entry(void *param)
{
    printf("test3_thread running...
");

    while (1)
    {
        usleep(2 * 1000);
    }
    
    return NULL;
}

static void *test4_thread_entry(void *param)
{
    printf("test4_thread running...
");

    while (1)
    {
        usleep(2 * 1000);
    }
    
    return NULL;
}

static void *test5_thread_entry(void *param)
{
    printf("test5_thread running...
");

    while (1)
    {
        usleep(2 * 1000);
    }
    
    return NULL;
};

static int create_all_app_thread(void)
{
    int ret = 0;

    for (int i = 0; i < APP_THREAD_INDEX_MAX; i++)
    {
        ret = pthread_create(&s_app_thread_table[i].thread_handle, NULL, s_app_thread_table[i].thread_entry, NULL);

        if (0 != ret)
        {
            printf("%s thread create error! thread_id = %ld
", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);
            return ret;
        }
        else
        {
            printf("%s thread create success! thread_id = %ld
", s_app_thread_table[i].name, s_app_thread_table[i].thread_handle);
            pthread_setname_np(s_app_thread_table[i].thread_handle, s_app_thread_table[i].name);
        }

        pthread_detach(s_app_thread_table[i].thread_handle);
    }

    return ret;
}

int main(int argc, char **argv)
{ 
    create_all_app_thread();
    
    while (1)
    {
        usleep(2 * 1000);
    }

    return 0;
}

我们可以通过top命令来查看,具体做法就是要在top输出中开启线程查看,请调用top命令的“-H”选项,该选项会列出所有Linux线程。

这里我们指定查看multi_thread进程的各线程运行情况,命令:

top -H -p `pidof multi_thread`

注意:这里的 `号并不是单引号!

这个符号在键盘上感叹号!键的左边。

我们先运行程序,再使用top命令查看,比如:

嵌入式

注意:我们创建线程的时候需要使用 pthread_setname_np 函数设置线程的名字,否则top -H显示不出来具体的线程。

假如我们把上例中的pthread_setname_np屏蔽掉,结果如下:

嵌入式

可见,不调用pthread_setname_np设置线程名称的话,top -H查看得到的各线程名称就是进程名。

以上就是本次的分享,期待大家的三连支持!

  审核编辑:汤梓红
 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分