working with fork() and pthreads
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > working with fork() and pthreads




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    working with fork() and pthreads  
Elena


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-06 06:20 AM

Hi, I have the following situation:
in the main() I create a thread by using pthread_create(), then my
main() forks,
the child program performs some action and exits. The parent waits for
it with "waitpid()".

What I see is that the pthread manager process becomes a zombee, and
the parent doesn't success to see that his child exited.


Please look at the code and help me to see what is the problem (thanks
in advance):


#include <stdio.h>    /* Basic I/O routines     */
#include <sys/types.h>    /* standard system types  */
#include <netinet/in.h>   /* Internet address structures  */
#include <sys/wait.h>
#include <sys/socket.h>   /* socket interface functions   */
#include <netdb.h>    /* host to IP resolution  */
#include <sys/time.h>   /* for timeout values     */
#include <unistd.h>   /* for table size calculations  */
#include <sys/errno.h> //errors
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <pthread.h>


pthread_t monLoopID;
void monLoop(void)  //thread
{
while(1){
sleep(10);
}



}


int main()
//----------------------------------------------------------------------


{
pid_t child_pID;
int status,rc,i;


if(pthread_create(&monLoopID, 0 ,(void *)&monLoop, NULL) != 0)
{
printf("thread creation .errno = %d\n",errno);
exit(10);
}
for(i=0;i<10;i++)
sleep(1);
child_pID = fork();
if (child_pID == 0)                // child
{
printf("hello from child!\n");
system("echo 1 2 3 > 1.txt");
printf("bye from child!\n");
exit(0);
}
else if (child_pID < 0)            // failed to fork
{
exit(1);
}
else    //parent
{
printf("hello from parent, waiting for child to finish!\n");
rc = waitpid(child_pID,&status,0);
if(WIFEXITED(status))
{
printf("child finished\n");
while(1)
{
sleep(10);
printf("keep alive from parent!\n");
}
}
}






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-06 12:18 PM

Elena wrote:

[]

Please do not crosspost. Use multiposting instead. Use google to know
the difference.






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
Elena


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-30-06 12:18 PM

OK, next time I'll do so.






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
mARK


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 12:16 AM

the discussions of crossposting and multiposting which i found via
google all recommend to crosspost, not multipost, for the good reason
that newsreaders won't show a crossposted article twice.

they also recommend to crosspost only when it is truely appropriate.






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
mARK


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 12:16 AM

running this on red hat linux 9 with gcc 3.2.2, i get this output:

hello from parent, waiting for child to finish!
bye from child!
child finished
keep alive from parent!
keep alive from parent!
keep alive from parent!

and so on, and no zombies.






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
Jim Cochrane


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 12:16 AM

On 2006-05-30, Maxim Yegorushkin <maxim.yegorushkin@gmail.com> wrote:
> Elena wrote:
>
> []
>
> Please do not crosspost. Use multiposting instead. Use google to know
> the difference.
>

e.g.:

http://www.blakjak.demon.co.uk/mul_crss.htm

--


*** Posted via a free Usenet account from http://www.teranews.com ***





[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 12:18 PM


mARK wrote:
> the discussions of crossposting and multiposting which i found via
> google all recommend to crosspost, not multipost, for the good reason
> that newsreaders won't show a crossposted article twice.
>
> they also recommend to crosspost only when it is truely appropriate.

Sorry, I confused the things! ;)))






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
Sunil Varma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 12:18 PM


mARK wrote:
> running this on red hat linux 9 with gcc 3.2.2, i get this output:
>
> hello from parent, waiting for child to finish!
> bye from child!
> child finished
> keep alive from parent!
> keep alive from parent!
> keep alive from parent!
>
> and so on, and no zombies.

Please debug the binary file using gdb.
set a breakpoint at the vey next statement after the call to fork().
see the process status of the child using the child_pID.
you will observe the process status to Z(Zombie).

Regards






[ Post a follow-up to this message ]



    Re: working with fork() and pthreads  
mARK


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-31-06 06:18 PM


Sunil Varma wrote:
> mARK wrote:
> ...
>
> Please debug the binary file using gdb.
> set a breakpoint at the vey next statement after the call to fork().
> see the process status of the child using the child_pID.
> you will observe the process status to Z(Zombie).
>

hmm, yes, but only if you break in gdb; if you just run it is fine.  i
have had problems with threads and gdb before.  i was able to debug
without threads in that case, so i don't know what you can do about
this if you must have threads present.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:14 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register