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" );
return;
}
if( hProcess == 0 ) {
printf( "Executing child\n" );
exit(2);
}
printf( "Fork worked\n" );
if( waitpid( hProcess, &nStatus, 0 ) == -1 ) {
printf( "Wait failed\n");
return;
}
if( WIFEXITED( nStatus ) )
printf( "Program exited normally with %d\n", WEXITSTATUS(nStatus) );
else
printf( "Program failed %d\n", WEXITSTATUS(nStatus) );
}
The results I get are:
1.
Executing child2. - when calld from the other thread or e.g. "on button click" event
Fork worked
Program exited normally with 2
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,
LUK
_______________________________________________ gnome-list mailing list gnome-list@xxxxxxxxx http://mail.gnome.org/mailman/listinfo/gnome-list