they don't have to modify alarmtest.c, so we can use the original version to test, to make it harder to cheat.
		
			
				
	
	
		
			88 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
//
 | 
						|
// test program for the alarm lab.
 | 
						|
// you can modify this file for testing,
 | 
						|
// but please make sure your kernel
 | 
						|
// modifications pass the original
 | 
						|
// versions of these tests.
 | 
						|
//
 | 
						|
 | 
						|
#include "kernel/param.h"
 | 
						|
#include "kernel/types.h"
 | 
						|
#include "kernel/stat.h"
 | 
						|
#include "kernel/riscv.h"
 | 
						|
#include "user/user.h"
 | 
						|
 | 
						|
void test0();
 | 
						|
void test1();
 | 
						|
void periodic();
 | 
						|
 | 
						|
int
 | 
						|
main(int argc, char *argv[])
 | 
						|
{
 | 
						|
  test0();
 | 
						|
  test1();
 | 
						|
  exit();
 | 
						|
}
 | 
						|
 | 
						|
volatile static int count;
 | 
						|
 | 
						|
void
 | 
						|
periodic()
 | 
						|
{
 | 
						|
  count = count + 1;
 | 
						|
  printf(1, "alarm!\n");
 | 
						|
  sigreturn();
 | 
						|
}
 | 
						|
 | 
						|
// tests whether the kernel calls
 | 
						|
// the alarm handler even a single time.
 | 
						|
void
 | 
						|
test0()
 | 
						|
{
 | 
						|
  int i;
 | 
						|
  printf(1, "test0 start\n");
 | 
						|
  count = 0;
 | 
						|
  sigalarm(2, periodic);
 | 
						|
  for(i = 0; i < 1000*500000; i++){
 | 
						|
    if((i % 250000) == 0)
 | 
						|
      write(2, ".", 1);
 | 
						|
    if(count > 0)
 | 
						|
      break;
 | 
						|
  }
 | 
						|
  sigalarm(0, 0);
 | 
						|
  if(count > 0){
 | 
						|
    printf(1, "test0 passed\n");
 | 
						|
  } else {
 | 
						|
    printf(1, "test0 failed\n");
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
void __attribute__ ((noinline)) foo(int i, int *j) {
 | 
						|
  if((i % 2500000) == 0) {
 | 
						|
    write(2, ".", 1);
 | 
						|
  }
 | 
						|
  *j += 1;
 | 
						|
}
 | 
						|
 | 
						|
void
 | 
						|
test1()
 | 
						|
{
 | 
						|
  int i;
 | 
						|
  int j;
 | 
						|
 | 
						|
  printf(1, "test1 start\n");
 | 
						|
  count = 0;
 | 
						|
  j = 0;
 | 
						|
  sigalarm(2, periodic);
 | 
						|
  for(i = 0; i < 500000000; i++){
 | 
						|
    if(count >= 10)
 | 
						|
      break;
 | 
						|
    foo(i, &j);
 | 
						|
  }
 | 
						|
  if(i != j || count < 10){
 | 
						|
    // i should equal j
 | 
						|
    printf(1, "test1 failed\n");
 | 
						|
  } else {
 | 
						|
    printf(1, "test1 passed\n");
 | 
						|
  }
 | 
						|
}
 |