《Module4Processes进程.ppt》由会员分享,可在线阅读,更多相关《Module4Processes进程.ppt(58页珍藏版)》请在三一办公上搜索。
1、Module 4:Processes进程,Process Concept 进程概念Process Scheduling 进程调度Operation on Processes 进程上的操作Cooperating Processes 协同进程Interprocess Communication 进程间通信,Course Overview,Goals:Understanding of OS and the OS/architecture interface/interactionPrerequisites:Data structure,compilerWhat to expect:We will c
2、over 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 Final,Textbook and Topics,Operating System Concepts(6th Edition),Silberschatz and Galvin,ISBN:0-471-41743-2Operating Systems
3、(4th Edition),Stallings,ISBN:013-031-999-6(recommand)Operating systems design and implement,Andrew S.TanenbaumTopicsProcesses and threadsProcessor schedulingSynchronizationVirtual memoryFile systemsI/O systems,Today,Process Concept 进程概念Process Scheduling 进程调度Operation on Processes 进程上的操作Cooperating
4、Processes 协同进程Interprocess Communication 进程间通信Silberschatz Chapter 4(4.1-4.4),What Is An Operating System?,A software layer between the hardware and the application programs/users which provides a virtual machine interface:easy and safeA resource manager that allows programs/users to share the hardw
5、are resources:fair and efficientA set of utilities to simplify application development,hardware,operating system,application(user),Abstract View of System Components,Why Do We Want An OS?,Benefits for application writersEasier to write programsSee high level abstractions instead of low-level hardwar
6、e detailsE.g.files instead of disk blocksPortabilityBenefits for usersEasier to use computersCan you imagine trying to use a computer without the OS?SafetyOS protects programs from each otherOS protects users from each other,Basic computer structure,System Abstraction:Processes,A process is a system
7、 abstraction:illusion of being the only job in the system,Processes:Mechanism and Policy,Mechanism:Creation,destruction,suspension,context switch,signaling,IPC,etc.Policy:Minor policy questions:Who can create/destroy/suspend processes?How many active processes can each user have?Major policy questio
8、n that we will concentrate on:How to share system resources between multiple processes?Typically broken into a number of orthogonal policies for individual resource such as CPU,memory,and disk.,Processor Abstraction:Threads,A thread is a processor abstraction:illusion of having 1 processor per execu
9、tion context,hardware:processor,operating system:thread,application:execution context,create,kill,synch.,context switch,Threads:Mechanism and Policy,Mechanism:Creation,destruction,suspension,context switch,signaling,synchronization,etc.Policy:How to share the CPU between threads from different proce
10、sses?How to share the CPU between threads from the same process?How can multiple threads synchronize with each other?How to control inter-thread interactions?Can a thread murder other threads at will?,Process Concept进程概念,An operating system executes a variety of programs:操作系统执行各种程序Batch system jobs
11、批处理系统-作业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.进程-在执行中的程序;进程的执行必须以顺序方式进行A process includes:一个进程包括program counter 程序计数器
12、stack 栈data section 数据部分,Run Time Storage Organization,Each variable must be assigned a storage classGlobal(static)variablesAllocated in globals region at compile-timeMethod local variables and parameters 函数和系统调用Allocate dynamically on stackDynamically created objects(using new)数据空间Allocate from hea
13、pObjects live beyond invocation of a methodGarbage collected when no longer“live”,Code 代码,Globals,Stack,Heap 数据,Memory,Why Did We Talk About All That Stuff?,Process=system abstraction for the set of resources required for executing a program=a running instance of a program=memory image+registers con
14、tent(+I/O state)The stack+registers content represent the execution context or thread of control,What About The OS?,Recall that one of the function of an OS is to provide a virtual machine interface that makes programming the machine easierSo,a process memory image must also contain the OS,OS,Code,G
15、lobals,Stack,Heap,Memory,Code,Globals,Stack,Heap,OS data space is used to store thingslike file descriptors for files beingaccessed by the process,status of I/Odevices,etc.,What Happens When There Are More Than One Running Process?,OS,Code,Globals,Stack,Heap,P0,P1,P2,Process State进程状态,As a process e
16、xecutes,it changes state 进程执行时,改变状态new:The process is being created.新建:在创建进程running:Instructions are being executed.运行:指令在执行waiting:The process is waiting for some event to occur.等待:进程等待某些事件发生ready:The process is waiting to be assigned to a processor.就绪:进程等待分配处理器terminated:The process has finished e
17、xecution.终止:进程执行完毕,Diagram of Process State进程状态图,Process Control Block(PCB)进程控制块,The OS manage information for each process in each process tableInformation associated with each process.同进程有关的信息Process state 进程状态Program counter程序计数器CPU registers CPU寄存器CPU scheduling information CPU调度信息Memory-managem
18、ent information内存管理信息Accounting information计账信息I/O status information I/O状态信息,Process Control Block(PCB)进程控制块,CPU Switch From Process to Process进程间CPU的切换,Process Scheduling Queues进程调度队列,Job queue set of all processes in the system.作业队列-在系统中的所有进程的集合Ready queue set of all processes residing in main me
19、mory,ready and waiting to execute.就绪队列-在主内存中的,就绪并等待执行的所有进程的集合Device queues set of processes waiting for an I/O device.设备队列-等待某一I/O设备的进程队列Process migration between the various queues.在各种队列之间进程的迁移,Ready Queue And Various I/O Device Queues就绪队列和各种I/O设备队列,Representation of Process Scheduling进程调度的描述,Sched
20、ulers调度,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 executed next and allocates CPU.短程调度(或CPU调度)-选择可被下一个执行并分配CPU的进程,Addition of Medium Term Schedulin
21、g中程调度,Schedulers调度(Cont.),Short-term scheduler is invoked very frequently(milliseconds)(must be fast).短程调度切换频率高Long-term scheduler is invoked very infrequently(seconds,minutes)(may be slow).长程调度不快The long-term scheduler controls the degree of multiprogramming.长程调度控制了多道程序的“道”Processes can be describe
22、d as either:进程可以用下列方式描述:I/O-bound process spends more time doing I/O than computations,many short CPU bursts.I/O型进程-花费I/O 时间多于计算,许多短CPU处理CPU-bound process spends more time doing computations;few very long CPU bursts.CPU 型进程-花费更多时间于计算,许多长CPU处理,Context Switch上下文切换,When CPU switches to another process,
23、the system must save the state of the old process and load the saved state for the new process.当CPU切换至另一个进程时,系统必须保存旧进程状态并为新进程调入所保留的状态Context-switch time is overhead;the system does no useful work while switching.上下文切换的时间开销较重;在切换时,系统没有做有用的工作Time dependent on hardware support.时间取决于硬件的支持,Process Creati
24、on进程创建,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 subset of parents resources.子进程共享父进程资源的子集Parent and child share no res
25、ources.父进程和子进程无资源共享Execution执行Parent and children execute concurrently.父进程和子进程并发执行Parent waits until children terminate.父进程等待,直到子进程终止,Process Creation(Cont.)进程创建,Address space 地址空间Child duplicate of parent.子女复制双亲Child has a program loaded into it.子女有一个程序被调入UNIX examples UNIX例子fork system call create
26、s new process fork 系统调用创建新进程execve system call used after a fork to replace the process memory space with a new program.在fork 用一个新程序替代了进程的内存空间之后,采用execve系统调用,Process Creation,How to create a process?System call.In UNIX,a process can create another process using the fork()system callint pid=fork();/*
27、this is in C*/The creating process is called the parent and the new process is called the childThe child 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 ad
28、dress spacesBy default,theres no memory sharingProcess creation is expensive because of this copyingThe exec()call is provided for the newly created process to run a different program than that of the parent,Process Creation,fork(),fork()code,PCBs,Example of Process Creation Using Fork,The UNIX shel
29、l is command-line interpreter whose basic purpose is for user to run applications on a UNIX systemcmd arg1 arg2.argn,A Tree of Processes On A Typical UNIX System典型UNIX系统中的进程树,Process Termination进程终止,Process executes last statement and asks the operating system to decide it(exit).进程执行的最后一项并询问操作系统作出决定
30、(退出)Output data from child to parent(via wait).从子进程向父进程输出数据)(通过等待)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 longer required.
31、赋予子进程的任务不再需要Parent is exiting.父进程Operating system does not allow child to continue if its parent terminates.若父进程终止,不允许子进程继续Cascading termination.级联终止,Process Creation/Destruction in Java,In Java,this API is hidden in the java.lang.Runtime and java.lang.Process classesimport java.lang.*Runtime rt=Run
32、time.getRuntime();/get runtimeProcess child=rt.exec(“loop-forever-program”);/create childchild.destroy();/kill child,Cooperating Processes协同进程,Independent process cannot affect or be affected by the execution of another process.独立进程不会影响另一个进程的执行或被另一个进程执行影响Cooperating process can affect or be affected
33、 by the execution of another process 协同进程可能影响另一个进程的执行或被另一个进程执行影响Advantages of process cooperation 进程协同的优点Information sharing 信息共享Computation speed-up 加速运算Modularity 模块化Convenience 方便,Producer-Consumer Problem生产者-消费者问题,Paradigm for cooperating processes,producer process produces information that is c
34、onsumed by a consumer process.生产者进程生产供消费者进程消费的信息unbounded-buffer places no practical limit on the size of the buffer.无界缓冲没有对缓冲区大小的限制bounded-buffer assumes that there is a fixed buffer size.有界缓冲对缓冲区大小作了限定,Bounded-Buffer Shared-Memory Solution,Shared data#define BUFFER_SIZE 10Typedef struct.item;item
35、bufferBUFFER_SIZE;int in=0;int out=0;Solution is correct,but can only use BUFFER_SIZE-1 elements,Bounded-Buffer Producer Process,item nextProduced;while(1)while(in+1)%BUFFER_SIZE)=out);/*do nothing*/bufferin=nextProduced;in=(in+1)%BUFFER_SIZE;,Bounded-Buffer Consumer Process,item nextConsumed;while(
36、1)while(in=out);/*do nothing*/nextConsumed=bufferout;out=(out+1)%BUFFER_SIZE;,Interprocess Communication(IPC)进程间通信,Mechanism for processes to communicate and to synchronize their actions.用于进程通信的机制,同步其间的活动Message system processes communicate with each other without resorting to shared variables.消息系统-
37、进程间通信无须对共享变量进行再分类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 建立通信连接exchange messages via send/receive 通过send/receive交换消息Impl
38、ementation of communication link 通信连接的实现physical(e.g.,shared memory,hardware bus)物理的(如,共享存储,硬件总线)logical(e.g.,logical properties)逻辑的(如,逻辑特性),Implementation Questions实现中的问题,How are links established?连接如何建立?Can a link be associated with more than two processes?连接可同多于两个的进程相关吗?How many links can there b
39、e between every pair of communicating processes?每对在通信进程有多少连接?What is the capacity of a link?一个连接的容量是多少?Is the size of a message that the link can accommodate fixed or variable?连接可使用的固定或可变消息的大小?Is a link unidirectional or bi-directional?连接是无向的还是双向的?,Direct Communication直接通信,Processes must name each o
40、ther explicitly:进程必须显式的命名send(P,message)send a message to process P向进程P发消息receive(Q,message)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.连接精确的与一对在通信的进程相关B
41、etween each pair there exists exactly one link.在每一对之间就存在一个连接The link may be unidirectional,but is usually bi-directional.连接可以无向,但通常是双向的,Indirect Communication间接通信,Messages are directed and received from mailboxes(also referred to as ports).消息导向至信箱并从信箱接收(被视作端口)Each mailbox has a unique id.每一个信箱有一个唯一的
42、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.连接可同多个进程相关Each pair of processes may share several communication lin
43、ks.每一对进程可共享多个通信连接Link may be unidirectional or bi-directional.连接可是无向或双向的Operations操作create a new mailbox 创建新的信箱send and receive messages through mailbox 通过信箱发送和接收消息destroy a mailbox 销毁信箱,Indirect Communication(Continued)间接通信,Mailbox sharing 信箱共享P1,P2,and P3 share mailbox A.P1,P2与P3共享信箱AP1,sends;P2 a
44、nd P3 receive.P1发送;P2与P3接受Who gets the message?谁得到消息?Solutions 解决方案Allow a link to be associated with at most two processes.允许一个连接最多同2个进程相关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
45、receiver was.允许系统任意选择接收者。发送者被通知谁是接收者。,Buffering缓冲,Queue of messages attached to 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 mu
46、st wait if link full.若连接满了发送者必须等待3.Unbounded capacity infinite length 无界容量-无限长度 Sender never waits.发送者从不等待,Client-Server Communication,Sockets 套接字Remote Procedure Calls远端系统调用,Sockets,A socket is defined as an endpoint for communication.套接字定义成通讯中的端Concatenation of IP address and port与ip地址和端口相关The soc
47、ket 161.25.19.8:1625 refers to port 1625 on host Communication consists between a pair of sockets.,Socket Communication,Remote Procedure Calls,Remote procedure call(RPC)abstracts procedure calls between processes on networked systems.RPC是网络系统中进程之间的系统调用的抽象Stubs client-side proxy for the actual proced
48、ure 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 server.,Execution of RPC,Remote Method Invocation,Remote Method Invocation(RMI)is a Jav
49、a mechanism similar to RPCs.RMI allows a Java program on one machine to invoke a method on a remote object.,Marshalling Parameters,Exception Conditions Error Recovery异常条件-出错恢复,Process terminates 进程终止Lost messages 消息丢失Scrambled Messages 消息受损,Process:Summary,An“instantiation”of a programSystem abstrac
50、tion:the set of resources required for executing a programExecution context(s)Address spaceFile handles,communication endpoints,etc.Historically,all of the above“lumped”into a single abstractionMore recently,split into several abstractionsThreads,address space,protection domain,etc.OS process manage