How to get the pid of thread? - Embedded

This is a discussion on How to get the pid of thread? - Embedded ; 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....

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get the pid of thread?

  1. How to get the pid of thread?

    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.

  2. Re: How to get the pid of thread?

    On Mar 11, 12:11 pm, Allen wrote:
    > 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.


    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

+ Reply to Thread