Path: utzoo!utgpu!water!watmath!clyde!bellcore!rutgers!mit-eddie!husc6!necntc!linus!philabs!nynex1!tsai
From: tsai@nynex1.UUCP (Nelson Tsai)
Newsgroups: comp.lang.c
Subject: Question about popen and pclose
Keywords: popen,pclose
Message-ID: <364@nynex1.UUCP>
Date: 10 Aug 88 22:29:04 GMT
Organization: NYNEX Science and Technology, White Plains, N.Y.
Lines: 36

I have some questions about popen function in C.
According to the reference manual, popen should come with pclose.
But, in my Sun OS 3.5 interesting thing happened if I forgot
to put pclose after popen. For instance, the following program works:

#include	
main() 
{
	int i ;
	for(i=1;i<100;i++) 
		proc(i);
}

proc(i)
int	i;
{
	FILE *gg;
	gg = popen("date","r");
	printf("%d \n",i);
}

But, if the function proc() was changed to :

proc(i)
int	i;
{
	FILE 	*gg;	
	char	ss[200];
	gg = popen("date","r");
	printf("%d \n",i);
	fgets(ss,200,gg);
}

Then, after executed about 20 times, it caused segmentation fault
(core dumped).  Why is that ?  Why reading from the pipe cause the
segmentation fault ?