代码之家  ›  专栏  ›  技术社区  ›  chew socks

python获取与os/proc/[pid]匹配的tid/

  •  1
  • chew socks  · 技术社区  · 7 年前

    我正在尝试获取线程ID( tid )我可以用来访问 /proc/[tid]/sched . 我可以在pid列中查找 htop 但是当我试图从python内部访问它时,我总是 -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()
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Darkonaut    7 年前

    我在Ubuntu 18.04的系统调用186

    import ctypes
    import threading
    
    def get_tid():
        """System call gettid on Linux, returning thread-id."""
        print(ctypes.CDLL('libc.so.6').syscall(186))
    
    threading.Thread(target=get_tid).run()