#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>

#define MAX_REC 15

static char global_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void subr(int level) {
  char local_str[2048];
  char *heap_str;
  int local_file;
  char local_file_name[255];
  int n;

  printf("Entering level %d\n", level);
  for (n=0; n<1024;n++) {
      sprintf(local_str + 2*n,"%2d",level);
  }
  heap_str = (char*) malloc(8192);
  memset(heap_str, 65+level, 8192);

  sprintf(local_file_name, "/tmp/callme-l%d", level);
  local_file = open(local_file_name,O_RDWR|O_CREAT);
    if (local_file < 1) {
      perror("open(local_file");
      exit(1);
    }
  write(local_file,local_str,2048);
  fsync(local_file);
  if (level < MAX_REC) {
    subr(++level);
  }
  else {
    sleep(900);
  }

  close(local_file);
}

int main () {
  char main_str[2048];
  int main_file;
  char *mmfile;

  sprintf(main_str, "%s", "abcdefghijklmnopqrstuvwxyz");
  main_file = open("/bin/csh",O_RDONLY); 
  if (main_file <0) {
    perror("callme: open");
    exit(1);
  }
  mmfile = mmap(NULL,8192,PROT_READ, MAP_SHARED, main_file,0); 
  if (mmfile == (char *) MAP_FAILED) {
    perror("callme: mmap");
    exit(1);
  }
  close(main_file); 
  printf("read from mmapped file: %lx\n",*mmfile);

  subr(0);

  munmap(mmfile,8192); 

  return(0);
}


