6

How can I list all threads within the current process in FreeBSD? Or at least, get the number of threads running.

I found the Linux system call pstat_getproc which returns a struct containing pst_nlwps, the number of threads. I am looking for something similar to this on FreeBSD.

Or perhaps there is something like /dev/fd but for threads.

Anything I can use to get some kind of idea about how many other threads are running.

I want to be able to do this programmatically in C, not using an existing application.

user2868331
  • 333
  • 4
  • 10

1 Answers1

2

Use procstat(1), eg

# procstat -t $(pgrep openvpn)
  PID    TID COMM             TDNAME           CPU  PRI STATE   WCHAN    
  537 100051 openvpn          -                  0  120 sleep   select 

which depends on libprocstat(3).

xcah
  • 143
  • 3
  • Sorry, I should have clarified. I am not looking for a command line tool, I wish to do it programmatically in C. – user2868331 Aug 26 '15 at 20:43
  • 1
    @user2868331Look at /usr/src/usr.bin/procstat/procstat_threads.c. – xcah Aug 27 '15 at 13:03
  • ... and https://github.com/freebsd/freebsd-src/blob/main/usr.bin/procstat/procstat_basic.c as called from procstat.c as that has a simple field to access with the thread count rather than counting TIDs. – gps Jan 23 '22 at 21:53