Advertisement

Intermediate process hangs while forking twice?

Started by December 20, 2005 12:56 AM
2 comments, last by markr 18 years, 8 months ago
To avoid zombie child process, I am forking my parent process twice and led the intermediate process to exit so that my grand child process can become the child of init. But unusual thing is that my intermediate process hangs. While doing strace it is showing futex_wait. What can be the reasons of intermediate process to hang. here is a snippet of code.. void forktwice(...) { pid_t pid=-1; pid_t tmppid=-1; int pfds[2]; char pipeBuf[30]; if(pipe(pfds) != 0){ return; } if((tmppid = fork()) == 0){ if((pid = fork()) == 0) { #ifdef LINUXPlatform signal(SIGCHLD, sysMonSignalHandler); #endif close(pfds[0]); close(pfds[1]); ---- //child do sth..---- } else { close(pfds[0]); char tmpBuf[10]; sprintf(tmpBuf, "%d", pid); int wByte = write(pfds[1], tmpBuf, strlen(tmpBuf) + 1); close(pfds[1]); sleep(5); ///Intermediate process exiting, it sends the child pid to superparent through //pipe. exit(0); } } else { char tmpBuf[10]; close(pfds[1]); //Super Parent going for a read on the pipe int readRet = read(pfds[0], tmpBuf, 10); tmpBuf[readRet] = '\0'; close(pfds[0]); int retChld = atoi(tmpBuf); int pstatus=0; //Calling waitpid non-block for intermediate waitpid(tmppid,&pstatus,0); ------------------------------------- }
It's not clear exactly what you're trying to do, but maybe there is a race condition.

Notice that you don't check the result from waitpid, do you know if it's succeeded.

If by "hangs", you mean, it becomes a zombie because its parent never waited, that's expected.

Have you tried running it through a debugger to find out where it is?

What you're doing seems obfuscated and unnecessarily complicated. Why not use the daemon() function?

Mark
Advertisement
Dear Mark,
My Super parent is a daemon process which need to spawn child process. To make the child processes as child of init it forks twice and intermediate process exits. I am checking the status of waitpid, called by superparent. But since both parent and super parent are hanging at futex_wait I cannot see the return status of the waitpid. By hanging I do not mean they turned defunct. But the superparent stopped processing the requests and intermediate is not exiting they both starts waiting for something and reached a lock condition.
My concern is why intermediate is not exiting, as only thing expected out of intermediate process is to write the pid of its child in a pipe and then exits.
regards,
rish
Run it through a debugger to see where it's hung.

It's presumably doing a blocking wait for something - the only things I can see here are the read and the write.

Maybe reading 10 bytes from the pipe will wait until it has at least 10 bytes - this will cause a problem as you may be writing fewer than 10.

I don't know why you'd need to have something reparented to init, but there's no obvious reason why you can't call daemon() from a process which is already daemon (which has forked).

Normally fork() and then having the child not immediately call _exit or exec, is a bad idea, as you'll get copies of things which belong to libraries (file descriptors mostly), possibly resulting in unexpected behaviour.

Mark

This topic is closed to new replies.

Advertisement