hang when using pthread and fork in 1.5.23-1 and snapshot 20070118, and now 1.5.24-1.

Brian Ford Brian.Ford@FlightSafety.com
Wed Jan 31 15:06:00 GMT 2007


On Fri, 26 Jan 2007, Peter Rehley wrote:

> Hello,
>
> I tried the latest release of cygwin1.dll (1.5.24-1) and it still is
> hanging in the same way.  I've tried to debug further with gdb, but
> so far I haven't got any useful information out of gdb.
>
> I'll keep trying to get some debug information, but if any one else
> can reproduce the problem I would be most appreciative.

I can reproduce a problem.  Your descriptions of it are a bit hard to
follow, so I'm not sure if it is your problem or not.  Unfortunately, I
don't have time to debug it right now.  I do have a few comments, though.

Why are you creating a thread just to fork/exec another process?

Pedantically, I believe you are supposed to call _exit, not exit, if fork
fails as stated here in the Solaris man page for fork:

     An applications should call _exit() rather than exit(3C)  if
     it  cannot execve(), since exit() will flush and close stan-
     dard I/O channels and thereby corrupt the  parent  process's
     standard I/O data structures. Using exit(3C) will flush buf-
     fered data twice. See exit(2).

I don't know, however, if this is really true in Cygwin, but it might
explain some misdiagnosed hangs on your part.

Also, the execve call appears to be suspect.  Again, the Solaris man page
for execve states:

     The value in
     argv[0] should point to a filename that is  associated  with
     the process being started by one of the exec functions.
[snip]
     As indicated, argc is at least one and the
     first member of the array points to a string containing  the
     name of the file.

Attached is a modified test case that fixes a few of these issues, but
still hangs (or stutters; it does appear to proceed after long periods of
time).

-- 
Brian Ford
Lead Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
the best safety device in any aircraft is a well-trained crew...
-------------- next part --------------
/* main.cc

 *

 */



#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <fcntl.h>

#include <pthread.h>



void usage()

  {

  printf("Usage:\n");

  printf("-p <program>  - use program instead of /bin/ls \n");

  exit(1);

  }



void * forkit2me(void *data)

  {



  pid_t pid;



  printf("forking\n");

  if ((pid = fork()) < 0 )

    {

    printf("Unable to fork\n");

    _exit(1);

    }

  else if (pid == 0 )

    {

    printf ("child: %s\n", (char *)data);



    execle((char *)data,(char *)data, NULL,NULL);



    perror("exec failed");

    _exit(1);

    }

  else

    {

    printf ("parent\n");

    }

  printf ("done here\n");



  return data;

  }



int main(int argc, char * argv[])

  {

  int quit=0;



  static char * prog2run = "/bin/ls";

  if (argc > 0)

    {

    for (int i=1; i < argc; i++)

      {

      if ( argv[i][0]== '-' )

        {

        switch (argv[i][1])

          {

          case 'p':

            if ( i+1 < argc )

              {

              prog2run=argv[i+1];

              i++;

              }

            break;

          default:

            usage();

          }

        }

      }

    }



  fcntl( fileno( stdout ), F_SETFD, 1 );

  fcntl( fileno( stderr ), F_SETFD, 1 );



  int rc;

  pthread_attr_t ta;

  pthread_t threadId;

  rc=pthread_attr_init(&ta);

  if (rc)

    {

    printf("pthread_attr_init failed: rc (%d)\n",rc);

    return 1;

    }



  rc=pthread_attr_setdetachstate(&ta,PTHREAD_CREATE_DETACHED);

  if (rc)

    {

    printf("pthread_attr_setdetachstate failed: rc (%d)\n",rc);

    return 1;

    }



  while (!quit)

    {

    printf("here we go\n");

 

#ifdef PRFAIL

    printf("creating thread\n");

#endif

    rc=pthread_create(&threadId, &ta, forkit2me, prog2run);

    if (rc)

      {

      printf("pthread_create failed: rc (%d)\n",rc);

      break;

      }

#ifdef PRFAIL

    printf("created\n");

#endif

    }



  return 0;

  }



-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list