This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

printf output missing in multi-threaded program


Hi,

some of my multi-threaded programs don't print everything they should.
I have for example a small program to show that LinuxThreads don't
behave like real POSIX threads concerning "fork()" (only the thread
which creates a child process can wait for its termination with
LinuxThreads). When I run the program on Solaris I get the following
output:

gorbag work 23 fork_thr                          !!!! Solaris
Thread thr_fork: forking a child process.
Child process: I'm sleeping for 2 seconds.
Thread thr_wait: sleeping for 5 seconds.
Thread thr_fork: sleeping for 10 seconds.
Child process: terminating.
Thread thr_wait: try to wait for child process.
Thread thr_wait: child terminated.
Thread thr_fork: try to wait for my child.       !!!!!! missing in Cygwin
Thread thr_fork: No child processes              !!!!!! missing in Cygwin
gorbag work 24 


When I compile and run the program on Cygwin the last output from
"thr_fork" will not be displayed. I've upgraded Cygwin to the latest
version yesterday. "cygcheck -c" displays that all packages are OK. Cygwin
is installed on Windows XP with all updates excluding SP2.

eiger src 4 fork_thr
Thread thr_fork: forking a child process.        !!!! Cygwin
Thread thr_wait: sleeping for 5 seconds.
Thread thr_fork: sleeping for 10 seconds.
Child process: I'm sleeping for 2 seconds.
Child process: terminating.
Thread thr_wait: try to wait for child process.
Thread thr_wait: child terminated.
eiger src 5 


A search in the mailing list and on the web wasn't successful (lots of
results which weren't related to my problem). I've split the sleeping
time of thr_fork in a loop in one second parts to see when the thread
terminates.

eiger src 11 fork_thr
Thread thr_fork: forking a child process.
Thread thr_wait: sleeping for 5 seconds.
Thread thr_fork: sleeping for 10 seconds.
Child process: I'm sleeping for 2 seconds.
Thread thr_fork: 1 s of sleeping time passed
Thread thr_fork: 2 s of sleeping time passed
Child process: terminating.
Thread thr_fork: 3 s of sleeping time passed
Thread thr_fork: 4 s of sleeping time passed
Thread thr_wait: try to wait for child process.
Thread thr_wait: child terminated.
eiger src 12 

In my opinion it has something to do with the thread implementation in
Cygwin. Does anybody know why thr_fork dies when thr_wait terminates?
Thank you very much for any suggestions in advance.

That's the program without testing "ret".

--- snip ------------------------------------------------------------

/* gcc -o fork_thr fork_thr.c -lpthread					*/

#define _REENTRANT			/* must precede any "#include"!	*/

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

void thr_fork (void);
void thr_wait (void);

pid_t fork_pid;				/* ID of child process		*/

int main (void)
{
  int		 ret;			/* return value of a function	*/
  pthread_t	 thr_id [2];  		/* ID's of created threads	*/

  ret = pthread_create (&thr_id [0], NULL,
			(void * (*) (void *)) thr_fork,
			(void *) 0);
  ret = pthread_create (&thr_id [1], NULL,
			(void * (*) (void *)) thr_wait,
			(void *) 1);
  ret = pthread_join (thr_id [0], NULL);
  ret = pthread_join (thr_id [1], NULL);
  return 0;
}


void thr_fork (void)
{
  int i;

  printf ("Thread thr_fork: forking a child process.\n");
  fork_pid = fork ();
  switch (fork_pid)
  {
    case -1:				/* error: no process created	*/
      perror ("fork failed");
      errno = 0;
      break;

    case 0:				/* child process       		*/
      printf ("Child process: I'm sleeping for 2 seconds.\n");
      sleep (2);
      printf ("Child process: terminating.\n");
      exit (0);
      break;

    default:				/* parent process		*/
      printf ("Thread thr_fork: sleeping for 10 seconds.\n");
      for (i = 1; i < 11; i++)
      {
	sleep (1);
	printf ("Thread thr_fork: %d s of sleeping time passed\n", i);
      };
      printf ("Thread thr_fork: try to wait for my child.\n");
      if (waitpid (fork_pid, NULL, WNOHANG) == -1)
      {
	perror ("Thread thr_fork");
	errno = 0;
      }
      else
      {
	printf ("Thread thr_fork: child terminated.\n");
      };
  };
}


void thr_wait (void)
{
  printf ("Thread thr_wait: sleeping for 5 seconds.\n");
  sleep (5);
  printf ("Thread thr_wait: try to wait for child process.\n");
  if (waitpid (fork_pid, NULL, WNOHANG) == -1)
  {
    perror ("Thread thr_wait");
    errno = 0;
  }
  else
  {
    printf ("Thread thr_wait: child terminated.\n");
  };
}


-- snip -------------------------------------------------------------


Kind regards

Siegmar


--
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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]