1 / 42

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

luyu
Télécharger la présentation

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; process execution must progress in sequential fashion. • A process includes: • program counter : which contains the address of next instruction to be executed. • Stack: which contains temporary data (such as method parameters, return addresses, and local variables) • data section: which contains global variables. Operating System Concepts

  3. Process 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 some event to occur. • ready: The process is waiting to be assigned to a process. • terminated: The process has finished execution. Operating System Concepts

  4. Diagram of Process State Operating System Concepts

  5. Process Control Block (PCB) Information associated with each process. • Process state: The state may be new, ready, running, waiting, halted, and SO on. • Program counter: The counter indicates the address of the next instruction to be executed for this process. • CPU registers: The registers vary in number and type, depending on the computer architecture. They include accumulators, stack pointers, and general-purpose registers… • CPU scheduling information: This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. • Memory-management information: This information may include such information as the page tables, or the segment tables, depending on the memory system used by the operating system • Accounting information: This information includes the amount of CPU and real time used, time limits, job or process numbers, and so on. • I/O status information: The information includes the list of I/O devices allocated to this process, a list of open files, and so on. Operating System Concepts

  6. Process Control Block (PCB) Operating System Concepts

  7. CPU Switch From Process to Process Operating System Concepts

  8. 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. • Process migration between the various queues. Operating System Concepts

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

  10. Representation of Process SchedulingFigure 4.5 Operating System Concepts

  11. Representation of Process Scheduling(cont..) • A common representation of process scheduling is a queueing diagram, such as that in Figure 4.5. Each rectangular box represents a queue. Two types of queues are present: the ready queue and a set of device queues. The circles represent the resources that serve the queues, and the arrows indicate the flow of processes in the system. Operating System Concepts

  12. Representation of Process Scheduling(cont…) • A new process is initially put in the ready queue. It waits in the ready queue until it is selected for execution (or dispatched). Once the process is assigned to the CPU and is executing, one of several events could occur: • The process could issue an I/O request, and then be placed in an I/O queue. • The process could create a new subprocess and wait for its termination. • The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue. • In the first two cases, the process eventually switches from the waiting state to the ready state, and is then put back in the ready queue. A process continues this cycle until it terminates, at which time it is removed from all queues and has its PCB and resources deallocated. Operating System Concepts

  13. 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. Operating System Concepts

  14. Addition of Medium Term SchedulingFigure 4.6 Operating System Concepts

  15. Medium-term scheduler • Some operating systems, such as time-sharing systems, may introduce an additional, intermediate level of scheduling. • This medium-term scheduler, diagrammed in Figure 4.6, removes processes from memory (and from active contention for the CPU), and thus reduces the degree of multiprogramming. • At some later time, the process can be reintroduced into memory and its execution can be continued where it left off. • This scheme is called swapping. The process is swapped out, and is later swapped in, by the medium-term scheduler. Operating System Concepts

  16. 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 – 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. Operating System Concepts

  17. 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

  18. 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

  19. Process Creation (Cont.) • Address space • Child address space is the same as it’s parent. • Child has its own address space. • UNIX examples • fork system call creates new process Operating System Concepts

  20. Processes Tree on a UNIX System Operating System Concepts

  21. Process Termination • Process executes last statement and asks the operating system to decide 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) because: • 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

  22. 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: If we want a particular task to run faster, we must break it into subtasks, each of which will be executing in parallel with the others. • Modularity: dividing the system functions into separate processes or threads • Convenience: Even an individual user may have many tasks on which to work at one time. For instance, a user may be editing, printing, and compiling in parallel. Operating System Concepts

  23. Cooperating Processes (Producer-Consumer Problem) • Paradigm(نموذج ) for cooperating processes, producer process produces information that is consumed by a consumer process. For example, a print program produces characters that are consumed by the printer driver. A compiler may produce assembly code, which is consumed by an assembler. The assembler, in turn, may produce object modules, which are consumed by the loader. • To allow producer and consumer processes to run concurrently, we must have available a buffer of items that can be filled by the producer and emptied by the consumer. • A producer can produce one item while the consumer is consuming another item. The producer and consumer must be synchronized, so that the consumer does not try to consume an item that has not yet been produced. In this situation, the consumer must wait until an item is produced. Operating System Concepts

  24. Cooperating Processes (Producer-Consumer Problem) cont… • The unbounded-buffer producer-consumer problem places no practical limit on the size of the buffer. The consumer may have to wait for new items, but the producer can always produce new items. • The bounded-buffer producer-consumer problem assumes a fixed buffer size. In this case, the consumer must wait if the buffer is empty, and the producer must wait if the buffer is full. • The buffer may either be provided by the operating system through the use of an interprocess-communication (IPC) facility, or by explicitly coded by the application programmer with the use of shared memory Operating System Concepts

  25. Bounded-Buffer – Shared-Memory Solution • Shared data #define BUFFER_SIZE 10 Typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; • The shared buffer is implemented as a circular array with two logical pointers: in and out. The variable in points to the next free position in the buffer; out points to the first full position in the buffer. The buffer is empty when in == out ; the buffer is full when ((in + 1) % BUFFERSIZE) == out. Operating System Concepts

  26. Bounded-Buffer – Producer Process • The code for the producer and consumer processes follows. The producer process has a local variable nextproduced in which the new item to be produced is stored: item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Operating System Concepts

  27. Bounded-Buffer – Consumer Process • The consumer process has a local variable nextconsumed in which the item to be consumed is stored: item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } Operating System Concepts

  28. Interprocess Communication (IPC) • Mechanism for processes to communicate and to synchronize their actions. • IPC is particularly useful in a distributed environment where the communicating processes may reside on different computers connected with a network. An example is a chat program used on the World Wide Web. • IPC is best provided by a message-passing system • message-passing system– processes communicate with each other without resorting(اللجوء) to shared variables. Operating System Concepts

  29. 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 • Implementation of communication link • physical (e.g., shared memory, hardware bus) • logical (e.g., Direct or indirect communication, Symmetric or asymmetric communication) Operating System Concepts

  30. 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 bi-directional. Operating System Concepts

  31. Indirect Communication • Messages are directed and received from mailboxes . • 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. • A mailbox may be owned either by a process or by the operating system. If the mailbox is owned by a process, then we distinguish between the owner (who can only receive messages through this mailbox) and the user (who can only send messages to the mailbox). Operating System Concepts

  32. Indirect Communication (cont…) • Since each mailbox has a unique owner, there can be no confusion about who should receive a message sent to this mailbox. When a process that owns a mailbox terminates, the mailbox disappears. Any process that subsequently sends a message to this mailbox must be notified that the mailbox no longer exists. • a mailbox owned by the operating system is independent and is not attached to any particular process. The operating system then must provide a mechanism that allows a process to do the following: • 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

  33. Indirect Communication (cont…) • 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

  34. Synchronization • Message passing may be either blocking or non-blocking. • Blocking send: The sending process is blocked until the message is received by the receiving process or by the mailbox. • Nonblocking send: The sending process sends the message and resumes operation. • Blocking receive: The receiver blocks until a message is available. • Nonblocking receive: The receiver retrieves either a valid message or a null. • Blocking is considered synchronous, Non-blocking is considered asynchronous • send and receive primitives may be either blocking or non-blocking. Operating System Concepts

  35. Buffering • Whether the communication is direct or indirect, messages exchanged by communicating processes reside in a temporary queue of messages attached to the link; implemented in one of three ways. 1.Zero capacity – 0 messagesSender must wait for receiver (the link cannot have any messages waiting init,(rendezvous)). 2. Bounded capacity – finite length of n messagesSender must wait if link full. 3. Unbounded capacity – infinite length Sender never waits. Operating System Concepts

  36. Client-Server Communication • Sockets • Remote Procedure Calls • Remote Method Invocation (Java) Operating System Concepts

  37. Sockets • A pair of processes communicating over a network employs a pair of sockets-one for each process. • A socket is defined as an endpoint for communication. • A socket is made up of an Concatenation of IP address and port number • The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 • Communication consists between a pair of sockets. Operating System Concepts

  38. Socket Communication • All connections must be unique. Therefore, if another process also on host X wished to establish another connection with the same web server, it would be assigned a port number not equal to 1625 Operating System Concepts

  39. Remote Procedure Calls • Remote procedure call (RPC) abstracts procedure calls between processes on networked systems. The semantics of RPCs allow a client to invoke a procedure on a remote host as it would invoke a procedure locally. The RPC system hides the necessary details allowing the communication to take place. • The RPC system does this by providing a stub on the client side. Typically, a separate stub exists for each separate remote procedure. When the client invokes a remote procedure, the RPC system calls the appropriate stub, passing it the parameters provided to the remote procedure. • This stub locates the port on the server and marshalls the parameters. Parameter marshalling involves packaging the parameters into a form which may be transmitted over a network. The stub then transmits a message to the server using message passing. • A similar stub on the server side receives this message and invokes the procedure on the server. If necessary, return values are passed back to the client using the same technique. Operating System Concepts

  40. Execution of RPC :server Operating System Concepts

  41. Remote Method Invocation • Remote 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. Operating System Concepts

  42. Marshalling Parameters • The skeleton is responsible for unmarshalling the parameters and invoking the desired method on the server. Operating System Concepts

More Related