资源预览内容
第1页 / 共36页
第2页 / 共36页
第3页 / 共36页
第4页 / 共36页
第5页 / 共36页
第6页 / 共36页
第7页 / 共36页
第8页 / 共36页
第9页 / 共36页
第10页 / 共36页
亲,该文档总共36页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Introduction to Operating Systems Lecture 2: Process and ThreadQIAN Weining wnqiansei.ecnu.edu.cn Institute of Massive Computing East China Normal University28/02/08ossei.ecnu2OutlineMotivation: overview of a running programWhat is a process?: from a VM perspectiveState transition of a processProcess schedulingCollaborating processesThreads (vs. processes)Thread schedulingKernel and user mode threads28/02/08ossei.ecnu3Overview of a running programExecution sequence:1. Fetch instruction at PC (to IF)2. Decode3.Execute (possibly using registers)4.Write result to registers/memory5. PC usermode switch: kernel=user)4. met other interrupts or system calls5. .6. reload state from PCB17. .28/02/08ossei.ecnu10Issues of multiprogrammingScheduling: multiplex in time to different processesAt most one active process on a CPU at a timeWhoWho? and How longHow long?Protection: allocate resources to processesAccess controlprocess no.registersprocess counterprocess state.memory limitslist of open filesPCBPCB28/02/08ossei.ecnu11State transition: What? and When?newreadyrunningwaitingterminatedadmitted exitinterruptscheduler dispatch i/o or event completion i/o or event wait28/02/08ossei.ecnu12State transition: When?Process creationProcess creationParent process creates children ones (tree of processes)Process terminationProcess terminationLast statement is executed and the process asks the OS to delete it (exit() orIts parent wants to terminate it (abort()maybe because of cascading terminationcascading termination28/02/08ossei.ecnu13State transition: How?Process creationProcess creationConstruction of the new PCB (inexpensive)Setting up the mapping for address space (more expensive)Copy data from parent process (fork() in unix) (very expensive)exec() in unix after a fork() replace the memory space with a new program“copy on write” is much less expensive (to be visited later)Copy I/O state (medium expensive)28/02/08ossei.ecnu14State transition: How?Process terminationProcess terminationoutput data from child to parentOS deallocate resourcesProcess schedulingProcess schedulingLong-term scheduling (job scheduling)Short-term scheduling (CPU scheduling)Medium-term schedulingProcess scheduling queuesJob queueReady queueDevice queues28/02/08ossei.ecnu15Process schedulingready queueCPUi/o queuei/o requesttime slice expiredfork()wait for an interruptI/Ochild executesinterrupt occurs28/02/08ossei.ecnu16Process schedulingLong-term schedulingLong-term schedulingwho should be brought into the ready queue (from job queue)Short-term schedulingShort-term schedulingwho should be executed on CPU next (from ready queue)Medium-term schedulingMedium-term scheduling (to be visited later)who should be swapped out/in?28/02/08ossei.ecnu17Collaborating processesWe may use multiple processes to collaborate on a task, but:with high creation/memory overheadrelatively high context-switch overheadneed additional communication mechanismsince address spaces are isolatedWe may use shared memory mappingshared memory mapping or message message passingpassing28/02/08ossei.ecnu18Shared memory mappingtextdataheapstack.textdataheapstack.sharedshared28/02/08ossei.ecnu19Inter-process communication (IPC)WhyWhy?: communicate or synchronize between processesWhatWhat?: two operations:send(message)receive(message)HowHow?:establish a communication linkcommunication linksend/receiveHow to implementHow to implement?physical vs. logical28/02/08ossei.ecnu20Threads: Why?Heavy weight process revisited: a thread a thread (lightweight process) + address space(lightweight process) + address spaceA single, sequential stream of executionA single, sequential stream of execution in its own address spaceMulti-threading (multi-tasking)Multi-threading (multi-tasking): a single program with a number of different concurrentconcurrent activitiesSeparate concurrencyconcurrency (thread) problem from protectionprotection (address space)28/02/08ossei.ecnu21Thread: Why?Embedded systemsPCDB servers: thread poolsNetwork serversParallel programming28/02/08ossei.ecnu22registers, stackThread: What?single thread processcode, data, filesregisters, stackmulti-thread processcode, data, filesregisters, stackregisters, stack28/02/08ossei.ecnu23Thread: What?State shared by all threads in a State shared by all threads in a process/address spaceprocess/address spacecontents of memory (global variables, heap)I/O stateState private to each threadState private to each threadCPU registers (and PC)Execution stack: for parameters, temporary variables, and return PCsStored in Thread Control Block (TCB)28/02/08ossei.ecnu24Process-thread relationship classificationembedded systems java os pilotmach, os/2, linux windows 9x windows nt (xp, vista, .) modern unix: solaris, hp-ux, .dos, early macintosht
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号