Results 1 to 2 of 2
Hi
Im using popen call, which in turn forks, invokes a shell and ties one end of pipe to it. Fine, but even after pclose -ing the dead shell is ...
- 11-22-2007 #1Just Joined!
- Join Date
- Sep 2006
- Posts
- 22
popen leads to a sh<defunct>
Hi
Im using popen call, which in turn forks, invokes a shell and ties one end of pipe to it. Fine, but even after pclose-ing the dead shell is not reaped out by main.
But man pages say pclose does a wait4. I badly need to get rid of that sh<defunct>. Below is snap shot of code what I do.
Can someone help me out to fix this issue.Code:. . sprintf(arg,"pidof %s",process); /* Clearing out buffer */ fflush(stdout); if((file_fp = popen(arg,"r")) == NULL) { printf(" Unable to find the Pid "); return FAILURE; } fgets(pid,10,file_fp); proc_pid = atoi(pid); if(pclose(file_fp) != 0) { printf("Failed to close pipe\n"); } . .
- 11-22-2007 #2
When I run this program:
I get this:Code:#include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define process "bash" int main(void) { int dummy; int jndex; char arg[80]; char pid[80]; FILE *file_fp; /* Create a defunct process so we can see how they look. */ dummy=fork(); if(dummy==-1) { perror("fork"); exit(1); } if(dummy==0) { exit(0); } printf("This is how a defunct process looks:\n"); fflush(stdout); system("ps -ef | grep defunct | grep -v grep | grep -v ps"); sprintf(arg,"/sbin/pidof %s",process); printf("This is the object command:\n%s\n", arg ); printf("This is the output of the object command:\n"); fflush(stdout); system(arg); if((file_fp=popen(arg,"r"))==NULL) { perror("popen()"); exit(1); } fgets(pid,sizeof(pid),file_fp); if(pclose(file_fp)!=0) { perror("pclose()"); exit(1); } printf("Any additional defunct processes?\n"); fflush(stdout); system("ps -ef | grep defunct | grep -v grep | grep -v ps"); printf("The acid test.\n"); for(jndex=0; jndex<10; jndex++ ) { if((file_fp=popen(arg,"r"))==NULL) { perror("popen()"); exit(1); } fgets(pid,sizeof(pid),file_fp); if(pclose(file_fp)!=0) { perror("pclose()"); exit(1); } } printf("Any additional defunct processes?\n"); fflush(stdout); system("ps -ef | grep defunct | grep -v grep | grep -v ps"); return 0; } /* main() */
What do you get when you run this exact program? (You may have to change the path of pidof if it's not in /sbin on your system.)Code:This is how a defunct process looks: wally 26985 26984 0 01:27 pts/19 00:00:00 [t <defunct>] This is the object command: /sbin/pidof bash This is the output of the object command: 20191 31245 31236 31222 31039 31030 31021 30950 30941 30932 Any additional defunct processes? wally 26985 26984 0 01:27 pts/19 00:00:00 [t <defunct>] The acid test. Any additional defunct processes? wally 26985 26984 0 01:27 pts/19 00:00:00 [t <defunct>]
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote