On 5/10/06, cheng long <kevinclcn@xxxxxxxxx> wrote:
1) Why do you think there is no thread group leader here ? I think the main thread is the thread group leader. The calls to getpid() in main thread returns its own "task_struct.pid" where as getpid() in each of the call returns the "task_struct.tgid", which you see is the same for all.
2) As others have pointed out, pthread_self() seems to correctly return its thread id , only for threads created by POSIX library. So it dosent work for your main().Just add these printf() statements in the above code
main() {
..........
.........
printf("main pid: %u\n", pthread_self());
for (i = 0; i < 3; i++) {
ret = pthread_create (&th[i], NULL, func, (void *)i);
printf("th[%d]: %u\n", i,th[i]);
...............
.................
}
and you see a output like this, where the pthread_self() correctly returns the id in each calling thread created by pthread_create, whereas main() does not:
main pid: 31577
main pid: 1073939072 /* pthread_self() returns wrong value for main */
In the thread 0 : 1082330304
In the thread 0 : 31577
th[0]: 1082330304 /* the thread id's assigned by POSIX library*/
In the thread 1 : 1090718784
In the thread 1 : 31577
th[1]: 1090718784
In the thread 2 : 1099107264
In the thread 2 : 31577
th[2]: 1099107264
my results is:
main pid: 2470
In the thread 0:tid 3086744480
In the thread 0:pid 2470
In the thread 1:tid 3076254624
In the thread 1:pid 2470
Yes, getpid() does return the tgid. but I don't think pthread_self() will
return the pid of the lighweight process. Because "Understanding the
Linux Kernel" said that the thread group leader's pid equals to the tid,
we can't find such a thread(lightweight process) in above results.
1) Why do you think there is no thread group leader here ? I think the main thread is the thread group leader. The calls to getpid() in main thread returns its own "task_struct.pid" where as getpid() in each of the call returns the "task_struct.tgid", which you see is the same for all.
2) As others have pointed out, pthread_self() seems to correctly return its thread id , only for threads created by POSIX library. So it dosent work for your main().Just add these printf() statements in the above code
main() {
..........
.........
printf("main pid: %u\n", pthread_self());
for (i = 0; i < 3; i++) {
ret = pthread_create (&th[i], NULL, func, (void *)i);
printf("th[%d]: %u\n", i,th[i]);
...............
.................
}
and you see a output like this, where the pthread_self() correctly returns the id in each calling thread created by pthread_create, whereas main() does not:
main pid: 31577
main pid: 1073939072 /* pthread_self() returns wrong value for main */
In the thread 0 : 1082330304
In the thread 0 : 31577
th[0]: 1082330304 /* the thread id's assigned by POSIX library*/
In the thread 1 : 1090718784
In the thread 1 : 31577
th[1]: 1090718784
In the thread 2 : 1099107264
In the thread 2 : 31577
th[2]: 1099107264
--
"There are 10 people in the world - those who understand binary and those who dont !"