Module4Processes进程

上传人:m**** 文档编号:568009130 上传时间:2024-07-23 格式:PPT 页数:58 大小:627.50KB
返回 下载 相关 举报
Module4Processes进程_第1页
第1页 / 共58页
Module4Processes进程_第2页
第2页 / 共58页
Module4Processes进程_第3页
第3页 / 共58页
Module4Processes进程_第4页
第4页 / 共58页
Module4Processes进程_第5页
第5页 / 共58页
点击查看更多>>
资源描述

《Module4Processes进程》由会员分享,可在线阅读,更多相关《Module4Processes进程(58页珍藏版)》请在金锄头文库上搜索。

1、4.1Module 4: Processes进程进程Process Concept 进程概念进程概念Process Scheduling 进程调度进程调度Operation on Processes 进程上的操作进程上的操作Cooperating Processes 协同进程协同进程Interprocess Communication 进程间通信进程间通信4.2Course OverviewGoals:Understanding of OS and the OS/architecture interface/interactionPrerequisites:Data structure, co

2、mpilerWhat to expect:We will cover core concepts and issues in lecturesIn sections, you and your TA will practice paper & pencil problems and talk about the projectA significant project(32 学时学时)1 Midterms and 1 Final4.3Textbook and TopicsOperating System Concepts (6th Edition), Silberschatz and Galv

3、in, ISBN: 0-471-41743-2Operating Systems (4th Edition), Stallings, ISBN: 013-031-999-6 (recommand)Operating systems design and implement,Andrew S.TanenbaumTopicsProcesses and threadsProcessor schedulingSynchronizationVirtual memoryFile systemsI/O systems4.4TodayProcess Concept 进程概念进程概念Process Schedu

4、ling 进程调度进程调度Operation on Processes 进程上的操作进程上的操作Cooperating Processes 协同进程协同进程Interprocess Communication 进程间通信进程间通信Silberschatz Chapter 4 (4.1-4.4)4.5What Is An Operating System?A software layer between the hardware and the application programs/users which provides a virtual machine interface: easy

5、and safeA resource manager that allows programs/users to share the hardware resources: fair and efficientA set of utilities to simplify application developmenthardwareoperating systemapplication (user)4.6Abstract View of System Components4.7Why Do We Want An OS?Benefits for application writersEasier

6、 to write programsTSee high level abstractions instead of low-level hardware detailsE.g. files instead of disk blocksPortabilityBenefits for usersEasier to use computersTCan you imagine trying to use a computer without the OS?SafetyTOS protects programs from each otherTOS protects users from each ot

7、her4.8Basic computer structureCPUMemorymemory busI/O busdiskNet interface4.9System Abstraction: ProcessesA process is a system abstraction:illusion of being the only job in the system hardware: computeroperating system: processuser: run applicationcreate, kill processes,inter-process comm.Multiplexi

8、ng resources4.10Processes: Mechanism and PolicyMechanism:Creation, destruction, suspension, context switch, signaling, IPC, etc.Policy:Minor policy questions:TWho can create/destroy/suspend processes?THow many active processes can each user have?Major policy question that we will concentrate on:THow

9、 to share system resources between multiple processes?TTypically broken into a number of orthogonal policies for individual resource such as CPU, memory, and disk.4.11Processor Abstraction: ThreadsA thread is a processor abstraction: illusion of having 1 processor per execution context hardware: pro

10、cessoroperating system: threadapplication: execution contextcreate, kill, synch.context switch4.12Threads: Mechanism and PolicyMechanism:Creation, destruction, suspension, context switch, signaling, synchronization, etc.Policy:How to share the CPU between threads from different processes?How to shar

11、e the CPU between threads from the same process?How can multiple threads synchronize with each other?How to control inter-thread interactions?TCan a thread murder other threads at will?4.13Process Concept进程概念进程概念An operating system executes a variety of programs: 操作系统执行各种程序操作系统执行各种程序Batch system job

12、s 批处理系统批处理系统 - 作业作业Time-shared systems user programs or tasks 分时系统分时系统 - 用户程序或任务用户程序或任务Textbook uses the terms job and process almost interchangeably. 本书使用的名词作业和进程,基本可互换本书使用的名词作业和进程,基本可互换Process a program in execution; process execution must progress in sequential fashion. 进程进程 - 在执行中的程序;进程的执行必须以顺序方

