This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
getpid() returns different numbers from different threads
- To: libc-alpha at sources dot redhat dot com
- Subject: getpid() returns different numbers from different threads
- From: Craig Rodrigues <rodrigc at mediaone dot net>
- Date: Fri, 6 Oct 2000 18:12:32 -0400
Hi,
I am running glibc 2.1.92, with Linux 2.2.14 under x86.
I have seen this phenomenon under glibc 2.1.3 as well.
The following code example under Linux will return different
values if getpid() is called from separate threads.
I tried the same example under Solaris and Digital Unix,
and both returned a single number in both threads.
What should the correct behavior be according to POSIX?
I know that in Linux, threads are implemented as processes
with the clone() command, so distinguishing between a thread
and a process is a bit confusing.
Thanks.
--
Craig Rodrigues
http://www.gis.net/~craigr
rodrigc@mediaone.net
#include "stdlib.h"
#include "pthread.h"
void print_message_function (void *ptr);
int main (int argc, char **argv)
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int status;
pthread_create (&thread1, NULL,
(void *) &print_message_function, (void *) message1);
pthread_create (&thread2, NULL,
(void *) &print_message_function, (void *) message2);
pthread_join(thread1, (void **)&status);
pthread_join(thread2, (void **)&status);
exit (0);
}
void print_message_function(void *ptr)
{
int i;
for(i=0; i < 20; i++)
printf("%s: pid: %d\n", ptr, getpid());
}