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" );
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 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,
LUK
_______________________________________________
gnome-list mailing list
gnome-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gnome-list


Relevant Pages

  • Re: portupgrade causes kernel message: maxproc limit exceeded by uid 0
    ... glib-2.26.1 inside a clean chroot. ... it might be a good idea to verify that your build environment ... printf: Cannot fork: Resource temporarily unavailable ...
    (freebsd-questions)
  • Re: Interview question !!!
    ... On many platforms, main _will_ print 220. ... (OT Try putting a fork() ... between fnand printf() in main and you ... Tim. ...
    (comp.lang.c)
  • system() without waiting
    ... I want to make a systemcall, but do not wait for completition. ... One solution would be to fork() and system in the child ... int main(int argc, char** argv) {printf ...
    (comp.unix.programmer)
  • Re: how many times the printf will be executed ?
    ... how many times the printf will be executed. ... Wrong newsgroup: fork() isn't part of the C standard, ... And if you're at it also use 'int mainand add ...
    (comp.lang.c)