13、式进行在执行中的程序;进程的执行必须以顺序方式进行A process includes: 一个进程包括一个进程包括program counter 程序计数器程序计数器stack 栈栈data section 数据部分数据部分4.14Run Time Storage OrganizationEach variable must be assigned a storage classGlobal (static) variablesAllocated in globals region at compile-timeMethod local variables and parameters 函数和

14、系统调用函数和系统调用Allocate dynamically on stackDynamically created objects (using new)数据空间数据空间Allocate from heapObjects live beyond invocation of a methodGarbage collected when no longer “live”Code 代码GlobalsStackHeap 数据Memory4.15Why Did We Talk About All That Stuff?Process = system abstraction for the set

15、of resources required for executing a program = a running instance of a program = memory image + registers content (+ I/O state)The stack + registers content represent the execution context or thread of control4.16What About The OS?Recall that one of the function of an OS is to provide a virtual mac

16、hine interface that makes programming the machine easierSo, a process memory image must also contain the OSOSCodeGlobalsStackHeapMemoryCodeGlobalsStackHeapOS data space is used to store thingslike file descriptors for files beingaccessed by the process, status of I/Odevices, etc.4.17What Happens Whe

17、n There Are More Than One Running Process?OSCodeGlobalsStackHeapP0P1P24.18Process State进程状态进程状态As a process executes, it changes state 进程执行时,改变状态进程执行时,改变状态new: The process is being created. 新建:在创建进程新建:在创建进程running: Instructions are being executed. 运行:指令在执行运行:指令在执行waiting: The process is waiting for

18、some event to occur. 等待:进程等待某些事件发生等待:进程等待某些事件发生ready: The process is waiting to be assigned to a processor. 就绪:进程等待分配处理器就绪:进程等待分配处理器terminated: The process has finished execution. 终止:进程执行完毕终止:进程执行完毕4.19Diagram of Process State进程状态图进程状态图4.20Process Control Block (PCB)进程控制块进程控制块The OS manage informati

19、on for each process in each process tableInformation associated with each process.同进程有关的信息同进程有关的信息Process state 进程状态进程状态Program counter程序计数器程序计数器CPU registers CPU寄存器寄存器CPU scheduling information CPU调度信息调度信息Memory-management information内存管理信息内存管理信息Accounting information计账信息计账信息I/O status information

20、I/O状态信息状态信息4.21Process Control Block (PCB)进程控制块进程控制块4.22CPU Switch From Process to Process进程间进程间CPU的切换的切换4.23Process Scheduling Queues进程调度队列进程调度队列Job queue set of all processes in the system. 作业队列作业队列 - 在系统中的所有进程的集合在系统中的所有进程的集合Ready queue set of all processes residing in main memory,ready and waitin

21、g to execute. 就绪队列就绪队列 - 在主内存中的,就绪并等待执行的所有进程的集合在主内存中的,就绪并等待执行的所有进程的集合Device queues set of processes waiting for an I/O device. 设备队列设备队列 - 等待某一等待某一I/O设备的进程队列设备的进程队列Process migration between the various queues. 在各种队列之间进程的迁移在各种队列之间进程的迁移4.24Ready Queue And Various I/O Device Queues就绪队列和各种就绪队列和各种I/O设备队列设

22、备队列4.25Representation of Process Scheduling进程调度的描述进程调度的描述4.26Schedulers调度调度Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue. 长程调度(或作业调度)长程调度(或作业调度)- 选择可以进入就绪队列的进程选择可以进入就绪队列的进程Short-term scheduler (or CPU scheduler) selects which process should be

23、executed next and allocates CPU. 短程调度(或短程调度(或CPU调度)调度)- 选择可被下一个执行并分配选择可被下一个执行并分配CPU的进程的进程4.27Addition of Medium Term Scheduling中程调度中程调度4.28Schedulers调度调度(Cont.)Short-term scheduler is invoked very frequently (milliseconds) (must be fast). 短程调度切换频率高短程调度切换频率高Long-term scheduler is invoked very infrequ

24、ently (seconds, minutes) (may be slow). 长程调度不快长程调度不快The long-term scheduler controls the degree of multiprogramming. 长程调度控制了多道程序的长程调度控制了多道程序的“道道”Processes can be described as either: 进程可以用下列方式描述:进程可以用下列方式描述:I/O-bound process spends more time doing I/O than computations, many short CPU bursts. I/O型进程

