Re: A question about _thread_ of Linux

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

 




cheng long <kevinclcn@xxxxxxxxx> wrote:
Hi,

As we know, Linux implements thread as process, and a thread is merely
a process that shares certain resources with other processes.

But why the threads print the same process id?
Code like this:

void* print_pid(void* noused)
{
printf("%u\n",getpid());

return NULL;
}

int main()
{
printf("%u\n",getpid());

pthread_t thread_id;

int ret=pthread_create(&thread_id, NULL, &print_pid, NULL);
if(ret != 0 ){
return 1;
}

pthread_join(thread_id, NULL);

return 0;
}
On 5/8/06, pushparaj vitekar < v_pushparaj@xxxxxxxxx> wrote:
hi cheng

   threads belonging to same group have thread group id tgid stored in kernel stack of that process,The identifier shared by the threads is the PID of the thread group leader , that is, the PID of the first lightweight process in the group
This is done to comply with POSIX standards  that all threads of a multithreaded application must have the same PID.

regards
Pushparaj

Hi cheng

The tgid in task_struct have the same pid (the pid of the thread group leader) in them. Hence though all threads in a group have **different pid (as any other process) they have the same tgid to indicate they are threads**

And to comply with POSIX the ** the getpid() sys call would return the value of tgid instead of  pid when it is a thread**


"There are 10 people in the world - those who understand binary and those who dont !"

[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