Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Part two: Process management A process can be thought of as a program in execution. A process will need certain resources-such as CPU time, memory, files, and I/O devices to accomplish its task. These resources are allocated to the process either when it is created or while it is executing. All these processes may execute concurrently. The operating system is responsible for the following activities in connection with process and thread management: the creation and deletion of both user and system processes; the scheduling of processes; and the provision of mechanisms for synchronization, communication, and deadlock handling for processes. P79 3.1/44 Chapter 3: Processes Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication 3.3/44 Objectives To introduce the notion of a process - a program in execution, which forms the basis of all computation. To describe the various features of processes, including scheduling, creation and termination, and communication. To describe communication in client-sewer systems. 3.4/44 Process Concept A question that arises in discussing operating systems involves what to call all the CPU activities. 在多道程序环境中,程序的执行是断断续续的,具备动 态特征,程序和程序的执行不再一一对应,有必要引入 一个新的概念 进程的定义:可并发执行的程序在一个数据集合上的运 行过程。 P81 3.5/44 Process Concept a process is a program in execution. A process is more than the program code, which is sometimes known as the text section. It also includes the current activity, as represented by the value of the program counter and the contents of the processor's registers. A process generally also includes the process stack, which contains temporary data (such as function parameters, return addresses, and local variables), and a data section, which contains global variables. A process may also include a heap, which is memory that is dynamically allocated during process run time. 3.6/44 Process in Memory 3.7/44 Program vs Process a program by itself is not a process; a program is a passive entity, such as a file containing a list of instructions stored on disk (often called an executable file), whereas a process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources. A program becomes a process when an executable file is loaded into memory. Although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences. 3.8/44 Process State As a process executes, it changes state. The state of a process is defined in part by the current activity of that process. Each process may be in one of the following states: 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 execution only one process can be running on any processor at any instant. Many processes may be ready and waiting. P83 3.9/44 Diagram of Process State 3.10/44 Process Control Block (PCB) Each process is represented in the operating system by a process control block It contains many pieces of information associated with a specific process, including these: Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information P84 3.11/44 Process Control Block (PCB) PCB的作用: 反映进程的动态特征,描述进程的执行 情况,使操作系统能控制进程的运行。 3.12/44 CPU Switch From Process to Process 3.13/44 Characters of process 动态性:进程有一定的生命周期,它由创建而 产生,由调度而执行,因得不到资源而暂停执 行,由撤消而消亡。 并发性:多个进程实体,同存于内存中,能在 一段时间内同时运行。 独立性:进程是独立运行的基本单位,是系统 中独立获得资源和独立调度的基本单位。 异步性:进程按各自独立的、不可预知的速度 向前推进。 结构特征:进程=程序+数据+进程控制块 3.14/44 Process Scheduling The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization. The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each program while it is running. To meet these objectives, the process scheduler selects an available process (possibly from a set of several available processes) for program execution on the CPU. For a single-processor system, there will never be more than one running process. If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled. P85 3.15/44 Process Scheduling Queues Job queue – set of all processes in the system Only one Ready queue – set of all processes residing in main memory, ready and waiting to execute Only one Device queues – set of processes waiting for an I/O device Each device has its own device queue Processes migrate among the various queues 3.16/44 Ready Queue And Various I/O Device Queues 3.17/44 Representation of Process Scheduling P88 3.18/44 Schedulers 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 Medium-term scheduler -- to remove processes from memory (and from active contention for the CPU) and thus the degree of multiprogramming. Later, the process can be reintroduced into memory, and its execution can be continued where it left off. P88 3.19/44 Addition of Medium Term Scheduling 3.20/44 Schedulers 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 – spends more time doing I/O than computations, many short CPU bursts CPU-bound process – spends more time doing computations; few very long CPU bursts 3.21/44 Schedulers It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-bound processes. If all processes are I/O bound, the ready queue will almost always be empty, and the short-term scheduler will have little to do. If all processes are CPU bound, the I/O waiting queue will almost always be empty, devices will go unused, and again the system will be unbalanced. The system with the best performance will thus have a combination of CPU-bound and I/O-bound processes. 3.22/44 long-term scheduler the long-term scheduler may be absent or minimal. time-sharing systems such as UNIX and Microsoft Windows systems often have no long-term scheduler but simply put every new process in memory for the short-term scheduler. The stability of these systems depends either on a physical limitation (such as the number of available terminals) or on the self-adjusting nature of human users. If the performance declines to unacceptable levels on a multiuser system, some users will simply quit. 3.23/44 Context Switch interrupts cause the operating system to change a CPU from its current task and to run a kernel routine. When an interrupt occurs, the system needs to save the current context of the process currently running on the CPU so that it can restore that context when its processing is done, essentially suspending the process and then resuming it. The context is represented in the PCB of the process; it includes the value of the CPU registers, the process state, and memory-management information. Generically, we perform a state save of the current state of the CPU, be it in kernel or user mode, and then a state restore to resume operations. P89 3.24/44 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 pure overhead; the system does no useful work while switching Time dependent on hardware support the memory speed, the number of registers that must be copied, and the existence of special instructions (such as a single instruction to load or store all registers). 3.25/44 Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes 3.26/44 Process Creation Resource sharing Sub-process may be able to obtain its resources directly from the operating system, or it may be constrained to a subset of the resources of the parent process. The parent may have to partition its resources among its children, or it may be able to share some resources (such as memory or files) among several of its children. Restricting a child process to a subset of the parent's resources prevents any process from overloading the system by creating too many sub-processes. P90 3.27/44 Process Creation execution The parent continues to execute concurrently with its children. The parent waits until some or all of its children have terminated. address space The child process is a duplicate of the parent process (it has the same program and data as the parent). The child process has a new program loaded into it. 3.28/44 Process Creation 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 3.29/44 int main() C Program Forking Separate { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } } 3.30/44 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 If parent is exiting Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading termination Users could arbitrarily kill some jobs (kill). 3.31/44 P95 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 P96 3.32/44 models of inter-process communication shared memory a region of memory that is shared by cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region. message passing communication takes place by means of messages exchanged between the cooperating processes. 3.33/44 models of inter-process communication Both of the models just discussed are common in operating systems, and many systems implement both. Message passing is useful for exchanging smaller amounts of data, because no conflicts need be avoided. Message passing is also easier to implement than is shared memory for inter-computer communication. Shared memory allows maximum speed and convenience of communication, as it can be done at memory speeds when within a computer. Shared memory is faster than message passing. 3.34/44 Communications Models Figure 3.13 Communications models. (a) Message passing. (b) Shared memory. 3.35/44 Inter-process Communication (IPC) Mechanism for processes to communicate and to synchronize their actions 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 communication link between them exchange messages via send/receive Implementation of communication link P99 physical (e.g., shared memory, hardware bus) logical (e.g., logical properties) 3.36/44 Implementation Questions How are links established? 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 accommodate fixed or variable? Is a link unidirectional or bi-directional? 3.37/44 Direct Communication Processes must name each other explicitly: send (P, message) – send a message to process P receive(Q, message) – receive a message from process 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 The link may be unidirectional, but is usually bidirectional P100 3.38/44 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 P101 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 3.39/44 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 3.40/44 Indirect Communication Mailbox sharing P1, P2, and P3 share mailbox A P1, sends; P2 and 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. 3.41/44 Synchronization Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null P102 3.42/44 Buffering Queue of messages attached to the link; implemented in one of three ways 1. Zero capacity – 0 messages Sender must wait for receiver (rendezvous) 2. Bounded capacity – finite length of n messages Sender must wait if link full 3. Unbounded capacity – infinite length Sender never waits P102 3.43/44 Homework: 1、2 End of Chapter 3