讲解:HW 2、C/C++、C/C++、Nachos Statistics、、|Hask

HW 2Follow the Lab1 instruction and create a new fresh Nachosfolder.OverviewSo far, all the code you have written for Nachos has been partof the operating system kernel (ThreadTest() is compiled and ranwithin Nachos). In a real operating system, the kernel not onlyuses its procedures internally, but allows user-level programsto access some of its routines via “system calls”. In thisproject, you are going to implement several system call routinesfor user programs and round-robin scheduling.The user programs are written in C and located under test/directory. There are several example programs provided,including halt, shell, etc. The command “./nachos -x ../test/programname” will load the program ‘programname’ toNachos memory (see addrspace.cc and progtest.cc) and run it onNachos.Task 1 (30pts) System Calls for user program: userprog/syscall.hdefines the system call prototypes of Nachos. These are kernelprocedures that user programs can invoke. You need to implementthe following system calls. Fork_POS()enables the user program to fork a kernel childthread. It takes integer 1, 2 or 3 as parameter, creates acorresponding kernel thread, which executes one of the testfunctions basing on the parameter value, and returns aninteger as the process ID of the child. For example, whenchild = Fork_POS(2) is called in a user program, a kernelthread will be created in ExceptionHandler() and thisthread will call the ForkTest2 function (using theFork()call of thread.cc). The child process ID will beassigned to child.void ForkTest1(int id){printf(ForkTest1 is called, its PID is %d\n, id);for (int i = 0; i {printf(ForkTest1 is in loop %d\n, i);for (int j = 0; j kernel->interrupt->OneTick();}Exit_POS(id);}void ForkTest2(int id){printf(ForkTest2 is called, its PID is %d\n, id);for (int i = 0; i {printf(ForkTest2 is in loop %d\n, i);for (int j = 0; j kernel->interrupt->OneTick();2? Wait_POS() provides the routine for the user program towait for the execution of its child thread. It takes thechild process ID as parameter. The parent will be put intosleep until the child awakens it. The child wakes up theparent when it finishes by calling Exit_POS, which you’llimplement. Wait_POS() and Exit_POS() should be able tohandle certain errors such as invalid child process ID orno parent process waiting. Exit_POS() is called by a child and wakes up its parent.This function is not a system call but you need toimplement in exception.ccFor this task, syscall.h, exception.cc, start.s (assemblyroutine for system calls and under test/ directory), andscheduler.cc must be modified. The userprog/exception.ccimplements the handlers for system calls and other user-levelexceptions. In the original file, only the Halt() system call issupported.Hint: you can find a good example from system call Add.You can find test programs (prog1.c, prog2.c and prog3.c) in theTesting section. For this task testing, you don’t needmultitasking with round-robin.Task 2 (20pts) Multitasking (running multiple user-levelprograms): when the ‘-x’ flag is used, the ‘main’ function ofNachos calls the RunUserProg() function with the stringfollowing ‘-x’ as parameter. Remember that every user programshould have its own thread structure, but for now there’s onlyone thread, the ‘main’ thread.The goal of this task is to make the command./nachos -x ../test/mprog1 -x ../test/mprog2work in Nachos. What this means is that when you type thiscommand, Nachos will load multiple user programs into its mainmemory and start executing them as threads with round-robinscheduling. You need to implement Write system call. WriteHW 2留学生作业代做、代写C/C++语言作业、代做C/C++程序设计作业、代写Nachos作业 代写留学生 Stati()writes string to a specified destination. We’refaking it using printf, therefore it always writes to thescreen. This system call takes three arguments: a char*buffer, an int size, and an OpenFileId. Since we’re fakingit, we can ignore the OpenFileId parameter. You need toobtain the first two arguments from the user program andprint an character at a time using printf.3Task 3 (45pts) Memory Allocation (Address Space for userprograms): The main issue in this implementation is how tocreate the address spaces of the Nachos processes and put themall in the main memory without overlapping. The allocation ofaddress space is done in userprog/addrspace.cc.If the total size of the user programs (forexample, ./nachos -x ../test/prog1 -x ../test/prog2 …) aresmaller than the memory size, you can just use contiguous memoryallocation. However, for the command like ./nachos -x ../test/matmult, you need to consider larger memory allocationneeded than Nachos physical memory size. matmult is a matrixmultiplication program. If you run the program now, you’ll getan assertion failure in userprog/addrspace.cc because there’snot enough memory to load it. So what you need to accomplish isto let Nachos run the program with part of the address spaceloaded, and load the other part(s) when they are accessed.Because an exception will arise when they are accessed, theloading should be done as an exception handling inuserprog/exception.cc.You’re free to use whatever design you choose. For the first case, you can use the page table and assumethe memory allocation is contiguous by keeping track of thefirst free page frame to load a given user program’saddress space. For the second case, you can store the whole address spacein a file, and load the accessed part every time anexception arises.Task 4 (5pts) Round-robin scheduling: Round-robin schedulinggives each process (Nachos thread) a fix time slice (quantum) torun. After the quantum expires, the CPU will be taken from theprocess and given to the next process in the ready list. Theolder process is then inserted to the end of the ready list. You already added the flag “quantum” from the programmingassignment 1, i.e., when you type the command “./nachos -quantum 200”, Nachos will run with the quantum set to 200clock ticks.4Testing:We will build and run your Nachos on the VM. TAs will testmultitasking with user programs with their quantum size andmemory allocation using matmult program.Place the following programs into test/ directory and updateMakefile. The test programs must be compiled before Nachos isbuilt.// Task 1 testing programs/*** prog1.c ***/#include syscall.hintmain(){int child;child = Fork_POS(1);Wait_POS(child);child = Fork_POS(2);Wait_POS(child);child = Fork_POS(3);Wait_POS(child);Exit(0);}/*** prog2.c ***/#include syscall.hintmain(){int child1, child2;child1 = Fork_POS(1);child2 = Fork_POS(2);Wait_POS(child1);Wait_POS(child2);Exit(0);}/*** prog3.c ***/#include syscall.hintmain(){Fork_POS(1);Fork_POS(2);Fork_POS(3);Exit(0);}5// mprog1.c for Task 2, 3 and 4 testing#include syscall.hintmain(){OpenFileId output = ConsoleOutput;char* str = current user program: prog1;int i,j;for (i = 0; i {Write(str, 27, output);for (j = 0; j }Exit(0);}// mprog2.c for Task 2, 3 and 4 testing#include syscall.hintmain(){OpenFileId output = ConsoleOutput;char* str = current user program: prog2;int i,j;for (i = 0; i {Write(str, 27, output);for (j = 0; j }Exit(0);}转自:http://ass.3daixie.com/2018110830818417.html

你可能感兴趣的:(讲解:HW 2、C/C++、C/C++、Nachos Statistics、、|Hask)