DOC PREVIEW
Duke CPS 110 - Lecture 10

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Duke University • Department of Computer Science1Nachos in CPS110Labs 1-3 use Nachos as a thread library.• Lab 1: understanding races.• Lab 2: implement and use synchronization primitives.• Lab 3: complex synchronization.Labs 4-6 use Nachos as an operating system.• Lab 4: concurrent user programs.• Lab 5: I/O with files and pipes.• Lab 6: virtual memory.Duke University • Department of Computer Science2What is Nachos?What is an operating system?• “friendly” interface between user programs (Powerpoint) and hardware (x86, IDE disk, video card, etc).User ApplicationsOperating SystemArchitectureDuke University • Department of Computer Science3Nachos looks, feels, and crashes like a “real” OS.• Both the Nachos “OS” and test programs run together as an ordinary process on an ordinary Unix system (Solaris).What is Nachos? (reality)User ApplicationsOperating SystemArchitectureNachos “OS”Solaris OSArchitectureMIPS User AppsDuke University • Department of Computer Science4What is Nachos? (for us)User ApplicationsOperating SystemArchitectureNachos “OS”Solaris OSArchitectureMIPS User AppsNachos runs real user programs on a simulated machine.• Nachos MIPS simulator executes real user programs.• The real OS is treated as part of the hardware.Duke University • Department of Computer Science5Look familiar?Java Virtual MachineSolaris OSArchitectureJava User AppsNachos “OS”Solaris OSArchitectureMIPS User AppsDuke University • Department of Computer Science6Introducing User Programs into NachosSPARC HWOS KernelMIPS simNachosUser ProgramsSyscallsMachine instructionsMIPS instrNachos callsUser ProcessInternalNachos threadConceptually:Nachos thread encapsulates user program, remains the schedulable entityDuke University • Department of Computer Science7Nachos Systems Call (Process)userprog/syscall.hSpaceid Exec (char *name, int argc, char** argv, int pipectrl) -Creates a user process by creating a new address space, reading the executable file into it, and creating a new internal thread (via Thread::Fork ) to run it. To start execution of the child process, the kernel sets up the CPU state for the new process and then calls Machine::Run to start the machine simulator executing the specified program's instructions in the context of the newly created child process. Exit (int status) -user process quits with status returned. The kernel handles an Exit system call by destroying the process data structures and thread(s), reclaiming any memory assigned to the process, and arranging to return the exit status value as the result of the Join on this process, if any. Join (Spaceid pid) - called by a process (the joiner) to wait for the termination of the process (the joinee) whose SpaceId is given by the pid argument. If the joinee is still active, then Join blocks until the joinee exits. When the joinee has exited, Join returns the joinee's exit status to the joiner.Duke University • Department of Computer Science8StartProcess(char *filename){OpenFile *executable = fileSystem->Open(filename);AddrSpace *space;if (executable == NULL) {printf("Unable to open file %s\n", filename);return;}space = new AddrSpace(executable);currentThread−>space = space;delete executable; // close filespace−>InitRegisters(); // set the initial register valuesspace−>RestoreState(); // load page table registermachine−>Run(); // jump to the user progamASSERT(FALSE); // machine->Run never returns;// the address space exits// by doing the syscall "exit"}−> ExecDuke University • Department of Computer Science9ExceptionHandler(ExceptionType which){int type = machine->ReadRegister(2);if ((which == SyscallException) && (type == SC_Halt)) {DEBUG('a', "Shutdown, initiated by user program.\n");interrupt->Halt();} else {printf("Unexpected user mode exception %d %d\n", which, type);ASSERT(FALSE);}} SPARC HWOS KernelMIPS simNachosUser ProgramsSyscallsMachine instructionsMIPS instrExceptionHandlerNote: system call code must convert user-space addresses to Nachos machine addresses or kernel addresses before they can be dereferencedDuke University • Department of Computer Science10Lab 4: Fun with AddrSpaceLab 4: concurrent processes with Exec, Exit, Join.• Nachos already provides:– Process abstraction (Thread + AddrSpace).– Context switching (swap registers, page tables).– Timeslicing.• But:– AddrSpace (as provided) allows only one process.Up next:• executable, address space review.• “Fixing” AddrSpace.Duke University • Department of Computer Science11Executable into AddrSpacetextidatawdatasymbol tablereloc recordsExecutable:headertextdatabssuser stackAddress Space:textdatabssuser stackPhysical Memory:freeDuke University • Department of Computer Science12AddrSpace::AddrSpace(OpenFile *executable){ ...executable->ReadAt((char *)&noffH, sizeof(noffH), 0);if ((noffH.noffMagic != NOFFMAGIC) && (WordToHost(noffH.noffMagic) == NOFFMAGIC)) SwapHeader(&noffH);ASSERT(noffH.noffMagic == NOFFMAGIC);// how big is address space?size = noffH.code.size + noffH.initData.size + noffH.uninitData.size + UserStackSize; // we need to increase the size to leave room for the stacknumPages = divRoundUp(size, PageSize);size = numPages * PageSize;ASSERT(numPages <= NumPhysPages); // check we're not trying// to run anything too big --// at least until we have virtual memoryDuke University • Department of Computer Science13// first, set up the translationpageTable = new TranslationEntry[numPages];for (i = 0; i < numPages; i++) {pageTable[i].virtualPage = i; // for now, virtual page # = phys page #pageTable[i].physicalPage = i;pageTable[i].valid = TRUE;pageTable[i].use = FALSE;pageTable[i].dirty = FALSE;pageTable[i].readOnly = FALSE; // if the code segment was entirely on// a separate page, we could set its// pages to be read-only}// zero out the entire address space, to zero the unitialized data segment// and the stack segment// bzero(machine->mainMemory, size); rm for Solarismemset(machine->mainMemory, 0, size);Duke University • Department of Computer Science14// then, copy in the code and data segments into memoryif (noffH.code.size > 0) {DEBUG('a', "Initializing code segment, at 0x%x, size %d\n",noffH.code.virtualAddr, noffH.code.size);executable->ReadAt(&(machine->mainMemory[noffH.code.virtualAddr]),noffH.code.size, noffH.code.inFileAddr);}if (noffH.initData.size > 0) {DEBUG('a', "Initializing data segment,


View Full Document

Duke CPS 110 - Lecture 10

Download Lecture 10
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?