Chapter 3 Processes Operating System Concepts 9 th

  • Slides: 39
Download presentation
Chapter 3: Processes Operating System Concepts – 9 th Edition Silberschatz, Galvin and Gagne

Chapter 3: Processes Operating System Concepts – 9 th Edition Silberschatz, Galvin and Gagne © 2013

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n Interprocess Communication n Examples of IPC Systems n Communication in Client-Server Systems Operating System Concepts – 9 th Edition 3. 2 Silberschatz, Galvin and Gagne © 2013

Objectives n To introduce the notion of a process -- a program in execution,

Objectives n To introduce the notion of a process -- a program in execution, which forms the basis of all computation n To describe the various features of processes, including scheduling, creation and termination, and communication n To explore interprocess communication using shared memory and message passing n To describe communication in client-server systems Operating System Concepts – 9 th Edition 3. 3 Silberschatz, Galvin and Gagne © 2013

Process Concept n Process – a program in execution; process execution must progress in

Process Concept n Process – a program in execution; process execution must progress in sequential fashion n Multiple parts for a process l The program code, also called text section l Current activity including program counter, processor registers l Stack containing temporary data 4 Function parameters, return addresses, local variables l Data section containing global variables l Heap containing memory dynamically allocated during process run time Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM. Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower Operating System Concepts – 9 th Edition 3. 4 Silberschatz, Galvin and Gagne © 2013

Process Concept (Cont. ) n Program is passive entity stored on disk (executable file),

Process Concept (Cont. ) n Program is passive entity stored on disk (executable file), process is active n It is important to realize that only one process can be running on any processor at any instant n One program can be several processes l Consider multiple users executing the same program Operating System Concepts – 9 th Edition 3. 5 Silberschatz, Galvin and Gagne © 2013

Process State and Process State Diagram n As a process executes, it changes state

Process State and Process State Diagram n As a process executes, it changes state l new: The process is being created l running: process Instructions are being executed l waiting: The process is waiting for some event to occur l ready: The process is waiting to be assigned to a processor l terminated: The process has finished execution Operating System Concepts – 9 th Edition 3. 6 Silberschatz, Galvin and Gagne © 2013

Process Control Block (PCB) Each process is represented in the operating system by a

Process Control Block (PCB) Each process is represented in the operating system by a process control block (PCB)—also called a task control block. . It contains many pieces of information associated with a specific process: 1. Process state – running, waiting, etc, 2. Program counter – location of instruction to next execute. 3. CPU registers –. Register information must be saved when an interrupt occurs, to allow the process to be continued correctly afterward 4. CPU scheduling information- priorities, scheduling queue pointers. 5. Memory-management information – memory allocated to the process 6. Accounting information – CPU used, clock time elapsed since start, time limits 7. I/O status information – I/O devices allocated to process, list of open files Operating System Concepts – 9 th Edition 3. 7 Silberschatz, Galvin and Gagne © 2013

CPU Switch From Process to Process Operating System Concepts – 9 th Edition 3.

CPU Switch From Process to Process Operating System Concepts – 9 th Edition 3. 8 Silberschatz, Galvin and Gagne © 2013

Process Scheduling n The objective of multiprogramming is to have some process running at

Process Scheduling n The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization. n The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each program n To meet these objectives, Process scheduler selects among available processes for next execution on CPU. l For a single-processor system, there will never be more than one running process. l If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled. n Scheduling queues of processes: l As processes enter the system, they are put into a job queue, which consists of all processes in the system. l The processes that are residing in main memory and are ready and waiting to execute are kept on a list called the ready queue. l The list of processes waiting for a particular I/O device is called a device queue. Each device has its own device queue> Operating System Concepts – 9 th Edition 3. 9 Silberschatz, Galvin and Gagne © 2013

Ready Queue And Various I/O Device Queues Operating System Concepts – 9 th Edition

Ready Queue And Various I/O Device Queues Operating System Concepts – 9 th Edition 3. 10 Silberschatz, Galvin and Gagne © 2013

Representation of Process Scheduling n Queueing diagram represents queues, resources, flows Operating System Concepts

Representation of Process Scheduling n Queueing diagram represents queues, resources, flows Operating System Concepts – 9 th Edition 3. 11 Silberschatz, Galvin and Gagne © 2013

Representation of Process Scheduling q A new process is initially put in the ready

Representation of Process Scheduling q A new process is initially put in the ready queue. It waits there until it is selected for execution, or dispatched. q Once the process is allocated the CPU and is executing, one of several events could occur: q The process could issue an I/O request and then be placed in an I/O queue. q The process could create a new child process and wait for the child’s termination. q The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue. q 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. q A process continues this cycle until it terminates, at which time it is removed from all queues and has its PCB and resources de-allocated. Operating System Concepts – 9 th Edition 3. 12 Silberschatz, Galvin and Gagne © 2013

Schedulers n Short-term scheduler (or CPU scheduler) – selects which process should be executed

Schedulers n Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU l n n n Short-term scheduler is invoked frequently (milliseconds) (must be fast) Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue l Long-term scheduler is invoked infrequently (seconds, minutes) (may be slow) l The long-term scheduler controls the degree of multiprogramming Processes can be described as either: l I/O-bound process – spends more time doing I/O than computations, many short CPU bursts l CPU-bound process – spends more time doing computations; few very long CPU bursts It is important that the long-term scheduler select a good process mix of I/Obound and CPU-bound processes. l 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. l If all processes are CPU bound, the I/O waiting queue will almost always be empty, devices will go unused, Operating System Concepts – 9 th Edition 3. 13 Silberschatz, Galvin and Gagne © 2013

Addition of Medium Term Scheduling n Medium-term scheduler can be added if degree of

Addition of Medium Term Scheduling n Medium-term scheduler can be added if degree of multiple programming needs to decrease l Remove process from memory, store on disk(swapped out), bring back in from disk to memory (swapped in) to continue execution: This scheme is called swapping Operating System Concepts – 9 th Edition 3. 14 Silberschatz, Galvin and Gagne © 2013

Context Switch n When CPU switches to another process, the system must save the

Context Switch n 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 via a context switch n Context of a process represented in the PCB n Context-switch time is overhead; the system does no useful work while switching l The more complex the OS and the PCB the longer the context switch Operating System Concepts – 9 th Edition 3. 15 Silberschatz, Galvin and Gagne © 2013

Process Creation System must provide mechanisms for process creation, termination, and so on Process

Process Creation System must provide mechanisms for process creation, termination, and so on Process Creation: n Parent process create children processes, which, in turn create other processes, forming a tree of processes n Generally, process identified and managed via a process identifier (pid) n Resource sharing options l Parent and children share all resources l Children share subset of parent’s resources l Parent and child share no resources n Execution options l Parent and children execute concurrently l Parent waits until children terminate n Address space l Child duplicate of parent(it has the same program and data as the parent) l Child has a new program loaded into it Operating System Concepts – 9 th Edition 3. 16 Silberschatz, Galvin and Gagne © 2013

Process Termination n Process executes last statement and then asks the operating system to

Process Termination n Process executes last statement and then asks the operating system to delete it using the exit() system call. l Returns status data from child to parent (via wait()) l Process’ resources are deallocated by operating system n Parent may terminate the execution of children processes using the abort() system call. Some reasons for doing so: l Child has exceeded allocated resources l Task assigned to child is no longer required l The parent is exiting and the operating systems does not allow a child to continue if its parent terminates Operating System Concepts – 9 th Edition 3. 17 Silberschatz, Galvin and Gagne © 2013

Process Termination n Some operating systems do not allow child to exists if its

