《进程管理英课件》由会员分享,可在线阅读,更多相关《进程管理英课件(38页珍藏版)》请在金锄头文库上搜索。
1、Chapter 4: ProcessesnProcess ConceptnProcess SchedulingnOperation on ProcessesnCooperating ProcessesnInterprocess CommunicationProcess ConceptnAn operating system executes a variety of programs: nBatch system jobsnTime-shared systems user programs or tasksnTextbook uses the terms job and process alm
2、ost interchangeably. nProcess a program in executionnA process includes:nprogram counternstack ndata sectionProcess Conceptn国内学术界较为一致的定义: 进程是一个具有一定独立功能的程序,关于某个数据集合的一次可以并发执行的运行活动。nFrom Wikipedia In computing, a process is an instance of a computer program that is being sequentially executed by a comp
3、uter system that has the ability to run several computer programs concurrently.Process ConceptnA single computer processor executes one or more instructions at a time (per clock cycle), one after the other.nTo allow users to run several programs at once, single-processor computer systems can perform
4、 multiprogramming and time-sharing. nUsing more than one physical processor on a computer, permits true simultaneous execution of more than one stream of instructions from different processes. nConcurrency is the term generally used to refer to several independent processes sharing a single processo
5、rnSimultaneously (or parallel) is used to refer to several processes, each with their own processor.Process vs. ProgramnDynamic/staticnprocess is a program in execution, so it is dynamic,nprogram is a passive collection of instuctions, so it is a static entity.nLify cyclenprocess has a life cycle. I
6、t is an active entity in the sense that it can be created, executed, and terminated during its lifetime.nProgram can exist forever.nStructurenprocess = program + data + PCBnEach process is represented in the OS by a process control block (PCB), it contains all information associated with a specific
7、process.Process vs. ProgramnNo one-to-one mapping between processes and programsncan have mulitple processes of the same programnone process can invoke multiple programsnProcess is the basic unit of being scheduled and resource allocation from the viewpoint of OS, while program can not be run withou
8、t process creation.nProcess can be executed concurrently or simultaneously.Process StatenAs a process executes, it changes statennew: The process is being created.nrunning: Instructions are being executed.nwaiting: The process is waiting for some event to occur.nready: The process is waiting to be a
9、ssigned to a processor.nterminated: The process has finished execution.Diagram of Process Statenewreadyrunningwaiting/blockedterminatedadmittedI/O or event completionscheduler dispatchI/O or event waitinterruptexitProcess Control Block (PCB)Information associated with each process:nProcess state nPr
10、ogram counternCPU registers nCPU scheduling informationnMemory-management informationnAccounting informationnI/O status informationProcess Control Block (PCB)CPU Switch From Process to ProcessChapter 4: ProcessesnProcess ConceptnProcess SchedulingnOperation on ProcessesnCooperating ProcessesnInterpr
11、ocess CommunicationProcess SchedulingProcess Scheduling QueuesnJob queue set of all processes in the system. nReady queue set of all processes residing in main memory,ready and waiting to execute.nDevice queues set of processes waiting for an I/O device.Process migration between the various queues.
12、Ready Queue And Various I/O Device QueuesRepresentation of Process SchedulingSchedulersnLong-term scheduler (or job scheduler) selects which processes should be brought into the ready queue. 长程调度(或作业调度)长程调度(或作业调度)nShort-term scheduler (or CPU scheduler) selects which process should be executed next
13、and allocates CPU. 短程调度(或短程调度(或CPU调度)调度)Addition of Medium Term SchedulingSchedulers (Cont.)nShort-term scheduler is invoked very frequently (milliseconds) (must be fast). 调用频率高调用频率高nLong-term scheduler is invoked very infrequently (seconds, minutes) (may be slow). 调用频率不高调用频率不高nThe long-term schedul
14、er controls the degree of multiprogramming. 长程调度控制了多道程序的长程调度控制了多道程序的“道数道数”nProcesses can be described as either: nI/O-bound process spends more time doing I/O than computations, many short CPU bursts.nCPU-bound process spends more time doing computations; few very long CPU bursts.Context Switch上下文切换
15、上下文切换nWhen CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.nContext-switch time is overhead; the system does no useful work while switching.nTime dependent on hardware support.Process CreationnParent process creates chil
16、dren processes, which, in turn create other processes, forming a tree of processes. nResource sharingnParent and children share all resources.nChildren share subset of parents resources.nParent and child share no resources.nExecutionnParent and children execute concurrently.nParent waits until child
17、ren terminate.Process Creation (Cont.)nAddress spacenChild duplicate of parent. nChild has a program loaded into it. nUNIX examplesnfork system call creates new processnexeclp system call used after a fork to replace the process memory space with a new program. A Tree of Processes On A Typical UNIX
18、SystemProcess TerminationnProcess executes last statement and asks the operating system to delete it (exit). nProcess resources are deallocated by operating system.nOutput data from child to parent (via wait). nParent may terminate execution of children processes nChild has exceeded超过超过 allocated re
19、sources.nTask assigned to child is no longer required.nParent is exiting. nOperating system does not allow child to continue if its parent terminates. nCascading termination. 级联终止级联终止Cooperating ProcessesnIndependent process cannot affect or be affected by the execution of another process. nCooperat
20、ing process can affect or be affected by the execution of another processnAdvantages of process cooperationnInformation sharingnComputation speed-upnModularitynConvenienceProducer-Consumer ProblemnParadigm for cooperating processes, producer process produces information that is consumed by a consume
21、r process. nunbounded-buffer places no practical limit on the size of the buffer. 无界缓冲没有对缓冲区大小的限制无界缓冲没有对缓冲区大小的限制nbounded-buffer assumes that there is a fixed buffer size. 有界缓冲对缓冲区大小作了限定有界缓冲对缓冲区大小作了限定Bounded-Buffer Shared-Memory SolutionnShared datavar n;type item = ;var buffer. array 0.n1 of item;in
22、, out: 0.n1;nProducer process repeatproduce an item in nextpwhile in+1 mod n = out do no-op;buffer in :=nextp;in :=in+1 mod n;until false;Bounded-Buffer (Cont.)nConsumer process repeatwhile in = out do no-op;nextc := buffer out;out := out+1 mod n;consume the item in nextc until false;nSolution is co
23、rrect, but can only fill up n1 buffer.ThreadsnA thread (or lightweight process) is a basic unit of CPU utilization; it consists of: nprogram counternregister setnstack spacenA thread shares with its peer threads its:ncode sectionndata sectionnoperating-system resourcescollectively know as a task. 整体
24、作为一个任务整体作为一个任务Threads (Cont.)nIn a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run. nCooperation of multiple threads in same job confers higher throughput and improved performance. nApplications that require sharing a common buffer (i.
25、e., producer-consumer) benefit from thread utilization. nThreads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism. nKernel-supported threads (Mach and OS/2). nUser-level threads; supported above the kernel, via a set of library calls
26、 at the user level. nHybrid approach implements both user-level and kernel-supported threads (Solaris 2). 混合处理实现用户级和内核支持线程Interprocess Communication-IPCnMechanism for processes to communicate and to synchronize their actions.nMessage system processes communicate with each other without resorting to
27、shared variables. nIPC facility provides two operationsnsend(message) message size fixed or variablenreceive(message)nIf P and Q wish to communicate, they need to :nestablish a communication link between themnexchange messages via send/receivenImplementation of communication linknphysical (e.g., sha
28、red memory, hardware bus)nlogical (e.g., logical properties)Implementation QuestionsnHow are links established? nCan a link be associated with more than two processes? nHow many links can there be between every pair of communicating processes? nWhat is the capacity of a link? ?nIs the size of a mess
29、age that the link can accommodate fixed or variable? nIs a link unidirectional or bi-directional? Direct CommunicationnProcesses must name each other explicitly显式显式: nsend (P, message) send a message to process Pnreceive(Q, message) receive a message from process QnProperties of communication linknL
30、inks are established automatically. nA link is associated with exactly one pair of communicating processes.nBetween each pair there exists exactly one link.nThe link may be unidirectional, but is usually bi-directional.Indirect CommunicationnMessages are directed and received from mailboxes.nEach ma
31、ilbox has a unique id. nProcesses can communicate only if they share a mailbox. nProperties of communication linknLink established only if processes share a common mailbox nA link may be associated with many processes.nEach pair of processes may share several communication links.nLink may be unidire
32、ctional.nal or bi-directional.nOperationsncreate a new mailboxnsend and receive messages through mailboxndestroy a mailboxTypes of MailboxnPrivate Mailbox: owner-Receive other user-SendnPublic Mailbox: owned by OSnShared Mailbox: created by a process, the ownership and privilege be passed to other p
33、rocessesSynchronizationnBlocking send:sender blocked until the message is receivednNonblocking send:send the message and resume operationnBlocking receive:receiver blocked until a message is availablenNonblocking receive:receiver retrieves either a valid message or nullBufferingnQueue of messages at
34、tached to the link; implemented in one of three ways. 1.Zero capacity 0 messages Sender must wait for receiver. 2.Bounded capacity finite length of n messages Sender must wait if link full. 3.Unbounded capacity infinite length Sender never waits.Exception Conditions Error RecoverynProcess terminates 进程终止进程终止nLost messages 消息丢失消息丢失nScrambled Messages 消息受损消息受损HomeworknP113 n4.1n4.4n4.6