2

I am creating multiple threads in my python program with thread name. Is there any way I can identify a specific task from /proc//task/*. I can see /proc/17094/task/17095/comm but that prints my program name only not the thread name

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self, name=name)
        self.threadID = threadID
        self.name = name
        self.counter = counter

    def run(self):
        print "Starting " + self.name
        # Get lock to synchronize threads
        print_time(self.name, self.counter, 9)
        # Free lock to release next thread

    def print_time(threadName, delay, counter):
        while counter:
            time.sleep(delay+9)
            print "%s: %s" % (threadName, time.ctime(time.time()))
            threadLock.acquire()
            counter -= 1
            threadLock.release()

threadLock = threading.Lock()
threads = []

# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# Start new Threads
thread1.start()
thread2.start()

# Add threads to thread list
threads.append(thread1)
threads.append(thread2)

print "Started both threads"
# Wait for all threads to complete
for t in threads:
    t.join()
print "Exiting Main Thread"

Here I want to see the thread based on name - Thread-1 -

I can see the tasks under /proc/

~ # ps -ef | grep thread1
root     17787  4859  0 00:24 pts/0    00:00:00 /bin/python ./thread1.py
root     17800  4938  0 00:24 pts/1    00:00:00 grep thread1
~ # ls -l /proc/17787/task/
total 0
dr-xr-xr-x 7 root root 0 Jun 15 00:24 17787
dr-xr-xr-x 7 root root 0 Jun 15 00:24 17788
dr-xr-xr-x 7 root root 0 Jun 15 00:24 17789

But there I couldn't see Thread-1 in any files under these directories.

martineau
  • 119,623
  • 25
  • 170
  • 301
user2677279
  • 127
  • 3
  • 10

1 Answers1

4

First, name your threads. https://linux.die.net/man/3/pthread_setname_np

So, after ps -ef | grep thread1 you discovered the pid was 17787

Run:

ps -T -p 17787

and this will display the threads of the process.

For more info, man ps

As requested, get thread info by name with:

cat /proc/pid/task/tid/comm

where pid is the process id and tid is the thread id (wildcard * for all)

Attersson
  • 4,755
  • 1
  • 15
  • 29
  • thanks for the reply. But my requirement is to see the details of the threads using thread name - I think if we create thread with thread name, name should come in the tasks directory under /proc/ directory – user2677279 Jun 15 '18 at 03:06
  • 1
    cat /proc/11896/task/*/comm - gives the thread names for trafficserver that is running in my linux server – user2677279 Jun 15 '18 at 07:02
  • cat /proc/pid/task/tid/comm gives only program name - not thread name. As I mentioned in the post, I am expecting 'Thread-1' and 'Thread-2'. – user2677279 Jun 15 '18 at 16:58
  • Yes you put the wildcard to get them all as you found out, I only wrote the syntax – Attersson Jun 15 '18 at 18:11
  • `cat /proc/pid/task/tid/comm` will show only 'python' if I execute my program as `python myprogram.py`. but I want to see it as 'Thread-1', as that is the name I have specified for the thread. – user2677279 Jun 16 '18 at 00:20
  • Ahh you have to name them https://linux.die.net/man/3/pthread_setname_np – Attersson Jun 16 '18 at 09:22
  • Thanks - but I think it is not implemented yet in python – user2677279 Jun 18 '18 at 02:51
  • 1
    Not sure what you mean "not implemented in python", everything is implemented in python https://stackoverflow.com/questions/34361035/python-thread-name-doesnt-show-up-on-ps-or-htop – e271p314 May 31 '19 at 14:15
  • @e271p314 Python does not set the name via `prctl`. It's not implemented. – Błażej Michalik Jul 14 '22 at 16:13