Process Termination n Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then all its children must also be terminated. l cascading termination. All children, grandchildren, etc. are terminated. l The termination is initiated by the operating system. n The parent process may wait for termination of a child process by using the wait()system call. The call returns status information and the pid of the terminated process pid = wait(&status); n If no parent waiting (did not invoke wait()) process is a zombie l Once the parent calls wait(), the process identifier of the zombie process and its entry in the process table are released. n If parent terminated without invoking wait , process is an orphan Operating System Concepts – 9 th Edition 3. 18 Silberschatz, Galvin and Gagne © 2013

Interprocess Communication n Processes within a system may be independent or cooperating n Independent

Interprocess Communication n Processes within a system may be independent or cooperating n Independent process cannot affect or be affected by the execution of another process while Cooperating process can affect or be affected by other processes, including sharing data n Clearly, any process that shares data with other processes is a cooperating process. n Reasons for cooperating processes: l Information sharing: Since several users may be interested in the same piece of information (for instance, a shared file), we must provide an environment to allow concurrent access to such information. l Computation speedup: 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 in multiple processing cores computer. l Modularity: We may want to construct the system in a modular fashion, l Convenience: Even an individual user may work on many tasks at the same time. n Cooperating processes need interprocess communication (IPC) n Two models of IPC: Shared memory and Message passing. Operating System Concepts – 9 th Edition 3. 19 Silberschatz, Galvin and Gagne © 2013

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces information that is consumed

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process n Producer-Consumer examples: l For example, a compiler may produce assembly code that is consumed by an assembler. l For example, a web server produces HTML files and images, which are consumed by the client web browser requesting the resource. n Types of buffers used in Producer-Consumer processes: l unbounded-buffer 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 l bounded-buffer assumes that there is a fixed buffer size. the consumer must wait if the buffer is empty, and the producer must wait if the buffer is full. Operating System Concepts – 9 th Edition 3. 20 Silberschatz, Galvin and Gagne © 2013

Interprocess Communication – Shared Memory n Recall that, normally, the operating system tries to

Interprocess Communication – Shared Memory n Recall that, normally, the operating system tries to prevent one process from accessing another process’s memory. n Shared memory requires that two or more processes agree to remove this restriction. They can then exchange information by reading and writing data in the shared areas. n The form of the data and the location are determined by these processes and are not under the operating system’s control. n One solution to the producer–consumer problem uses shared memory. (unbounded-buffer and boundedbuffer) n Major issues is to provide mechanism that will allow the user processes to synchronize their actions when they access shared memory. Operating System Concepts – 9 th Edition 3. 21 Silberschatz, Galvin and Gagne © 2013

Figure 3. 18 Shared-memory solution to the producer – consumer problem. Operating System Concepts

Figure 3. 18 Shared-memory solution to the producer – consumer problem. Operating System Concepts – 9 th Edition 3. 22 Silberschatz, Galvin and Gagne © 2013

Interprocess Communication – Message Passing n Message passing provides a mechanism to allow processes

Interprocess Communication – Message Passing n Message passing provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space. n It is particularly useful in a distributed environment, where the communicating processes may reside on different computers connected by a network. l example, an Internet chat program could be designed so that chat participants communicate with one another by exchanging messages. n A message-passing facility provides at least two operations: send(message) l receive(message) l n The message size is either fixed or variable Operating System Concepts – 9 th Edition 3. 23 Silberschatz, Galvin and Gagne © 2013

Message Passing (Cont. ) n If processes P and Q wish to communicate, they

Message Passing (Cont. ) n If processes P and Q wish to communicate, they need to: Establish a communication link between them l Exchange messages via send/receive n Implementation issues: l l How are links established? l Can a link be associated with more than two processes? l How many links can there be between every pair of communicating processes? l What is the capacity of a link? l Is the size of a message that the link can accommodate fixed or variable? l Is a link unidirectional or bi-directional? Operating System Concepts – 9 th Edition 3. 24 Silberschatz, Galvin and Gagne © 2013

Message Passing (Cont. ) n Logical Implementation of communication link 4 Direct 4 4

Message Passing (Cont. ) n Logical Implementation of communication link 4 Direct 4 4 or indirect Synchronous or asynchronous Automatic or explicit buffering Operating System Concepts – 9 th Edition 3. 25 Silberschatz, Galvin and Gagne © 2013

Direct Communication n Processes must name each other explicitly: l send (P, message) –

Direct Communication n Processes must name each other explicitly: l send (P, message) – send a message to process P l receive(Q, message) – receive a message from process Q n Properties of communication link l Links are established automatically l A link is associated with exactly one pair of communicating processes l Between each pair there exists exactly one link l The link may be unidirectional, but is usually bi-directional Operating System Concepts – 9 th Edition 3. 26 Silberschatz, Galvin and Gagne © 2013

Indirect Communication n Messages are directed and received from mailboxes l Each mailbox has

Indirect Communication n Messages are directed and received from mailboxes l Each mailbox has a unique id l Processes can communicate only if they share a mailbox n Properties of communication link l Link established only if processes share a common mailbox l A link may be associated with many processes l Each pair of processes may share several communication links l Link may be unidirectional or bi-directional Operating System Concepts – 9 th Edition 3. 27 Silberschatz, Galvin and Gagne © 2013

Indirect Communication n Operations l create a new mailbox l send and receive messages

Indirect Communication n Operations l create a new mailbox l send and receive messages through mailbox l destroy a mailbox n 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 – 9 th Edition 3. 28 Silberschatz, Galvin and Gagne © 2013

Indirect Communication n Mailbox sharing l Now suppose that processes P 1, P 2,

Indirect Communication n Mailbox sharing l Now suppose that processes P 1, P 2, and P 3 all share mailbox A. Process P 1 sends a message to A, while both P 2 and P 3 execute a receive() from A. Which process will receive the message sent by P 1? n Solutions l Allow a link to be associated with at most two processes l Allow only one process at a time to execute a receive operation l Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. Operating System Concepts – 9 th Edition 3. 29 Silberschatz, Galvin and Gagne © 2013

Synchronization n Message passing may be either blocking or non-blocking n Blocking is considered

Synchronization n Message passing may be either blocking or non-blocking n Blocking is considered synchronous n l Blocking send -- the sender is blocked until the message is received by the receiving process or by the mailbox. l Blocking receive -- the receiver is blocked until a message is available. Non-blocking is considered asynchronous l Non-blocking send -- the sender sends the message and resumes operation. l Non-blocking receive -- The receiver retrieves either a valid message or a null. n Different combinations of send() and receive() are possible. When both send() and receive() are blocking, we have a rendezvous between the sender and the receiver. n The solution to the producer–consumer problem becomes trivial when we use blocking send() and receive() statements. The producer merely invokes the blocking send() call and waits until the message is delivered to either the receiver or the mailbox. Likewise, when the consumer invokes receive(), it blocks until a message is available. Operating System Concepts – 9 th Edition 3. 30 Silberschatz, Galvin and Gagne © 2013

Non-Blocking Message Passing Operating System Concepts – 9 th Edition 3. 31 Silberschatz, Galvin

Non-Blocking Message Passing Operating System Concepts – 9 th Edition 3. 31 Silberschatz, Galvin and Gagne © 2013

Buffering n Whether communication is direct or indirect Queue of messages attached to the

Buffering n Whether communication is direct or indirect Queue of messages attached to the link between communicating processes. n such queues implemented in one of three ways 1. Zero capacity – no messages are queued on a link. Sender must wait for receiver (rendezvous) 2. Bounded capacity – The queue has finite length n; 4 thus, at most n messages can reside in it. 4 If the queue is not full when a new message is sent, the message is placed in the queue and the sender can continue execution without waiting. 4 however. If the link is full, the sender must block until space is available in the queue. 3. Unbounded capacity – infinite length. Sender never waits Operating System Concepts – 9 th Edition 3. 32 Silberschatz, Galvin and Gagne © 2013

