user1251216
user1251216

Reputation: 97

How to check the state of Linux threads?

How could I check the state of a Linux threads using codes, not tools? I want to know if a thread is running, blocked on a lock, or asleep for some other reason. I know the Linux tool "top" could do this work. But how to implement it in my own codes. Thanks.

Upvotes: 4

Views: 24157

Answers (3)

Umut
Umut

Reputation: 2483

You could also find it with by looking at the cgroup hierarchy of the service that your process belongs. Cgroups have a file called "tasks" and this file lists all the tasks of a service.

For example:

cat /sys/fs/cgroup/systemd/system.slice/hello.service/tasks

Note: cgroup should be enabled in your linux kernel.

Upvotes: 0

RRM
RRM

Reputation: 2679

Lets say your process id is 100.

Go to /proc/100/task directory and there you could see multiple directories representing each threads.

then inside each subdirectory e.g. /proc/100/task/10100 there is a file named status.

the 2nd line inside this file is the state information of the thread.

Upvotes: 3

I think you should study in details the /proc file system, also documented here, inside kernel source tree.

It is the way the Linux kernel tells things to outside!

There is a libproc also (used by ps and top, which reads /proc/ pseudo-files).

See this question, related to yours.

Reading files under /proc/ don't do any disk I/O (because /proc/ is a pseudo file system), so goes fast.

Upvotes: 8

Related Questions