ERROR: Too many open raster files

Here is an extract from the code which is causing the error:

ERROR: Too many open raster files

      sprintf(chardist,"%i",hold_distance[i]);
      strcpy(tempchar,strcat(tempchar,chardist));
      sprintf(systemstring,"%s%s%s%s%s%s%s","r.buffer input=",route," output=",r
oute,"_BUFF distances=",tempchar," units=meters");
      printf("%s\n",systemstring);
      system(systemstring);
      system("sleep 3");

      sprintf(systemstring,"%s%s%s%s%s%s%s%s%s","r.cross input=\"",map_to_use,",
",route,"_BUFF\", output=",map_to_use,"_",route,"_CR");
      system(systemstring);

     if ((fd = G_open_cell_old(map_to_use,mapset)) < 0)
        G_fatal_error("Could not open raster map ");

      if ((buff_fd = G_open_cell_old(map_buff,mapset)) < 0)
        G_fatal_error("Could not open raster buffer map ");

      if ((new_map_fd = G_open_cell_new(new_map)) < 0)
        G_fatal_error("Could not create overlay map");
      
      for(row = 0; row < nrows;row++)
        {
          G_zero_cell_buf(buf);
          G_zero_cell_buf(buff_buf);
          G_zero_cell_buf(new_map_buf);
          G_get_map_row(fd,buf,row);
          G_get_map_row(buff_fd,buff_buf,row);

          for (col = 0;col < ncols;col++)
            {
              tmp = hold_impact[buff_buf[col]] * hold_catrank[buf[col]];
              if (tmp == 0)
                new_map_buf[col] = MAX_NO_IMPACT/2;
--More-- if (tmp < 0)
                new_map_buf[col] = (MAX_NO_IMPACT - (tmp/MAX_NO_IMPACT))/2;
              if (tmp > 0)
                new_map_buf[col] = ((tmp/MAX_NO_IMPACT) - MAX_NO_IMPACT)/2;

            }

          G_put_map_row(new_map_fd,new_map_buf);
        }
          
      G_close_cell(fd);
      G_close_cell(buff_fd);
      G_close_cell(new_map_fd);

The program is taking a map and a route map, calling r.buuffer and r.cross
by a system call to produce a buffer map and then using

G_open_cell_nap_old and G_open_cell_map_new on 2 and 1 files
respectively to prouduce a new map.As you can see i close all
3 files at the end.

After this, I then run another program ( same executabele), which
draws these four maps : base map, route map, buffer map, my own calculated
map.This is a modification of slide.show.sh as follows:
for i in $MAPLIST
do
                
        atnum=`expr $atnum % $totmaps`
        at_vert=`expr $atnum % $DOWN`
        at_hori=`expr $atnum / $DOWN`

        d.frame -s lab.$at_hori.$at_vert
        echo $i in $MAPSET | d.text size=80

        d.frame -s map.$at_hori.$at_vert
        d.erase
        d.rast $i
        atnum=`expr $atnum + 1`
                
done

now, an error occurs on two occasions.Sometimes, the r.buffer command
fails with ERROR: Too many raster files open and sometimes one of the
d.rast commands fails with the same error.When I then exit GRASS and delete
some of my calculated raster files, re-enter and re-run the problem disappears until those files hacve been re-created.

The system I am using is GRASS 4.0 on an RS6000/320 running AIX.
The file descriptor limit seems to be about 2000 -
I ascertained this by running a test C program whcih
fopens files until it falls over,

Any help would be much appreciated.

Joe Mintz, Dept Computer Science, Birkbeck Colleege.