Communications in Client-Server Systems n we described how processes can communicate using shared memory

Communications in Client-Server Systems n we described how processes can communicate using shared memory and message passing. n These techniques can be used for communication in client–server systems as well. n In this section, we explore two other strategies for communication in client– server systems: l Sockets l Remote Procedure Calls Operating System Concepts – 9 th Edition 3. 33 Silberschatz, Galvin and Gagne © 2013

Sockets n A socket is defined as an endpoint for communication. n A pair

Sockets n A socket is defined as an endpoint for communication. n A pair of processes communicating over a network employs a pair of sockets—one for each process. n A socket is identified by an IP address concatenated with a port number. n In general, sockets use a client–server architecture. The server waits for incoming client requests by listening to a specified port. Once a request is received, the server accepts a connection from the client socket to complete the connection. n All ports below 1024 are well known, used for standard services n When a client process initiates a request for a connection, it is assigned a port by its host computer. This port has some arbitrary number greater than 1024. n The IP address 127. 0. 0. 1 is a special IP address known as the loopback. When a computer refers to IP address 127. 0. 0. 1, it is referring to itself. Operating System Concepts – 9 th Edition 3. 34 Silberschatz, Galvin and Gagne © 2013

Socket Communication For example, if a client on host X with IP address 146.

Socket Communication For example, if a client on host X with IP address 146. 86. 5. 20 wishes to establish a connection with a web server (which is listening on port 80) at address 161. 25. 19. 8, host X may be assigned port 1625. The connection will consist of a pair of sockets: (146. 86. 5. 20: 1625) on host X and (161. 25. 19. 8: 80) on the web server 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 greater than 1024 and not equal to 1625. This ensures that all connections consist of a unique pair of sockets. Operating System Concepts – 9 th Edition 3. 35 Silberschatz, Galvin and Gagne © 2013

Sockets in Java provides three different types of sockets. l Connection-oriented (TCP) sockets are

Sockets in Java provides three different types of sockets. l Connection-oriented (TCP) sockets are implemented with the Socket class. l Connectionless (UDP) sockets use the Datagram. Socket class. l Finally, the Multicast. Socket class is a subclass of the Datagram. Socket class. l A multicast socket allows data to be sent to multiple recipients. Operating System Concepts – 9 th Edition 3. 36 Silberschatz, Galvin and Gagne © 2013

Consider this “Date” client-server example Operating System Concepts – 9 th Edition 3. 37

Consider this “Date” client-server example Operating System Concepts – 9 th Edition 3. 37 Silberschatz, Galvin and Gagne © 2013

Remote Procedure Calls n The semantics of RPCs allows a client to invoke a

Remote Procedure Calls n The semantics of RPCs allows a client to invoke a procedure on a remote host as it would invoke a procedure locally. n Stubs – client-side proxy for the actual procedure on the server n The RPC system hides the details that allow communication to take place by providing a stub on the client side. n Typically, a separate stub exists for each separate remote procedure. n When the client invokes a remote procedure, the RPC system calls the appropriate stub, passing it the parameters provided to the remote procedure. n This stub locates the port on the server and marshals the parameters. n Parameter marshalling involves packaging the parameters into a form that can be transmitted over a network. n The stub then transmits a message to the server using message passing. n A similar stub on the server side receives this message and invokes the procedure on the server. n If necessary, return values are passed back to the client using the same technique. Operating System Concepts – 9 th Edition 3. 38 Silberschatz, Galvin and Gagne © 2013

End of Chapter 3 Operating System Concepts – 9 th Edition Silberschatz, Galvin and

End of Chapter 3 Operating System Concepts – 9 th Edition Silberschatz, Galvin and Gagne © 2013