Question about popen and pclose

Nelson Tsai tsai at nynex1.UUCP
Thu Aug 11 08:29:04 AEST 1988


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	<stdio.h>
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 ?



More information about the Comp.lang.c mailing list