Linux, creating thread fails

sollala
sollala used Ask the Experts™
on
My following program works perfect on solaris, but failing on linux. I have red hat7.3. Any one seen such behavior?

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>

static int childpid = 0;

static void* theThread(void* lpParameter)
{
   int pid = fork();
   switch (pid)
   {
       case 0:
           printf("Child here... (pid = %d)\n", getpid());
           exit(99);
           break;
       case -1:
           fprintf(stderr, "Failed to fork, errno = %d\n", errno);
           exit(1);
       default:
           childpid = pid;
           break;
   }
   return 0;
}

static void sigchldHandler(int)
{
   printf("Got a signal!!!\n");
}


int main()
{
   signal(SIGCHLD, sigchldHandler);
   printf("Main process pid = %d\n", getpid());

   pthread_t t;
   pthread_create(&t, NULL, theThread, NULL);
   pthread_join(t, NULL);

   int status;
   printf("Parent here... (pid = %d, child pid = %d)\n",
          getpid(), childpid);
   if (waitpid(childpid, &status, 0) < 0)
       fprintf(stderr, "Failed to waitpid, errno = %d\n", errno);
   else if (WIFEXITED(status))
       printf("Child exited with status = %d\n",
              WEXITSTATUS(status));
   return NULL;

   return 0;
}


Error mesg :
Main process pid = 2370
Child here... (pid = 2373)
Got a signal!!!, pid = 2372
Parent here... (pid = 2370, child pid = 2373)
Failed to waitpid, errno = 10
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
From the manpage for waitpid:
 In the Linux kernel, a kernel-scheduled thread  is  not  a
       distinct  construct  from  a process. Instead, a thread is
       simply a process that is created  using  the  Linux-unique
       clone(2)  system call; other routines such as the portable
       pthread_create(3) call  are  implemented  using  clone(2).
       Thus,  if  two threads A and B are siblings, then thread A
       cannot wait on any processes forked by  thread  B  or  its
       descendents,  because an uncle cannot wait on his nephews.
       In some other Unix-like systems,  where  multiple  threads
       are implemented as belonging to a single process, thread A
       can wait on any processes forked by sibling thread B;  you
       will  have  to rewrite any code that makes this assumption
       for it to work on Linux.


----
sollala:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ / No refund
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheWeakestLink
EE Cleanup Volunteer
PAQed per request/recommendation - No Refund

YensidMod
Community Support Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial