Aman Jain
Aman Jain

Reputation: 11317

How to print thread id of all threads a process on linux has

I know how to print thread id while executing in the context of a thread, but I would like to print all thread ids that a process has spawned. I need this to correlate with strace output for debugging.

How to get current thread id:
pid_t x = syscall(__NR_gettid);

Upvotes: 1

Views: 2427

Answers (2)

eerorika
eerorika

Reputation: 238461

You can read the virtual /proc filesystem. Iterate over the dirnames in /proc/self/task.

if(DIR* dir = opendir("/proc/self/task")) {
    while (dirent* entry = readdir(dir))
        if (entry->d_name[0] != '.')
            std::cout << entry->d_name;
    closedir(dir);
}

Upvotes: 1

OpenUserX03
OpenUserX03

Reputation: 1458

From https://unix.stackexchange.com/a/901/134332

For each process, a lot of information is available in /proc/12345 where 12345 is the process ID. Information on each thread is available in /proc/12345/task/67890 where 67890 is the kernel thread ID. This is where ps, top and other tools get their information.

Upvotes: 1

Related Questions