Sample Header Ad - 728x90

Portable way to get all thread ids running inside a process.

6 votes
1 answer
3979 views
During the porting of an application form Linux to FreeBSD I came up with the following problem. I need to get all thread id of all threads running inside my application. In terms of PThreads, I need an instance of an pthread_t array which contain all threads in my program (either PThreads or OpenMP) to send a signal using pthread_signal to them. The current Linux implementation uses a non portable workaround by traversing the procfs to obtain all pids of a process: int foreach_pid(void (*func)(pid_t, void *aux),void*aux){ DIR *proc_dir; char dirname; pid_t pid; if ( ! func ) return -1; snprintf(dirname, sizeof(dirname), "/proc/%d/task", getpid()); proc_dir = opendir(dirname); if (proc_dir) { /* /proc available, iterate through tasks... */ struct dirent *entry; while ((entry = readdir(proc_dir)) != NULL) { if(entry->d_name == '.') continue; pid = atoi(entry->d_name); func(pid, aux); } closedir(proc_dir); return 0; } else { return -1; } } and uses the kill function to send the signals to all threads via their process ids. Obviously, this is not a portable to solution because even if linprocfs is mounted under FreeBSD it does not provide the *task* directory. So what I am searching for is a routine/an interface/a library/a syscall to have a portable way to obtain similar information. Either as pid_t or preferable as pthread_t.
Asked by M.K. aka Grisu (191 rep)
Nov 7, 2017, 10:31 AM
Last activity: Jul 5, 2023, 09:02 AM