Subject: Problems with threads in Linux emulation
To: None <tech-kern@netbsd.org>
From: Matthias Scheler <tron@zhadum.de>
List: tech-kern
Date: 09/09/2000 22:57:35
	Hello,

in an attempt to find a fix for the problems in our Linux emulation with
programs using the Linux Posix Thread Implementation I wrote the
following small test program:

---------------------------------------------------------------------------

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void *Tescht(void *Gugu)

{
 (void)sleep(1);
 (void)printf("Thread %ld has pid %d\n",(long)Gugu,getpid());
 return NULL;
}

int main(int argc,char **argv)

{
 long NumThreads,Index;
 pthread_t *LWP;

 if (argc!=2)
  {
   (void)fprintf(stderr,"Usage: %s numerofthreads\n",argv[0]);
   return EXIT_FAILURE;
  }
 NumThreads=strtol(argv[1],NULL,10);

 if ((LWP=calloc(NumThreads,sizeof(pthread_t)))==NULL)
  {
   perror("calloc");
   return EXIT_FAILURE;
  }
  
 (void)printf("main process has pid %d\n",getpid());
 for (Index=0; Index<NumThreads; Index++)
  if (pthread_create(&LWP[Index],NULL,Tescht,(void *)Index))
   {
    perror("pthread_create");
    return EXIT_FAILURE;
   }

 for (Index=0; Index<NumThreads; Index++) (void)pthread_join(LWP[Index],NULL);
 (void)puts("main process finished");

 return EXIT_SUCCESS;
}

---------------------------------------------------------------------------

As you can see it only creates a number of threads which print out a
message and terminate afterwards. If you invoke that program under
Linux with an arbitrary number of threads it works fine. Under NetBSD
it works fine if only one thread is created. But if more than one thread
is created the main program hangs after printing "main process finished".
While it is hanging two processes are left:

tron@lyssa:~/install/tescht>ps -guwx | grep habba
tron 1481  0.0  0.0     0     0 p1 Z          -  0:00.00 (habba)
tron 1480  0.0  0.1    32   584 p1 S    12:55AM  0:00.00 ./habba 2 

Any ideas what is going on here?

	Kind regards

-- 
Matthias Scheler                            http://www.sighardstrasse.de/~tron/