/*
 * TLB stress test; the program forks itself into a tree of subprocesses
 * that each resume with some heavy stack trashing; tests if the stack
 * modifications do not 'leak' to the other processes.
 */



#include <eti/mm.h>


int fib(i)
{
    if (i<2) { return 1; }
        else { return fib(i-1) + fib(i-2); }
}    


void FORK() 
{ 
    int pid   = getpid(); 
    int result= fork(); 
    
    if (result == 0) { 
        int i;
        for (i=0; i<41; i++) {
            printf("pid= %2d: Fib(%d)= %d\n", getpid(), i, fib(i) );   
        }
    }
    
    printf("Program %d Forked; result= %d\n", pid, result ); 
}


int INC(int i)
{
    if (i==0) { return 0; }
         else { 
//              printf("pid= %2d: %d 0x%08x\n", getpid(), i, &i);

                if (i==49000) { FORK(); }
                if (i==48000) { FORK(); }
                if (i==47000) { FORK(); }
                if (i==46000) { FORK(); }
                if (i==45000) { FORK(); }
                if (i==44000) { FORK(); }
                if (i==43000) { FORK(); }

                return INC(i-1)+1; 
              }
}


main() 
{
  int x,z,i;
  int orig_pid= getpid();

  printf("Program %2d Started\n", getpid());
  
  x= hmpv_read_fcnt_cl0();
  for (i=0; i<1000; i++) { getpid(); }
  z= hmpv_read_fcnt_cl0();
  printf("getpid takes %d cycles\n", (z-x) / 1000);

  i= INC(50000);
  printf("Program %2d Terminated; result= %d\n", getpid(), i);
}


mmain()
{
    int i;

    for (i=0; 1; i++) {
        printf( "USER MODE WRITE %d\n", i);
    }
}