25、型进程 - 花费花费I/O 时间多于计算,许多短时间多于计算,许多短CPU处理处理CPU-bound process spends more time doing computations; few very long CPU bursts. CPU 型进程型进程 - 花费更多时间于计算,许多长花费更多时间于计算,许多长CPU处理处理4.29Context Switch上下文切换上下文切换When CPU switches to another process, the system must save the state of the old process and load the sav

26、ed state for the new process. 当当CPU切换至另一个进程时,系统必须保存旧进程状态并为新进程切换至另一个进程时,系统必须保存旧进程状态并为新进程调入所保留的状态调入所保留的状态Context-switch time is overhead; the system does no useful work while switching. 上下文切换的时间开销较重;在切换时,系统没有做有用的工作上下文切换的时间开销较重;在切换时,系统没有做有用的工作Time dependent on hardware support. 时间取决于硬件的支持时间取决于硬件的支持4.30

27、Process Creation进程创建进程创建Parent process creates children processes, which, in turn create other processes, forming a tree of processes. 父进程创建子进程,如此轮流创建进程下去,构成一个进程树父进程创建子进程,如此轮流创建进程下去,构成一个进程树Resource sharing 资源共享资源共享Parent and children share all resources. 父进程子进程共享所有的资源父进程子进程共享所有的资源Children share subs

28、et of parents resources. 子进程共享父进程资源的子集子进程共享父进程资源的子集Parent and child share no resources. 父进程和子进程无资源共享父进程和子进程无资源共享Execution执行执行Parent and children execute concurrently. 父进程和子进程并发执行父进程和子进程并发执行Parent waits until children terminate. 父进程等待,直到子进程终止父进程等待,直到子进程终止4.31Process Creation (Cont.)进程创建进程创建Address sp

29、ace 地址空间地址空间Child duplicate of parent. 子女复制双亲子女复制双亲Child has a program loaded into it. 子女有一个程序被调子女有一个程序被调入入UNIX examples UNIX例子例子fork system call creates new process fork 系统调用创建新进程系统调用创建新进程execve system call used after a fork to replace the process memory space with a new program. 在在fork 用一个新程序替代了进程

30、的内存空间之后,采用用一个新程序替代了进程的内存空间之后,采用execve系统调用系统调用4.32 Process CreationHow to create a process? System call.In UNIX, a process can create another process using the fork() system callint pid = fork();/* this is in C */The creating process is called the parent and the new process is called the childThe chi

31、ld process is created as a copy of the parent process (process image and process control structure) except for the identification and scheduling stateParent and child processes run in two different address spacesBy default, theres no memory sharingProcess creation is expensive because of this copyin

32、gThe exec() call is provided for the newly created process to run a different program than that of the parent4.33Process Creationfork()fork() codeexec()PCBs4.34Example of Process Creation Using ForkThe UNIX shell is command-line interpreter whose basic purpose is for user to run applications on a UN

33、IX systemcmd arg1 arg2 . argn4.35A Tree of Processes On A Typical UNIX System典型典型UNIX系统中的进程树系统中的进程树4.36Process Termination进程终止进程终止Process executes last statement and asks the operating system to decide it (exit).进程执行的最后一项并询问操作系统作出决定(退出)进程执行的最后一项并询问操作系统作出决定(退出) Output data from child to parent (via w

34、ait). 从子进程向父进程输出数据)(通过等待)从子进程向父进程输出数据)(通过等待)Process resources are deallocated by operating system. 操作操作系统收回进程的资源系统收回进程的资源Parent may terminate execution of children processes (abort). 父进程可中止子进程的执行(终止)父进程可中止子进程的执行(终止)Child has exceeded allocated resources.子进程超量分配资源子进程超量分配资源Task assigned to child is no

35、 longer required. 赋予子进程的任务不再需要赋予子进程的任务不再需要Parent is exiting. 父进程父进程TOperating system does not allow child to continue if its parent terminates. 若父进程终止,不允许子进程继续若父进程终止,不允许子进程继续TCascading termination. 级联终止级联终止4.37Process Creation/Destruction in JavaIn Java, this API is hidden in the java.lang.Runtime a

36、nd java.lang.Process classesimport java.lang.*Runtime rt = Runtime.getRuntime();/ get runtimeProcess child = rt.exec(“loop-forever-program”);/ create childchild.destroy();/ kill child4.38Cooperating Processes协同进程协同进程Independent process cannot affect or be affected by the execution of another process

