在Linux上的C语言中,如何获取进程的所有线程? [英] On Linux, in C, how can I get all threads of a process?

查看:1394
本文介绍了在Linux上的C语言中,如何获取进程的所有线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历当前进程所有线程的所有消息?有什么方法不涉及进入/proc吗?

How to iterate through all tids of all threads of the current process? Is there some way that doesn't involve diving into /proc?

推荐答案

基于阅读/proc

#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>

然后,从功能内部:

    DIR *proc_dir;
    {
        char dirname[100];
        snprintf(dirname, sizeof dirname, "/proc/%d/task", getpid());
        proc_dir = opendir(dirname);
    }

    if (proc_dir)
    {
        /* /proc available, iterate through tasks... */
        struct dirent *entry;
        while ((entry = readdir(proc_dir)) != NULL)
        {
            if(entry->d_name[0] == '.')
                continue;

            int tid = atoi(entry->d_name);

            /* ... (do stuff with tid) ... */
        }

        closedir(proc_dir);
    }
    else
    {
        /* /proc not available, act accordingly */
    }

这篇关于在Linux上的C语言中,如何获取进程的所有线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