|
Home > Archive > Unix Programming > May 2006 > working with fork() and pthreads
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
working with fork() and pthreads
|
|
|
| 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");
}
}
}
| |
| Maxim Yegorushkin 2006-05-30, 7:18 am |
| Elena wrote:
[]
Please do not crosspost. Use multiposting instead. Use google to know
the difference.
| |
|
| OK, next time I'll do so.
| |
|
| 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.
| |
|
| 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.
| |
| Jim Cochrane 2006-05-30, 7:16 pm |
| 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 ***
| |
| Maxim Yegorushkin 2006-05-31, 7:18 am |
|
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! ;)))
| |
| Sunil Varma 2006-05-31, 7:18 am |
|
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
| |
|
|
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.
|
|
|
|
|