37、. 独立进程不会影响另一个进程的执行或被另一个进程执行影响独立进程不会影响另一个进程的执行或被另一个进程执行影响Cooperating process can affect or be affected by the execution of another process 协同进程可能影响另一个进程的执行或被另一个进程执行影响协同进程可能影响另一个进程的执行或被另一个进程执行影响Advantages of process cooperation 进程协同的优点进程协同的优点Information sharing 信息共享信息共享Computation speed-up 加速运算加速运算Mod

38、ularity 模块化模块化Convenience 方便方便4.39Producer-Consumer Problem生产者生产者-消费者问题消费者问题Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. 生产者进程生产供消费者进程消费的信息生产者进程生产供消费者进程消费的信息unbounded-buffer places no practical limit on the size of the buffer. 无界缓冲

39、没有对缓冲区大小的限制无界缓冲没有对缓冲区大小的限制bounded-buffer assumes that there is a fixed buffer size. 有界缓冲对缓冲区大小作了限定有界缓冲对缓冲区大小作了限定4.40Bounded-Buffer Shared-Memory SolutionShared data#define BUFFER_SIZE 10Typedef struct . . . item;item bufferBUFFER_SIZE;int in = 0;int out = 0;Solution is correct, but can only use BUFF

40、ER_SIZE-1 elements4.41Bounded-Buffer Producer Process item nextProduced;while (1) while (in + 1) % BUFFER_SIZE) = out); /* do nothing */bufferin = nextProduced;in = (in + 1) % BUFFER_SIZE;4.42Bounded-Buffer Consumer Processitem nextConsumed;while (1) while (in = out); /* do nothing */nextConsumed =

41、bufferout;out = (out + 1) % BUFFER_SIZE; 4.43Interprocess Communication (IPC)进程间通信进程间通信Mechanism for processes to communicate and to synchronize their actions.用于进程通信的机制,同步其间的活动用于进程通信的机制,同步其间的活动Message system processes communicate with each other without resorting to shared variables. 消息系统消息系统 - 进程间通

42、信无须对共享变量进行再分类进程间通信无须对共享变量进行再分类IPC facility provides two operations IPC提供两个操作提供两个操作 :send(message) message size fixed or variable 发送发送 - 固定或可变大小消固定或可变大小消息息receive(message)接受接受If P and Q wish to communicate, they need to 若若P与与Q要通信,需要要通信,需要:establish a communication link between them 建立通信连接建立通信连接exchan

43、ge messages via send/receive 通过通过send/receive交换消息交换消息Implementation of communication link 通信连接的实现通信连接的实现physical (e.g., shared memory, hardware bus)物理的(如,共享存储,硬物理的(如,共享存储,硬件总线)件总线)logical (e.g., logical properties)逻辑的(如,逻辑特性)逻辑的(如,逻辑特性)4.44Implementation Questions实现中的问题实现中的问题How are links establishe

44、d? 连接如何建立?连接如何建立?Can a link be associated with more than two processes? 连接可同多于两个的进程相关吗?连接可同多于两个的进程相关吗?How many links can there be between every pair of communicating processes? 每对在通信进程有多少连接?每对在通信进程有多少连接?What is the capacity of a link? 一个连接的容量是多少?一个连接的容量是多少?Is the size of a message that the link can

45、accommodate fixed or variable? 连接可使用的固定或可变消息的大小?连接可使用的固定或可变消息的大小?Is a link unidirectional or bi-directional? 连接是无向的还是双连接是无向的还是双向的?向的?4.45Direct Communication直接通信直接通信Processes must name each other explicitly: 进程必须显式的命名进程必须显式的命名send (P, message) send a message to process P向进程向进程P发消息发消息receive(Q, messa

46、ge) receive a message from process Q从进程从进程Q收消息收消息Properties of communication link 通信连接的特性通信连接的特性Links are established automatically. 连接自动建立连接自动建立A link is associated with exactly one pair of communicating processes. 连接精确的与一对在通信的进程相关连接精确的与一对在通信的进程相关Between each pair there exists exactly one link. 在每一

47、对之间就存在一个连接在每一对之间就存在一个连接The link may be unidirectional, but is usually bi-directional. 连接可以无向,但通常是双向的连接可以无向,但通常是双向的4.46Indirect Communication间接通信间接通信Messages are directed and received from mailboxes (also referred to as ports).消息导向至信箱并从信箱接收(被视作端口)消息导向至信箱并从信箱接收(被视作端口)Each mailbox has a unique id. 每一个信

