progtest.cc
来自「操作系统课程设计。在UNIX平台下实现Solary操作系统的一些功能」· CC 代码 · 共 125 行
CC
125 行
/////////////////////////////////////////////////////////////
//FileName : progtest.cc
//
//Creator : Li Shouchao(0410716)
//CreateTime : 2007-1-3
//
//File Desc:
// 1.Test routines for demonstrating that Nachos can load a user program and execute it.
// 2.Also, routines for testing the Console hardware device.
/////////////////////////////////////////////////////////////#include "copyright.h"#include "system.h"#include "console.h"#include "addrspace.h"#include "synch.h"//----------------------------------------------------------------------// StartProcess// Run a user program. Open the executable, load it into// memory, and jump to it.//----------------------------------------------------------------------int numOfProc = 1;voidStartProcess(char *filename){ OpenFile *executable = new OpenFile(filename); AddrSpace *space; if (executable == NULL) { printf("Unable to open file %s\n", filename); return; } space = new AddrSpace(executable); currentThread->space = space; delete executable; space->InitRegisters(); space->RestoreState(); machine->Run(); ASSERT(FALSE);}
//////////////////////////////////////////////////////
// Function name : LoadProc
// Creator : Fang Wenbin(0410706)
// CreateTime : 2007-1-19 22:01:02
//
// Function Desc : load user program into memory
// The steps of LoadProc
// 1. load file from disk to OpenFile structure
// 2. allocate user space
// 3. print page numbers that program covers
// Fails if:
// 1.
// 2.
// 3.
// Note :
// Return type : void
// Argument : char *filename
//////////////////////////////////////////////////////
void LoadProc(char *filename){ OpenFile *executable = new OpenFile(filename); AddrSpace *space; if (executable == NULL) { printf("Unable to open file %s\n", filename); return; } //end if (executable...) space = new AddrSpace(executable); currentThread->space = space; delete executable; printf("Proc %d covers pages:", numOfProc); for (int i = 0; i < space->numPages; i++) printf("%d ", space->pageTable[i].physicalPage); putchar('\n'); numOfProc++;}// Data structures needed for the console test. Threads making// I/O requests wait on a Semaphore to delay until the I/O completes.static Console *console;static Semaphore *readAvail;static Semaphore *writeDone;//----------------------------------------------------------------------// ConsoleInterruptHandlers// Wake up the thread that requested the I/O.//----------------------------------------------------------------------static void ReadAvail(int arg) { readAvail->V(); }static void WriteDone(int arg) { writeDone->V(); }//----------------------------------------------------------------------// ConsoleTest// Test the console by echoing characters typed at the input onto// the output. Stop when the user types a 'q'.//----------------------------------------------------------------------void ConsoleTest (char *in, char *out){ char ch; console = new Console(in, out, ReadAvail, WriteDone, 0); readAvail = new Semaphore("read avail", 0); writeDone = new Semaphore("write done", 0); for (;;) { readAvail->P(); // wait for character to arrive ch = console->GetChar(); console->PutChar(ch); // echo it! writeDone->P() ; // wait for write to finish if (ch == 'q') return; // if q, quit }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?