memory crash

sheldon newhouse (pokie@tcimet.net)
Mon, 9 Dec 1996 19:48:33 -0500 (EST)


Just for the hell of it, I decided to see what would happen if I
overloaded my system with cpu intensive programs which would cause the
system to run out of memory and swap. The system started thrashing, and
ultimately required a hard boot. I don't think this is healthy. Any user
could do this.

I was hoping that the system would not allow me to run more programs
as I got close to the total memory and swap limit. Not so.

My system:

P5-133, Triton chipset. 48 MB RAM, 98 MB swap. Kernel 2.0.27

Basically, I had a C program which took about 7 MB or RAM and ran many
copies of the same program at the same time.

Just in case anyone is interested I enclose the source code below. It was
compiled as cc -O.

I have some questions:
1. Is it possible to set limits on accounts so that
a. this kind of thing cannot be done ?
b. other performance won't suffer?
2. What should be done to insulate the system against this kind of thing?

-sen

Source code:

/* this program when run sufficiently many times simultaneously caused a
hard crash. */

#include<stdio.h>
#include<math.h>
#define isize 150000
#define minit 5
#define maxit 500
#define xl (double)-3.0
#define xu (double)3.0
#define mult 2
#define numprn 5
#define xi 0.0
#define yi 0.0
#define v1 1.0
#define v2 0.0

main()
{
FILE *pfl,*fopen();
double a=(double)1.37,aleng=(double)1.0,hbleng,ha,b=(double)0.3,si,ti;
double y1a[isize],*py1a=y1a,x1a[isize],*px1a=x1a,al,rnumit,rprn,ent,
rsize,rmult,bleng,cbleng,amax,asum,bmax,y1,wn,w2,
cmax[maxit],*pcmax=cmax,csum[maxit],*pcsum=csum;
int jsz[maxit],*pjsz=jsz,jsize,ii,numit,j,i,k;
char flname[15],*pflname=flname;
double w1a[isize],*pw1a=w1a,w2a[isize],*pw2a=w2a,aexpan[isize],
*paex=aexpan,bexpan[isize],*pbex=bexpan;

hbleng=(double)0.5*aleng;
rsize=(double)isize;
rmult=(double)mult;
bleng=aleng/rsize;
cbleng=bleng/rmult;

ha=(double)-2.0*a;
sprintf(pflname,"htest.op\0");

pfl=fopen(flname,"w");
fprintf(pfl,"This is Henon map (x1,y1)=(a-x*x+b*y,x)\n");
fprintf(pfl, " with xi=%f yi=%f\n",xi,yi);
fprintf(pfl,"isize=%d mult=%d \n",isize,mult);
fprintf(pfl," a= %f maxit=%d\n",a,maxit);
fprintf(pfl,"N=number of iterates, l=log of length e=entropy\n");
for(numit=0;numit<maxit;numit++)
{ *(pcmax+numit)=(double)-1.0e200;
*(pcsum+numit)=0.0;
*(pjsz+numit)=0;
};

si=xi-hbleng*v1;
ti=yi-hbleng*v2;

for(k=0;k<mult;k++)
{ *px1a=si;
*py1a=ti;
*pw1a=v1;
*pw2a=v2;
*paex=0.0;

for(i=1;i<isize;i++)
{ *(px1a+i)=*(px1a+i-1)+cbleng*v1;
*(py1a+i)=*(py1a+i-1)+cbleng*v2;
*(pw1a+i)=v1;
*(pw2a+i)=v2;
*(paex+i)=0.0;
};
jsize=isize;
for(numit=0;numit<maxit;numit++)
{ i=0;
while(i<jsize)
{
if((*(px1a+i)<=xl)||(*(px1a+i)>=xu))
{
*(px1a+i)=*(px1a+jsize-1);
*(py1a+i)=*(py1a+jsize-1);
*(pw1a+i)=*(pw1a+jsize-1);
*(pw2a+i)=*(pw2a+jsize-1);
*(paex+i)=*(paex+jsize-1);
jsize=jsize-1;
}
else
{
w2=*(pw2a+i);
y1=*(py1a+i);
*(py1a+i)=b*(*(px1a+i));
*(pw2a+i)=b*(*(pw1a+i));
*(pw1a+i)=ha*(*(px1a+i))*(*(pw1a+i))+w2;
*(px1a+i)=(double)1.0+y1-a*(*(px1a+i)*(*(px1a+i)));
wn=(fabs(*(pw1a+i))>fabs(*(pw2a+i)))?
fabs(*(pw1a+i)):fabs(*(pw2a+i));
*(paex+i)+=log(wn);
*(pw1a+i)/=wn;
*(pw2a+i)/=wn;
i++;
};
};
if(jsize<1)break;
amax=*paex;
for (i=0;i<jsize;i++)
amax=(*(paex+i)>amax)?*(paex+i):amax;
for(i=0;i<jsize;i++)
*(pbex+i)=*(paex+i)-amax;
asum=0.0;
for(i=0;i<jsize;i++)
asum+=exp(*(pbex+i));
bmax=(*(pcmax+numit)>amax)?*(pcmax+numit):amax;
*(pcsum+numit)=*(pcsum+numit)*exp(*(pcmax+numit)
-bmax)+
asum*exp(amax-bmax);
*(pcmax+numit)=bmax;
*(pjsz+numit)+=jsize;
};
si+=(aleng/rmult)*v1;
ti+=(aleng/rmult)*v2;
};
for(numit=minit-1;numit<maxit;numit++)
{ if((*(pcsum+numit)<=0.0)||(*(pjsz+numit)<=0))break;
al=*(pcmax+numit)+log(*(pcsum+numit))-log((double)(*(pjsz+numit)));
if(((numit+1)%numprn)!=0.0)continue;
ent=al/((double)(numit+1));
fprintf(pfl,"N=%d L=%.11e E=%.11e\n",numit+1,al,ent);
};
al=*(pcmax+numit);
fprintf(pfl,"either maxit reached or\n no more points in the window\n");
fprintf(pfl,"%e %e\n",xl,xu);
fclose(pfl);
}