【问题标题】:android binder driver implementationandroid binder驱动实现
【发布时间】:2018-04-13 09:06:41
【问题描述】:

我正在学习活页夹驱动程序。它有一个结构binder_thread 用于保存线程信息。我发现它使用分配有current->pidbinder_thread.pid 来区分彼此。例如binder_get_thread方法中,该字段用于判断当前线程是否已添加到树中。

static struct binder_thread *binder_get_thread(struct binder_proc *proc)
{
    struct binder_thread *thread = NULL;
    struct rb_node *parent = NULL;
    struct rb_node **p = &proc->threads.rb_node;
    while (*p) {
        parent = *p;
        thread = rb_entry(parent, struct binder_thread, rb_node);
        if (current->pid < thread->pid)
            p = &(*p)->rb_left;
        else if (current->pid > thread->pid)
            p = &(*p)->rb_right;
        else
            break;
    }
    .....
}

但是据我所知,current-&gt;pid是当前进程id,怎么可以用来区分线程呢?

【问题讨论】:

    标签: android linux-device-driver android-binder


    【解决方案1】:

    这是一个有点混乱的术语,但在内核级别,每个线程都被分配了自己的 pid 值,它唯一地标识它。当一个进程启动时,它的第一个线程被赋予一个唯一的pid,并且线程组ID被设置为相同的pid。创建新线程时,它们会被赋予唯一的pid 值,但线程组 ID 相同,因此它们可以链接回其父级。因此,您正在查看的这个 rb 树搜索代码将起作用,因为 current-&gt;pid 是当前正在执行的线程 ID,它正在与树中线程条目的 thread-&gt;pid 值进行比较。

    如果您有兴趣,paxdiablo 在这个 SO 问题中的回答会更详细地解释线程/进程 ID: If threads share the same PID, how can they be identified?

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2015-08-06
      • 1970-01-01
      相关资源
      最近更新 更多