[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gnome
Subject:    problem with fork
From:       "Lukasz Gromotowicz" <gromot () gmail ! com>
Date:       2008-05-29 22:05:14
Message-ID: c938281f0805291505g3fac4ef7lce0da92e950be44c () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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

[Attachment #5 (text/html)]

Hi all,<br><br>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. &quot;on_button_click&quot;). In other \
parts of the program I can properly create the thread. The function which I call is \
here (as simple as possible):<br>

<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt \
0.8ex; padding-left: 1ex;" class="gmail_quote">void test()<br> {<br>&nbsp;&nbsp; \
int&nbsp;&nbsp; nStatus;<br>&nbsp;&nbsp; pid_t hProcess;<br><br>&nbsp;&nbsp; hProcess \
= fork();<br>&nbsp;&nbsp; if( hProcess == -1 ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
printf( &quot;Unable to fork process\n&quot; );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
return;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; if( hProcess == 0 ) \
{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf( &quot;Executing child\n&quot; );<br> \
&nbsp;&nbsp;&nbsp; &nbsp; exit(2);<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; printf( \
&quot;Fork worked\n&quot; );<br><br>&nbsp;&nbsp; if( waitpid( hProcess, &amp;nStatus, \
0 ) == -1 ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf( &quot;Wait \
failed\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>&nbsp;&nbsp; \
}<br><br>&nbsp;&nbsp; if( WIFEXITED( nStatus ) )<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
printf( &quot;Program exited normally with %d\n&quot;, WEXITSTATUS(nStatus) \
);<br>&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf( &quot;Program \
failed %d\n&quot;, WEXITSTATUS(nStatus) );<br>}<br></blockquote><br>The results I get \
are:<br> 1. <br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: \
0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">Executing child<br>Fork \
worked<br>Program exited normally with 2<br></blockquote>2. - when calld from the \
other thread or e.g. &quot;on button click&quot; event <br> <blockquote \
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; \
padding-left: 1ex;" class="gmail_quote">Fork worked<br>Program failed \
0<br></blockquote><br>In the second, incorrect result you can see there is no \
&quot;Executeing child&quot; printed out. Can you think of any reason of such weird \
behaviour? <br> <br><br>Best regards,<br>LUK<br><br>



_______________________________________________
gnome-list mailing list
gnome-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gnome-list


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic