Re: A question about _thread_ of Linux

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,
On 5/10/06, Manish Regmi <regmi.manish@xxxxxxxxx> wrote:> On 5/9/06, cheng long <kevinclcn@xxxxxxxxx> wrote:> > > Why do you think so?> > > pid is allocated by pid types. ie. pid is allocated differently for> > > process (i mean those created with CLONE_THREAD) and threads. so they> > > are so different.> >> > #include <stdio.h>> > #include <pthread.h>> > int main()> > {> >     printf("main pid: %u\n", getpid());> >     printf("main thread id: %u\n", pthread_self());> >     return 0;> > }> >> > As we know, the tid is the first thread's pid, but the above progrom> > prints two different number, how's that?> >>> In this case tgid and pid must be same. it seems that pthread_self()> cannot be used without pthread_create. Don't know for sure.>

pthread_self() will return the id of the thread, i,e. , the tid. Thisis unique for each thread.On the other hand, getpid() (get PROCESS id) wil return the pid of thecurrent _PROCESS_ , i.e., this is the tgid which will be same for allthe threads invoked from within that process.
To summarize:tgid <==> getpid() <==> pid of the process which invokes (callpthread_create) the threads and is common to all threads invoked bythe process.tid <==> pthread_self() <==> pid of the particular thread, and isunique to each process....
And as Manish pointed out, if you call pthread_self() outside the"thread context", it gives some different id.

Try this for some clarity :
#include <stdio.h>#include <pthread.h>void * func (void * argc){    int i = (int)argc;    printf ("In the thread %d : %u\n",i,pthread_self());    printf ("In the thread %d : %u\n",i,gettid());}int main(){   pthread_t th[2];   int i,ret;   printf("main pid: %u\n", getpid());   for (i = 0; i < 3; i++)   ret = pthread_create (&th[i], NULL, func, (void *)i);   pthread_join(th[0], NULL);   return 0;}--Raseel.��������!���W��v������ޗ��{��f������ޖw�n'�������Y�����


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux