1 / 47

Chapter 4: Processes

Chapter 4: Processes. Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems. Process Concept. An operating system executes a variety of programs: Batch system – jobs( 作业)

xanti
Download Presentation

Chapter 4: Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Processes • Process Concept • Process Scheduling • Operations on Processes • Cooperating Processes • Interprocess Communication • Communication in Client-Server Systems Operating System Concepts

  2. Process Concept • An operating system executes a variety of programs: • Batch system – jobs(作业) • Time-shared systems – user programs or tasks(任务) • Textbook uses the terms job and process almost interchangeably. • Process – a program in execution; a program is a passive entity,while a process is an active entity(进程是有生命的,程序是无生命的) • A process must include: • Text section(program code,程序代码) • Current activity(program counter&processor register,当前状态) • Stack(存放临时数据:如函数参数、返回地址) • data section(数据区,存放进程执行时用到的数据) Operating System Concepts

  3. Process State • 进程是活动着的程序,任何进程在任何时刻都处于某一状态(state): • New(新建): 进程刚被创建,还没提交给系统 • Running(正在运行): 占有CPU. • Waiting(等待): 进程因为等待某事件的发生如I/O完成,不能继续执行. • Ready(就绪): 万事具备,只欠东风(被执行). • Terminated(终止): 进程已经执行完. • 实际系统中进程的状态划分略有差异,如 LINUX: • TASK_RUNNING(就绪或执行): R • TASK_INTERRUPTIBLE(可中断等待). S • TASK_UNINTERRUPTIBLE(不可中断等待): D • TASK_STOPPED(暂停). T • TASK_ZOMBIE(僵死) Z #ps –el 详细列出当前系统中所有的进程,其中的S属性即为进程状态 #ps ax 按BSD格式列出进程,其中的STAT属性还列出以下属性: W: has no resident pages <: high-priority process N: low_priority task L: has pages locked into memory(for real-time) Operating System Concepts

  4. diagram of process state(5 states) Operating System Concepts

  5. Linux进程状态图 Operating System Concepts

  6. Process Control Block (PCB,进程控制块) OS为了管理、控制进程,设置PCB,存储进程相关信息 • Process state(状态) • Program counter(程序计数器) • CPU registers(寄存器值) • CPU scheduling information(调度信息) • Memory-management information(存储管理信息) • Accounting information(记帐信息,如CPU time) • I/O status information(I/O状态信息,如打开的文件) Operating System Concepts

  7. Process Control Block (PCB) Operating System Concepts

  8. CPU Switch From Process to Process Operating System Concepts

  9. 进程调度队列(Process Scheduling Queues) • 作业队列(Job queue) – set of all processes in the system. • 就绪队列(Ready queue) – set of all processes residing in main memory, ready and waiting to execute. • 设备队列(Device queues) – set of processes waiting for an I/O device. • OS调度进程,使进程在各队列间迁移( migrate) Operating System Concepts

  10. Ready Queue And Various I/O Device Queues Operating System Concepts

  11. 进程在各队列间迁移(方块表示队列,圆圈表示资源)进程在各队列间迁移(方块表示队列,圆圈表示资源) Operating System Concepts

  12. 调度(schedulers,选择进程在各队列间迁移) • Long-term scheduler (or job scheduler,作业调度) – selects which processes should be brought into the ready queue. - always appears on batch systems, not present on time-sharing OSs like UNIX.(往往在批处理系统中出现,以控制系统中的并行作业数) • Short-term scheduler (or CPU scheduler,CPU调度) – selects which process should be executed next and allocates CPU. • Medium-term scheduler(swapping,对换) – swap in, swap out, partially executed processes Operating System Concepts

  13. 对换调度(Medium Term Scheduling) Operating System Concepts

  14. 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 described as either: • I/O-bound process(I/O约束的进程) – spends more time doing I/O than computations, many short CPU bursts. • CPU-bound process(CPU约束的进程) – spends more time doing computations; few very long CPU bursts. Operating System Concepts

  15. 进程上下文切换(Context Switch) • When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.(进程切换时必须保存当前进程的状态,恢复调入进程的状态) • Context-switch time is overhead; the system does no useful work while switching.(很费时,如果系统频繁进行进程切换,将严重影响系统性能) • Time dependent on hardware support. Operating System Concepts

  16. Operation on process:Process Creation • Parent process create 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 parent’s resources. • Parent and child share no resources. • Execution • Parent and children execute concurrently. • Parent waits until children terminate. Operating System Concepts

  17. Process Creation (Cont.) • Address space • Child duplicate of parent. • Child has a program loaded into it. • UNIX examples • fork system call creates new process • exec system call used after a fork to replace the process’ memory space with a new program. Operating System Concepts

  18. Processes Tree on a UNIX System Pagedaemon:2#进程, swapper:3#进程,init :1#进程 Operating System Concepts

  19. #include <stdio.h>void main(){ int pid; pid = fork(); //system call if (pid < 0 ) { //error ocurrred printf(“fork failed.”); exit(-1); //system call } else if (pid == 0 ) { //child process printf(“child process executing…\n”); execlp(“/bin/ls”,”ls”,NULL); //system call } else { //parent process printf(“parent wating…\n”); wait(NULL); //system call, wait for children completion printf(“child complete.”); exit(0); }} 结果: parent waiting… child process executing… <ls result> child complete Operating System Concepts

  20. Operation on process: Process Termination • Process executes last statement and asks the operating system to delete it (exit).(主动终止) • 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. • Parent is exiting. • Operating system does not allow child to continue if its parent terminates. • Cascading termination.(级联终止) Operating System Concepts

  21. Cooperating Processes • Independent process cannot affect or be affected by the execution of another process. • Cooperating process can affect or be affected by the execution of another process • Advantages of process cooperation • Information sharing • Computation speed-up • Modularity • Convenience Operating System Concepts

  22. Example of cooperating processes:Producer-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.(无限缓冲) • bounded-buffer assumes that there is a fixed buffer size.(有限缓冲) Operating System Concepts

  23. Bounded-Buffer – Shared-Memory Solution • Shared data #define BUFFER_SIZE 10 Typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Operating System Concepts

  24. Bounded-Buffer Producer: item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Consumer: item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } • Solution is correct, but can only use BUFFER_SIZE-1 elements(why?) Operating System Concepts

  25. Interprocess communication(IPC) • Mechanism for processes to communicate and to synchronize their actions. • signal(信号机制):给其他进程发送异步事件信号。如: # kill –9 pid :向 pid 进程发送9号信号(无条件终止) # kill –l :显示系统中所有的信号 • # ls –l | wc –l :求出当前目录下的文件数 • pipe(管道技术):一个进程的输出作为另外一个进程的输入,实现相关进程(如父子进程)通信,可看作一个临时文件。如: • 无名管道(pipe):#ls –l | grep ‘^d’,实现相同父进程的子进程间通信。 • 命名管道(named pipe,FIFO):是有名字的管道,在OS中作为一个对象(文件,即FIFO文件)存在,实现任何进程间通信 • # mkfifo pipename • # ls –l > pipename & wc –l < pipename Operating System Concepts

  26. 共享内存(shared memory,SM):通过将两个或多个进程地址空间的一部分映射到相同的物理内存来实现共享内存机制,利用共享内存可以实现灵活的进程间通讯机制。但是,内存被共享之后,对共享内存的访问同步需要由其他 IPC 机制,例如信号量来实现。 • 消息传递(Message passing) • 信号量(semaphore,第七章讲) • 基于C/S的IPC(sockets,RPC,RMI) • 消息队列、信号量、共享内存是UNIX SYSV中三个经典的IPC机制 • 信号及管道是UNIX中较早使用的IPC机制 Operating System Concepts

  27. IPC:message passing • Message system – processes communicate with each other without resorting to shared variables. • IPC facility provides two operations: • send(message) – message size fixed or variable • receive(message) • If P and Q wish to communicate, they need to: • establish a communicationlink between them • exchange messages via send/receive Operating System Concepts

  28. Message-passing:Implementation Questions • Direct or indirect communication • Synchronization scheme • Buffering methods Operating System Concepts

  29. Message-passing: Direct Communication • Processes exchange messages directly • Symmetric: processesmust name each other explicitly: • send (P, message) – send a message to process P • receive(Q, message) – receive a message from process Q • Asymmetric: only the sender names the recipient • send (P, message) – send a message to process P • receive(id, message) – receive all messages associated with process “id” • 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. • The link may be unidirectional, but is usually bi-directional. Operating System Concepts

  30. Message-passing: Indirect Communication • Messages are directed and received from mailboxes (also referred to as ports). • Each mailbox has a unique id. • Processes 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 links. • Link may be unidirectional or bi-directional. Operating System Concepts

  31. Message-passing: Indirect Communication • Operations • create a new mailbox • send and receive messages through mailbox • destroy a mailbox • Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A Operating System Concepts

  32. Message-passing: Indirect Communication • Mailbox sharing • P1, P2, and P3 share mailbox A. • P1, sends; P2and P3 receive. • Who gets the message? • Solutions • Allow a link to be associated with at most two processes. • 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. Operating System Concepts

  33. Message-passing: Synchronization • Message passing may be either blocking or non-blocking. • Blocking is considered synchronous • Non-blocking is considered asynchronous • send and receive primitives may be either blocking or non-blocking. • The most common case is non-blocking send and blocking receive Operating System Concepts

  34. Message-passing: Buffering • Queue of messages attached to the link; implemented in one of three ways. 1. Zero capacity – 0 messagesSender must wait for receiver 2. Bounded capacity – finite length of n messagesSender must wait if link full. 3. Unbounded capacity – infinite length Sender never waits. Case 1 is also called no buffering Case 2 and 3 are also called automatic buffering Operating System Concepts

  35. Linux中的消息队列(参见ipc目录) Message queue is also called mailbox Is an indirect message communication method Msgque:消息队列数组,其下标subscription就是相应消息队列的id; Msgid_ds:消息队列头,是对某个消息队列的描述,包括访问权限(msg_perms), 及对队列中消息的管理 Msg: 即消息节点 Operating System Concepts

  36. 消息队列数据结构: Operating System Concepts

  37. Operations on message queue: Msgget Msgsend Msgrcv Msgctl Operating System Concepts

  38. Client-Server Communication • Communications may take place between processes on different machines. Some methods used: • Sockets • Remote Procedure Calls • Remote Method Invocation (Java) Operating System Concepts

  39. Sockets(套接字) • Originated from BSD UNIX • A socket is defined as an endpoint for communication. • A socket is a concatenation of IP address and port(端口) • Port:一个16位的整数,1024以下为系统用,如23为telnet service专用,21为ftp server专用,80为http server专用,应用程序应使用1024以上的端口 • The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 • Communication takes place between a pair of sockets. • Type of socket: * connection-oriented(TCP,面向连接):可靠 * connectionless(UDP,无连接):可靠性较差,但方便 * multicast(多播):可以连接至多个节点) • Any socket may be either blocking or non-blocking Operating System Concepts

  40. Socket Communication Operating System Concepts

  41. A typical C/S communication using sockets Server side: getprotobyname Client side: gethostbyname socket getprotobyname bind socket listen connect recv accept send close close Operating System Concepts

  42. Service side: int sd,sd2; int port = 1280; sd=socket(AF_INET, SOCK_STREAM,0); BindSocket( sd , nPort ) ; ListenSocket( sd ); while(1){ sd2=AcceptSocket( sd ); SendData(sd2,databuf); closesocket(sd2); } client side: int sd; int port = 1280; sd=socket(AF_INET, SOCK_STREAM,0); ConnectSocket(sd,port); RecvData(sd,databuf); closesocket(sd); Operating System Concepts

  43. Remote Procedure Calls(远程过程调用) • Remote procedure call (RPC) abstracts procedure calls between processes on networked systems. • 操作系统中的很多服务(services)依赖于RPC来实现,如windows2000中的COM+、DHCP、DNS server、remote storage、telnet等) • Stubs(存根) –proxy for the actual procedure on the server.(隐藏RPC调用的细节) • The client-side stub locates the server and packs and sends the parameters • The server-side stub receives this message, unpacks the parameters, and performs the procedure on the server. • 一个RPC服务对应一个port(端口,就像socket中的port),如果client不知道某个RPC的port,可以询问matchmaker(媒人),matchmaker也是一个服务,但它占用固定的RPC端口 • OSF-DCE:open software foundation-distributed computing environment,一个RPC标准,要求RPC简洁、透明、高效 Operating System Concepts

  44. Remote Procedure Calls • Microsoft RPC:基于OSF RPC - MIDL:Microsoft Interface Definition Language,接口描述语言,用它来描述对远程过程的访问 - MIDL编译器生成相应的stub - stub调用client side run-time library来发送请求及参数 - server端完成相应的过程 Operating System Concepts

  45. Operating System Concepts

  46. Remote Method Invocation(远程方法调用) • Remote Method Invocation (RMI) is a Java mechanism similar to RPCs.(JAVA版的RPC) • RMI allows a Java program on one machine to invoke a method on a remote object.(允许应用程序调用一个异地对象的方法) Operating System Concepts

  47. RMI的参数传递 Operating System Concepts

More Related