I am trying to get the thread IDs (tid
) that I can use to access /proc/[tid]/sched
. I can look it up in the PID column of htop
but when I try to access it from inside python I keep getting -1
.
#!/usr/bin/env python3
import ctypes
import threading
def get_tid():
libc = ctypes.cdll.LoadLibrary('libc.so.6')
print(libc.syscall(224))
threading.Thread(target=get_tid).run()
It's syscall 186 for me on Ubuntu 18.04
import ctypes
def get_tid():
"""System call gettid on Linux, returning thread-id."""
return ctypes.CDLL('libc.so.6').syscall(186)
Python 3.8 introduced threading.get_native_id()
Return the native integral Thread ID of the current thread assigned by the kernel. This is a non-negative integer. Its value may be used to uniquely identify this particular thread system-wide (until the thread terminates, after which the value may be recycled by the OS).
Availability: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX.