コード例 #1
0
ファイル: ostests.c プロジェクト: zhan1182/OS-Design
void RunOSTests() {
  // STUDENT: run any os-level tests here
  
  int handle = 0;
  char *file1 = "test1";
  char mem[20000];
  char mem_read[20000];
  int start_byte = 0;
  int num_bytes = 0;
  int ct = 0;
  char stuff = 'a';
  int ct_err = 0;

  // first, initialize the file system
  if(DfsOpenFileSystem() == DFS_FAIL)
    {
      dbprintf('s', "ostests: failed to open file system.\n");
    }

  //make sure such file does not exist, if yes, error
  if(DfsInodeFilenameExists(file1) != DFS_FAIL)
    {
      dbprintf('s', "Error: found unexisted file.\n");
    }

  // now create an inode for such file
  handle = DfsInodeOpen(file1);
  // now check if such file exists
  printf("hello.\n");
  if(DfsInodeFilenameExists(file1) != handle)
    {
      dbprintf('s', "Error: file inode did not create.\n");
    }

  // start to write bytes into memory
  // first write something into the mem
  
  for(ct = 0; ct < 512; ct++)
    {
      mem[ct] = stuff;
      stuff = 'z' ? 'a' : (stuff + 1);
    }

  // now that mem is stuffed, we write part of it into file
  num_bytes = 350; // write 350 bytes
  start_byte = 0; // start from 25th byte

  //////////////////////// ERROR HERE ///////////////////////
  if(DfsInodeWriteBytes(handle, mem, start_byte, num_bytes) != num_bytes)
    {
      dbprintf('s', "dfs write: write 350 bytes failed.\n");
    }

  // now check if previous 25 bytes are empty and all stuff are written, read from 0, to 512
  if(DfsInodeReadBytes(handle, mem_read, 0, 512) != num_bytes)
    {
      dbprintf('s', "dfs read: read 350 bytes failed.\n");
    }
  
  // now test if the read data is identicle to the written
  // first test if first 25 are 0
  for(ct = 0; ct < 25; ct++)
    {
      if(mem_read[ct] != 0)
	{
	  ct_err++;
	}
    }
  /* dbprintf('s', "Among the first 25 data, %d are not correct.\n", ct_err); */
  printf("Among the first 25 data, %d are not correct.\n", ct_err);
  // then test if next 350 bytes are what were written
  ct_err = 0;
  for(; ct < 375; ct++)
    {
      if(mem_read[ct] != mem[ct-25])
	{
	  ct_err++;
	}
    }
  /* dbprintf('s', "Among the next 350 bytes, %d are not correct.\n", ct_err); */
  printf("Among the next 350 bytes, %d are not correct.\n", ct_err);
  // last check if the remaining 138 bytes are null
  ct_err = 0;
  for(; ct < 512; ct++)
    {
      if(mem_read[ct] != 0)
	{
	  ct_err++;
	}
    }
  /* dbprintf('s', "Among the last 138 bytes, %d are not correct.\n", ct_err); */
  printf("Among the last 138 bytes, %d are not correct.\n", ct_err);

  // next test if we can over write into the file
  // change the mem
  stuff = '1';
  for(ct = 0; ct < 1024; ct++)
    {
      mem[ct] = stuff;
      stuff = '9' ? '1' : (stuff + 1);
    }
  // now write this into the previous
  if(DfsInodeWriteBytes(handle, (void *)mem, 0, 1024) != num_bytes)
    {
      dbprintf('s', "dfs write: write 1024 bytes failed.\n");
    }
  
  // now check if previous 25 bytes are empty and all stuff are written, read from 0, to 512
  if(DfsInodeReadBytes(handle, mem_read, 0, 1024) != num_bytes)
    {
      dbprintf('s', "dfs read: read 1024 bytes failed.\n");
    }

  ct_err = 0;
  for(ct = 0; ct < 1024; ct++)
    {
      if(mem_read[ct] != mem[ct])
	{
	  ct_err++;
	}
    }
  /* dbprintf('s', "Among the first dfs block, %d are not correct.\n", ct_err); */
  printf("Among the first dfs block, %d are not correct.\n", ct_err);

  // we need to check if those data still there after closing file system
  // now we test how it works when write more than one block
  stuff = 'a';
  for(ct = 0; ct < 1025; ct++)
    {
      mem[ct] = stuff;
      stuff = 'z' ? 'a' : (stuff + 1);
    }
  
// now write this into the previous
  if(DfsInodeWriteBytes(handle, (void *)mem, 1024, 1025) != num_bytes)
    {
      dbprintf('s', "dfs write: write 1024 bytes failed.\n");
    }
  
  // now check if previous 25 bytes are empty and all stuff are written, read from 0, to 512
  if(DfsInodeReadBytes(handle, mem_read, 1024, 1025) != num_bytes)
    {
      dbprintf('s', "dfs read: read 1024 bytes failed.\n");
    }

  ct_err = 0;
  for(ct = 0; ct < 1025; ct++)
    {
      if(mem_read[ct] != mem[ct])
	{
	  ct_err++;
	}
    }
  /* dbprintf('s', "Among the second dfs block, %d are not correct.\n", ct_err); */
  printf("Among the second dfs block, %d are not correct.\n", ct_err);

  // check if in the third block, all are null except the first byte
  if(DfsInodeReadBytes(handle, mem_read, 2048, 1024) != 1024)
    {
      dbprintf('s', "dfs read: third 1024 bytes failed.\n");
    }
  if(mem_read[0] == 0)
    {
      dbprintf('s', "Error: previous write was not right.\n");
    }

  for(ct = 1; ct < 1024; ct++)
    {
      if(mem_read[ct] != 0)
	{
	  dbprintf('s', "Error: a byte is not written while it is not supposed to.\n");
	  break;
	}
    }

  // now we write into enough bytes to create indirect table
  stuff = 'A';
  for(ct = 0; ct < 13313; ct++)
    {
      mem[ct] = stuff;
      stuff = 'Z' ? 'A' : (stuff + 1);
    }
  // write till 5th indirect block, and one more extra byte in 6th
  if(DfsInodeWriteBytes(handle, (void *)mem, 2048, 13313) != 13313)
    {
      dbprintf('s', "dfs write: write 13313 bytes failed.\n");
    }

  // we should check this by using block print

  // close inode
  if(DfsInodeDelete(handle) != DFS_SUCCESS)
    {
      dbprintf('s', "Error: inode delete fail.\n");
    }

  // close the file system
  if(DfsCloseFileSystem() != DFS_SUCCESS)
    {
      dbprintf('s', "Error: file system close fail.\n");
    }


  printf("======================== ostests completed ===============================.\n");
}
コード例 #2
0
ファイル: process.c プロジェクト: yfeleke/ece469g05
//----------------------------------------------------------------------
//
//	main
//
//	This routine is called when the OS starts up.  It allocates a
//	PCB for the first process - the one corresponding to the initial
//	thread of execution.  Note that the stack pointer is already
//	set correctly by _osinit (assembly language code) to point
//	to the stack for the 0th process.  This stack isn't very big,
//	though, so it should be replaced by the system stack of the
//	currently running process.
//
//----------------------------------------------------------------------
void main (int argc, char *argv[])
{
  int i,j;
  int n;
  char buf[120];
  char *userprog = (char *)0;
  int base=0;
  int numargs=0;
  int allargs_offset = 0;
  char allargs[SIZE_ARG_BUFF];
  
  debugstr[0] = '\0';

  printf ("Got %d arguments.\n", argc);
  printf ("Available memory: 0x%x -> 0x%x.\n", (int)lastosaddress, MemoryGetSize ());
  printf ("Argument count is %d.\n", argc);
  for (i = 0; i < argc; i++) {
    printf ("Argument %d is %s.\n", i, argv[i]);
  }

  FsModuleInit ();
  for (i = 0; i < argc; i++) 
  {
    if (argv[i][0] == '-') 
    {
      switch (argv[i][1]) 
      {
      case 'D':
	dstrcpy (debugstr, argv[++i]);
	break;
      case 'i':
	n = dstrtol (argv[++i], (void *)0, 0);
	ditoa (n, buf);
	printf ("Converted %s to %d=%s\n", argv[i], n, buf);
	break;
      case 'f':
      {
	int	start, codeS, codeL, dataS, dataL, fd, j;
	int	addr = 0;
	static unsigned char buf[200];
	fd = ProcessGetCodeInfo (argv[++i], &start, &codeS, &codeL, &dataS,
				 &dataL);
	printf ("File %s -> start=0x%08x\n", argv[i], start);
	printf ("File %s -> code @ 0x%08x (size=0x%08x)\n", argv[i], codeS,
		codeL);
	printf ("File %s -> data @ 0x%08x (size=0x%08x)\n", argv[i], dataS,
		dataL);
	while ((n = ProcessGetFromFile (fd, buf, &addr, sizeof (buf))) > 0) 
	{
	  for (j = 0; j < n; j += 4) 
	  {
	    printf ("%08x: %02x%02x%02x%02x\n", addr + j - n, buf[j], buf[j+1],
		    buf[j+2], buf[j+3]);
	  }
	}
	close (fd);
	break;
      }
      case 'u':
	userprog = argv[++i];
        base = i; // Save the location of the user program's name 
	break;
      default:
	printf ("Option %s not recognized.\n", argv[i]);
	break;
      }
      if(userprog)
        break;
    }
  }
  dbprintf ('i', "About to initialize queues.\n");
  AQueueModuleInit ();
  dbprintf ('i', "After initializing queues.\n");
  MemoryModuleInit ();
  dbprintf ('i', "After initializing memory.\n");
  ProcessModuleInit ();
  dbprintf ('i', "After initializing processes.\n");
  SynchModuleInit ();
  dbprintf ('i', "After initializing synchronization tools.\n");
  KbdModuleInit ();
  dbprintf ('i', "After initializing keyboard.\n");
  ClkModuleInit();
  for (i = 0; i < 100; i++) {
    buf[i] = 'a';
  }
  i = FsOpen ("vm", FS_MODE_WRITE);
  dbprintf ('i', "VM Descriptor is %d\n", i);
  FsSeek (i, 0, FS_SEEK_SET);
  FsWrite (i, buf, 80);
  FsClose (i);

  // JSM -- commented out for initial build, MUST ADD BACK IN!!
  DfsModuleInit();
  dbprintf ('i', "After initializing dfs filesystem.\n");


  ////////////////////////////////////////
  // JSM -- add debug stuff, and close file system before fdisk app has run
  // FOR TESTING PURPOSES!!


  i = DfsAllocateBlock();
  j = DfsAllocateBlock();
  DfsFreeBlock(i);
  DfsFreeBlock(j);

  dbprintf('F', "ProcessFork: closing filesystem and exiting simulator\n");
  DfsCloseFileSystem();
  exitsim();


  ////////////////////////////////////////

  // Setup command line arguments
  if (userprog != (char *)0) {
    numargs=0;
    allargs_offset = 0;
    // Move through each of the argv addresses
    for(i=0; i<argc-base; i++) {
      // At each argv address, copy the string into allargs, including the '\0'
      for(j=0; allargs_offset < SIZE_ARG_BUFF; j++) {
        allargs[allargs_offset++] = argv[i+base][j];
        if (argv[i+base][j] == '\0') break; // end of this string
      }
      numargs++;
    }
    allargs[SIZE_ARG_BUFF-1] = '\0'; // set last char to NULL for safety
    ProcessFork(0, (uint32)allargs, userprog, 1);
  } else {
    dbprintf('i', "No user program passed!\n");
  }

  // Start the clock which will in turn trigger periodic ProcessSchedule's
  ClkStart();

  intrreturn ();
  // Should never be called because the scheduler exits when there
  // are no runnable processes left.
  GracefulExit();	// NEVER RETURNS!
}
コード例 #3
0
ファイル: traps.c プロジェクト: yfeleke/ece469g05
void GracefulExit() {
  dbprintf('F', "GracefulExit: closing filesystem and exiting simulator\n");
  DfsCloseFileSystem();
  DfsCacheFlush();
  exitsim();
}