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 ]
|