Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 816
user1251216 Avatar asked Dec 27 '22 04:12

user1251216


2 Answers

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.

like image 28
RRM Avatar answered Jan 11 '23 18:01

RRM


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.

like image 151
Basile Starynkevitch Avatar answered Jan 11 '23 18:01

Basile Starynkevitch