Sign in to follow this  

zombie process

Recommended Posts

i have a deamon process u can call it system monitor. It is configured to run via the “/etc/inittab” and is a child of the system init process. It is responsible for starting/stopping/monitoring processes running on a host. If system montor has to start a subsystem/process, It forks twice to avoid subsystem it runs to turn zombie so that the processes it starts after forking twice become the children of init and system nonitor need not to wait for child to complete. Now there is a problem of system monitor turning defunct, which I want to avoid? or atleast I want to close all the file descriptors associated with it? Below is the code I am using, please suggest the changes i can make into it: runsubsystem() { pid_t pid=-1; pid_t tmppid=-1; int pfds[2]; char pipeBuf[30]; if(pipe(pfds) != 0){ printf("runsubsystem(): could not create pipe, hence returning without creating a process"); return; } // Start the subsystem. if((tmppid = fork()) == 0){ if((pid = fork()) == 0) { .... grand child/subsystem routine... } else if(pid == -1) { int errcode = errno; close(pfds[0]); close(pfds[1]); const char *errmsg = strerror(errcode); if(errmsg == NULL) errmsg = "Error number out of range"; printf( "ERROR : second fork failed in runsubsystem():<%s>", errmsg); } else { printf( "first fork parent, now about to exit :%d", pid); close(pfds[0]); char tmpBuf[10]; sprintf(tmpBuf, "%d", pid); /* We are copying the grandchild pid to send it to super parent/system monitor by pipe for its record*/ printf("first fork parent, now about to exit :%d :%s", pid, tmpBuf); write(pfds[1], tmpBuf, strlen(tmpBuf) + 1); close(pfds[1]); exit(0); } }else if(tmppid == -1){ int errcode = errno; close(pfds[0]); close(pfds[1]); printf("About to call wait"); const char *errmsg = strerror(errcode); if(errmsg == NULL) errmsg = "Error number out of range"; printf("ERROR : first fork failed in runsubsystem(): <%s>", errmsg); }else{ char tmpBuf[10]; close(pfds[1]); printf("Super Parent going for a read on the pipe"); int readRet = read(pfds[0], tmpBuf, 10); tmpBuf[readRet] = '\0'; int retChld = atoi(tmpBuf); printf( "Super Parent reading buf:%s :%d :%d", tmpBuf, retChld, readRet); wait(NULL); close(pfds[0]); pid = retChld; ----- system monitor work as usual-- }

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this