4

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.

  • Can you register the thread_id somewhere via pthread_self(3) when a thread starts up? – thrig Nov 7 '17 at 15:48
  • The problem in most cases I am not able to modify the thread rotuines. For example in OpenMP. – M.K. aka Grisu Nov 8 '17 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.