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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With