posix thread
I've got problem with posix thread on RH7.2 and MDK 8.1,and the problem is:
When I create new thread with pthread_create(... ) my parent process is forked twice.
When I recompile this program on Solaris2.7 it works.
This is my program :
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int myglobal;
void *thread_function(void *arg) {
int i,j;
for ( i=0; i<20; i++ ) {
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}
int main(void) {
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
printf("error creating thread.%d ",errno);
abort();
}
for ( i=0; i<20; i++) {
myglobal=myglobal+1;
printf("o");
fflush(stdout);
sleep(1);
}
if ( pthread_join ( mythread, NULL ) ) {
printf("error joining thread.");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
exit(0);
}
And I compile it with
gcc -o test test.cc -lpthread
When I run it:
./test&
and check it with ps -fe | grep test
I see three processes with name test.
|