Re: linux线程与内核轻量级进程的映射关系

152 views
Skip to first unread message

Shuo Chen

unread,
May 11, 2013, 9:58:24 AM5/11/13
to pon...@googlegroups.com
identity. 用 gettid(2) 拿到的就是你说的那两个“号”。

On Friday, May 10, 2013 10:50:41 PM UTC-7, Chen Jie wrote:
在linux内核中,线程基本等同于进程,每个线程都对应与内核中的一个轻量级进程,那么他们之间是怎样的一个映射关系,即通过线程号怎么计算得到内核轻量级进程号,映射函数是什么?

请教之,谢谢

Auxten wpc

unread,
May 20, 2013, 3:19:34 AM5/20/13
to pon...@googlegroups.com
Glibc does not provide a wrapper for this system call; call it using syscall(2).
try this:
static inline pid_t gko_gettid(void)
{
#if defined (__APPLE__)
    return getpid();
#elif defined(__linux__)
    return syscall(SYS_gettid);
#endif
}



--
 
---
您收到此邮件是因为您订阅了 Google 网上论坛的“TopLanguage”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 pongba+un...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 



--
----------------------------------
Auxten *StarDust* blog.optool.net

Cong Wang

unread,
Jun 4, 2013, 10:36:49 PM6/4/13
to pon...@googlegroups.com
2013/5/11 Shuo Chen <gian...@gmail.com>:
> identity. 用 gettid(2) 拿到的就是你说的那两个“号”。
>

你确定? getpid(2)拿到的是进程号,在Linux上其实是
thread group identifier (TGID),gettid(2) 拿到的是线程号,TID。
单独一个拿不到两个号的。

clone(2)里面明确解释到:

Thread groups were a feature added in Linux 2.4 to
support the POSIX threads notion of a set of threads
that share a single PID. Internally, this shared PID is
the so-called thread group identifier (TGID) for the thread
group. Since Linux 2.4, calls to getpid(2) return the TGID
of the caller.

The threads within a group can be distinguished by their
(system-wide) unique thread IDs (TID). A new thread's
TID is available as the function result returned to the caller
of clone(), and a thread can obtain its own TID using gettid(2).

至少在用户空间,它们是没有明确的对应关系的,所以你只能调用
它们两个来获取这两个ID。

Shuo Chen

unread,
Jun 5, 2013, 4:41:43 PM6/5/13
to pon...@googlegroups.com
你读原帖了吗?
Reply all
Reply to author
Forward
0 new messages