problem with fork

Hi all,

I write the program in C++, GTK, and need to use the fork. It did not work as expected, so made some tests trying this from different parts of the program. It looks like the problem exists when I try to create the fork from the other then main thread, and from any GTK event (e.g. "on_button_click"). In other parts of the program I can properly create the thread. The function which I call is here (as simple as possible):

void test()
   int   nStatus;
   pid_t hProcess;

   hProcess = fork();
   if( hProcess == -1 ) {
      printf( "Unable to fork process\n" );
   if( hProcess == 0 ) {
      printf( "Executing child\n" );
   printf( "Fork worked\n" );

   if( waitpid( hProcess, &nStatus, 0 ) == -1 ) {
      printf( "Wait failed\n");

   if( WIFEXITED( nStatus ) )
      printf( "Program exited normally with %d\n", WEXITSTATUS(nStatus) );
      printf( "Program failed %d\n", WEXITSTATUS(nStatus) );

The results I get are:
Executing child
Fork worked
Program exited normally with 2
2. - when calld from the other thread or e.g. "on button click" event
Fork worked
Program failed 0

In the second, incorrect result you can see there is no "Executeing child" printed out. Can you think of any reason of such weird behaviour?

Best regards,

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]