6

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[256];
 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[0] == '.')
            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.

M.K. aka Grisu
  • 191
  • 1
  • 5

1 Answers1

1

Obtaining the list of threads on FreeBSD is done with sysctl(3). The OID is CTL_KERN, KERN_PROC, KERN_PROC_ALL.

JdeBP
  • 66,967
  • 12
  • 159
  • 343