48、箱有一个唯一的每一个信箱有一个唯一的idProcesses can communicate only if they share a mailbox. 仅当共享一个信箱时进程才能通信仅当共享一个信箱时进程才能通信Properties of communication link 通信连接的特性通信连接的特性Link established only if processes share a common mailbox 仅当进程共有一个信箱时连接才能建立仅当进程共有一个信箱时连接才能建立A link may be associated with many processes.连接可同多个进程相关

49、连接可同多个进程相关Each pair of processes may share several communication links. 每一对进程可共享多个通信连接每一对进程可共享多个通信连接Link may be unidirectional or bi-directional.连接可是无向或双向的连接可是无向或双向的Operations操作操作create a new mailbox 创建新的信箱创建新的信箱send and receive messages through mailbox 通过信箱发送和接收消息通过信箱发送和接收消息destroy a mailbox 销毁信箱销毁

50、信箱4.47Indirect Communication (Continued)间接通信间接通信Mailbox sharing 信箱共享信箱共享P1, P2, and P3 share mailbox A. P1, P2与与P3共享信箱共享信箱AP1, sends; P2 and P3 receive. P1发送;发送; P2与与P3接受接受Who gets the message?谁得到消息?谁得到消息?Solutions 解决方案解决方案Allow a link to be associated with at most two processes. 允许一个连接最多同允许一个连接最多同2

51、个进程相关个进程相关Allow only one process at a time to execute a receive operation. 只允许一个时刻有一个进程执行接受操作只允许一个时刻有一个进程执行接受操作Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. 允许系统任意选择接收者。发送者被通知谁是接收者。允许系统任意选择接收者。发送者被通知谁是接收者。4.48Buffering缓冲缓冲Queue of messages attached to

52、the link; implemented in one of three ways. 消息队列附加在连接上;采用三个之一的实现方案消息队列附加在连接上;采用三个之一的实现方案1. Zero capacity 0 messages零容量零容量 - 0 消息消息 Sender must wait for receiver (rendezvous). 发送者必须等待接收者发送者必须等待接收者2. Bounded capacity finite length of n messages有界容量有界容量 - n个消息有限长度个消息有限长度 Sender must wait if link full.

53、若连接满了发送者必须等待若连接满了发送者必须等待3. Unbounded capacity infinite length 无界容量无界容量 - 无限长度无限长度 Sender never waits. 发送者从不等待发送者从不等待4.49Client-Server CommunicationSockets 套接字套接字Remote Procedure Calls远端系统调用远端系统调用4.50SocketsA socket is defined as an endpoint for communication.套接字定义成通讯中的端套接字定义成通讯中的端Concatenation of IP

54、 address and port与与ip地址和端口相关地址和端口相关The socket 161.25.19.8:1625 refers to port 1625 on host Communication consists between a pair of sockets.4.51Socket Communication4.52Remote Procedure CallsRemote procedure call (RPC) abstracts procedure calls between processes on networked systems.RPC是网络系统中进程之间的系统调

55、用的抽象是网络系统中进程之间的系统调用的抽象Stubs client-side proxy for the actual procedure on the server. Stub为服务器端客户程序的代理为服务器端客户程序的代理The client-side stub locates the server and marshalls the parameters.The server-side stub receives this message, unpacks the marshalled parameters, and peforms the procedure on the serve

56、r.4.53Execution of RPC4.54Remote Method InvocationRemote Method Invocation (RMI) is a Java mechanism similar to RPCs.RMI allows a Java program on one machine to invoke a method on a remote object.4.55Marshalling Parameters4.56Exception Conditions Error Recovery异常条件异常条件 - 出错恢复出错恢复Process terminates 进

57、程终止进程终止Lost messages 消息丢失消息丢失Scrambled Messages 消息受损消息受损4.57Process: SummaryAn “instantiation” of a programSystem abstraction: the set of resources required for executing a programExecution context(s)Address spaceFile handles, communication endpoints, etc.Historically, all of the above “lumped” into

58、 a single abstractionMore recently, split into several abstractionsThreads, address space, protection domain, etc.OS process management:Supports user creation of processes and interprocess communication (IPC) Allocates resources to processes according to specific policiesInterleaves the execution of multiple processes to increase system utilization4.58Next TimeThreadsRead Silberschatz Chapter 5

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号