這一段看《unix環境高級編程》,一個關于線程的小例子。
#include
#include
#include
pthread_t?ntid;
void?printids(const?char?*s){
pid_t?pid;
pthread_t?tid;
pid=getpid();
tid=pthread_self();
printf("%s?pid?%u?tid?%u?(0x%x)n",s,(unsigned?int)pid,
(unsigned?int)tid,(unsigned?int)tid);
}
void?*thr_fn(void?*arg){
printids("new?thread:");
return?((void?*)0);
}
int?main(){
int?err;
err=pthread_create(&ntid,NULL,thr_fn,NULL);
if(err!=0)
fprintf(stderr,"can't?create?threadn");
printids("main?thread");
sleep(1);
exit(0);
}
根據線程模型,在同一進程環境下的線程有不同的線程ID,但所在進程ID相同。書上說linux是使用clone系統調用來實現pthread_create的,執行這段程序得到的主線程和新線程所在的進程號并不是匹配的。可我在機器上試了一下,進程號是相同的。這是其中一次運行結果:
new?thread:?pid?2568?tid?3076471728?(0xb75f3bb0)
main?thread?pid?2568?tid?3076476448?(0xb75f4e20)
系統是用的是redhat?enterprise?AS?3?update?5,內核V?2.4.21-32.ELsmp
莫非實現變了?。。。
|
getconf?GNU_LIBPTHREAD_VERSION
用上面的命令,看一下輸出,就知道你使用的線程庫是LinuxThread還是NPTL,后者同一進程的不同線路程的進程號一致。前者不一致