`
Michaelmatrix
  • 浏览: 209053 次
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

gettid 和pthread_self的区别

 
阅读更多

gettid 和pthread_self的区别

The man page for gettid says:


The thread ID returned by this call is not the same thing as a POSIX thread ID
(i.e., the opaque value returned by pthread_self(3)).

看来,线程的id,在linux中分为POSIX thread ID , 和内核中对每一个线程的id.

gettid是linux 的一个系统调用, 查看sys_gettid
/* Thread ID - the internal kernel "pid" */
asmlinkage long sys_gettid(void)
{
return task_pid_vnr(current);
}


struct upid {
/* Try to keep pid_chain in the same cacheline as nr for find_pid */
int nr;
struct pid_namespace *ns;
struct hlist_node pid_chain;
};

struct pid
{
atomic_t count;
/* lists of tasks that use this pid */
struct hlist_head tasks[PIDTYPE_MAX];
struct rcu_head rcu;
int level;
struct upid numbers[1];
};


这里面的level不清楚是什么含义? 有知道的告诉我,多谢了。




问:_syscall[0-6]跑哪里去了
答:没有了

问:那现在用啥呢?
答:用系统调用 syscall(2),第一个参数是SYS_name,后面参数照写






# define _GNU_SOURCE
# include < stdio. h>
# include < sys/ types. h>
# include < pthread. h>
# include < stdlib. h>
# include < string . h>
# include < unistd. h>
# include < linux/ unistd. h>
# include < sys/ syscall. h>

void * thsf( void * arg )
{
pthread_t th;
char * retstr;
retstr= malloc ( 50) ;
memset ( retstr, 0, 50) ;


strcpy ( retstr, "New thread quit!" ) ;


th= pthread_self( ) ;
# if 1
printf ( "New thread,pid:%d,tid:%x,another tid:%x./n" ,
getpid( ) ,
th,
( long ) syscall( SYS_gettid) ) ;
# endif

pthread_exit ( retstr) ;
}

int main( )
{
pthread_t nth1, mth, oth;
void * a;
if ( pthread_create ( & nth1, NULL , thsf, NULL ) )
printf ( "Error create new thread./n" ) ;
mth= pthread_self( ) ;

printf ( "Main thread,pid:%d,tid:%x,another tid:%x./n" , getpid( ) , mth, ( long ) syscall( SYS_gettid) ) ;
if ( ! pthread_join ( nth1, & a) )
{
printf ( "New thread quit,message from new thread:%s./n" , ( char * ) a) ;
free ( ( char * ) a) ;
}
return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics