How to get the pid of thread?

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I want to get the pid of the thread created by pthread_create, so that
I can know which thread is my working thread in the outputs of ps
command.

Re: How to get the pid of thread?
Quoted text here. Click to load it

U are referring to kernel right?

It is in fs/proc/base.c:   proc_task_read() function (which is called
when u try to read the directory /proc/xxxx/task):

Following function is shown:

        /* f_version caches the tgid value that the last readdir call
couldn't
         * return. lseek aka telldir automagically resets f_version to
0.
         */
        ns = filp->f_dentry->d_sb->s_fs_info;
        tid = (int)filp->f_version;
        filp->f_version = 0;
        for (task = first_tid(leader, tid, pos - 2, ns);
             task;
             task = next_tid(task), pos++) {
                tid = task_pid_nr_ns(task, ns);
                if (proc_task_fill_cache(filp, dirent, filldir, task,
tid) < 0) {
                        /* returning this tgid failed, save it as the
first
                         * pid for the next readir call */
                        filp->f_version = (u64)tid;
                        put_task_struct(task);
                        break;
                }
        }
out:


The above for-loop will print out all the thread ID of the particular
process, eg:

ls /proc/3407/task/
3407  3413  3427  3435  3436  4303  4304  4798

Site Timeline