Duke CPS 110 - Implementing Processes

Unformatted text preview:

1Implementing ProcessesImplementing ProcessesReview: ThreadsReview: Threadsvsvs. Processes. Processes1. The process is a kernel abstraction for anindependent executing program.includes at least one “thread of control”also includes a private address space (VAS)- VAS requires OS kernel supportoften the unit of resource ownership in kernel- e.g., memory, open files, CPU usage2. Threads may share an address space.Threads have “context” just like vanilla processes.- thread context switch vs. process context switchEvery thread must exist within some process VAS.Processes may be “multithreaded” with threadprimitives supported by a library or the kernel.datadata2QuestionsQuestionsA process is an execution of a program within a privatevirtual address space (VAS).1. What are the system calls to operate on processes?2. How does the kernel maintain the state of a process?Processes are the “basic unit of resource grouping”.3. How is the process virtual address space laid out?What is the relationship between the program and the process?4. How does the kernel create a new process?How to allocate physical memory for processes?How to create/initialize the virtual address space?Nachos Exec/Exit/Join ExampleNachos Exec/Exit/Join ExampleExec parent Exec childJoinExitSpaceID pid = Exec(“myprogram”, 0);Create a new process running theprogram “myprogram”. Note: inUnix this is two separate systemcalls: fork to create the processand exec to execute the program.int status = Join(pid);Called by the parent to wait for achildtoexit,and“reap”itsexitstatus. Note: child may haveexited before parent calls Join!Exit(status);Exit with status, destroyingprocess. Note: this is not theonly way for a proess to exit!.3Mode Changes for Exec/ExitMode Changes for Exec/ExitSyscall traps and “returns” are not always paired.Exec “returns” (to child) from a trap that “never happened”Exit system call trap never returnssystem may switch processes between trap and returnIn contrast, interrupts and returns are strictly paired.ExeccallExecentry touser spaceExitcallExecreturnJoincallJoinreturnparentchildtransition from user to kernel mode (callsys)transition from kernel to user mode (retsys)Exec enters the child bydoctoring up a saved usercontext to “return” through.Process InternalsProcess Internals++user IDprocess IDparent PIDsibling linkschildrenvirtual address space process descriptorresourcesthreadstackEach process has a threadbound to the VAS.The thread has a saved usercontext as well as a systemcontext.The kernel can manipulatethe user context to start thethread in user modewherever it wants.Process state includesa file descriptor table,links to maintain theprocess tree, and aplace to store the exitstatus.The address space isrepresented by pagetable,asetoftranslations to physicalmemory allocated from akernel memory manager.The kernel mustinitialize the processmemory with theprogram image to run.4Review: TheReview: TheVirtual Address SpaceVirtual Address SpaceA typical process VAS space includes:• user regions in the lower halfV->P mappings specific to each processaccessible to user or kernel code• kernel regions in upper halfshared by all processesaccessible only to kernel code• Nachos: process virtual address spaceincludes only user portions.mappings change on each process switchtextdataBSSuser stackargs/env0datakernel textandkernel data2n-12n-10x00xffffffffA VAS for a private address space system (e.g.,Unix) executing on a typical 32-bit architecture.The Birth of a ProgramThe Birth of a Programint j;char* s = “hello\n”;int p() {j=write(1,s,6);return(j);}myprogram.ccompiler…..p:store thisstore thatpushjsr _writeretetc.myprogram.sassembler datamyprogram.olinkerobjectfiledataprogram(executable file)myprogramdatadatadatalibrariesand otherobjects5What’s in an Object File or Executable?What’s in an Object File or Executable?int j = 327;char* s = “hello\n”;char sbuf[512];int p() {int k = 0;j = write(1, s, 6);return(j);}textdataidatawdataheadersymboltablerelocationrecordsUsedbylinker;mayberemoved after final linkstep and strip.Header “magic number”indicates type of image.Section table an arrayof (offset, len, startVA)program sectionsprogram instructionspimmutabledata(constants)“hello\n”writable global/static dataj, sj, s ,p,sbufThe Program and the Process VASThe Program and the Process VAStextdataidatawdataheadersymboltablerelocationrecordsprogramtextdataBSSuser stackargs/envkerneldataprocess VASsectionssegmentsBSS“Block Started by Symbol”(uninitialized global data)e.g., heap and sbuf go here.Args/env strings copiedin by kernel when theprocess is created.Process text segmentis initialized directlyfrom program textsection.Process datasegment(s) areinitialized from idataand wdata sections.Process stack and BSS(e.g., heap) segment(s) arezero-filled.Process BSS segment may beexpanded at runtime with asystem call (e.g., Unix sbrk)called by the heap managerroutines.Text and idata segmentsmay be write-protected.6Review: Virtual AddressingReview: Virtual AddressingtextdataBSSuser stackargs/envkerneldatavirtualmemory(big)physicalmemory(small)virtual-to-physicaltranslationsUser processesaddress memorythrough virtualaddresses.The kernel and themachine collude totranslate virtualaddresses tophysical addresses.The kernel controlsthe virtual-physicaltranslations in effectfor each space.The machine does notallow a user processto access memoryunless the kernel“says it’s OK”.The specific mechanisms for memorymanagement and address translation aremachine-dependent.Memory Management 101Memory Management 101Once upon a time...memory was called “core”, and programs(“jobs”) were loaded and executed one by one.• load image in contiguous physical memorystart execution at a known physical locationallocate space in high memory for stack and data• address text and data using physical addressesprelink executables for known start address• run to completion7Memory and MultiprogrammingMemory and MultiprogrammingOne day, IBM decided to load multiple jobs in memory atonce.• improve utilization of that expensive CPU• improve system throughputProblem 1: how do programs address their memory space?load-time relocation?Problem 2: how does the OS protect memory from rogueprograms????Base and Bound RegistersBase and Bound RegistersGoal: isolate jobs from one another, and from their placementin the machine memory.• addresses are offsets from the job’s base


View Full Document

Duke CPS 110 - Implementing Processes

Download Implementing Processes
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 Implementing Processes 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 Implementing Processes 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?