wrong exit code with linux threads

Tomas Ulin tomas at mysql.com
Mon Jan 31 13:29:37 PST 2005


 
Hi,

the program below (tmp.c)  gives strange output regarding the child exit 
status when using linux threads on freebsd 4.7.

With lib pthread- correct:
Exit value 123
Exit value 123

With llthread - error:
Exit value 123
Exit value 0

Any ideas?

Thanks,

Tomas Ulin



#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
/*
ok - gcc -o tmp tmp.c -L/usr/local/lib -pthread ; ./tmp
not ok - gcc -o tmp tmp.c -L/usr/local/lib -llthread ; ./tmp
*/
void wait_and_display(pid_t pid)
{
  int status;
  while ( pid != waitpid(pid, &status, 0) );
  printf("Exit value %d\n",  WEXITSTATUS(status));
}
void *athread(void *data)
{
  pthread_exit(0);
}
int main(void)
{
  pid_t pid;
/* Case 1: Explicit call to exit */
  if ((pid = fork()) == 0) /* child */
    exit(123);
/* parent */
  wait_and_display(pid);
/* Case 2: Start thread and Explicit call to exit */
  if ((pid = fork()) == 0) /* child */
  {
    pthread_t thread;
    pthread_attr_t thread_attr;
    int result;
    pthread_attr_init(&thread_attr);
    result = pthread_create(&thread,
                          &thread_attr,
                          athread,
                          0);

    sleep(2);
    exit(123);
  }
/* parent */
  wait_and_display(pid);
  exit(0);
}



More information about the freebsd-threads mailing list