Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Sep 2006
    Posts
    22

    Arrow 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.

    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");
    	}
                 .
                 .
    Can someone help me out to fix this issue.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    When I run this program:
    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() */
    I get this:
    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>]
    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.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...