Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin and Gagne  2002 4.1 Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.

Similar presentations


Presentation on theme: "Silberschatz, Galvin and Gagne  2002 4.1 Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating."— Presentation transcript:

1 Silberschatz, Galvin and Gagne  2002 4.1 Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems

2 Silberschatz, Galvin and Gagne  2002 4.2 Operating System Concepts 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.

3 Silberschatz, Galvin and Gagne  2002 4.3 Operating System Concepts 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.

4 Silberschatz, Galvin and Gagne  2002 4.4 Operating System Concepts Diagram of Process State

5 Silberschatz, Galvin and Gagne  2002 4.5 Operating System Concepts 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.

6 Silberschatz, Galvin and Gagne  2002 4.6 Operating System Concepts Process Control Block (PCB)

7 Silberschatz, Galvin and Gagne  2002 4.7 Operating System Concepts CPU Switch From Process to Process

8 Silberschatz, Galvin and Gagne  2002 4.8 Operating System Concepts 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.

9 Silberschatz, Galvin and Gagne  2002 4.9 Operating System Concepts Ready Queue And Various I/O Device Queues

10 Silberschatz, Galvin and Gagne  2002 4.10 Operating System Concepts Representation of Process Scheduling Figure 4.5

11 Silberschatz, Galvin and Gagne  2002 4.11 Operating System Concepts 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.

12 Silberschatz, Galvin and Gagne  2002 4.12 Operating System Concepts 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.

13 Silberschatz, Galvin and Gagne  2002 4.13 Operating System Concepts 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.

14 Silberschatz, Galvin and Gagne  2002 4.14 Operating System Concepts Addition of Medium Term Scheduling Figure 4.6

15 Silberschatz, Galvin and Gagne  2002 4.15 Operating System Concepts 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.

16 Silberschatz, Galvin and Gagne  2002 4.16 Operating System Concepts 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.

17 Silberschatz, Galvin and Gagne  2002 4.17 Operating System Concepts 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.

18 Silberschatz, Galvin and Gagne  2002 4.18 Operating System Concepts 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.

19 Silberschatz, Galvin and Gagne  2002 4.19 Operating System Concepts 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

20 Silberschatz, Galvin and Gagne  2002 4.20 Operating System Concepts Processes Tree on a UNIX System

21 Silberschatz, Galvin and Gagne  2002 4.21 Operating System Concepts 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.

22 Silberschatz, Galvin and Gagne  2002 4.22 Operating System Concepts 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.

23 Silberschatz, Galvin and Gagne  2002 4.23 Operating System Concepts 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.

24 Silberschatz, Galvin and Gagne  2002 4.24 Operating System Concepts 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

25 Silberschatz, Galvin and Gagne  2002 4.25 Operating System Concepts 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.

26 Silberschatz, Galvin and Gagne  2002 4.26 Operating System Concepts 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; }

27 Silberschatz, Galvin and Gagne  2002 4.27 Operating System Concepts 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; }

28 Silberschatz, Galvin and Gagne  2002 4.28 Operating System Concepts 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.

29 Silberschatz, Galvin and Gagne  2002 4.29 Operating System Concepts 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  physical (e.g., shared memory, hardware bus)  logical (e.g., Direct or indirect communication, Symmetric or asymmetric communication)

30 Silberschatz, Galvin and Gagne  2002 4.30 Operating System Concepts 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.

31 Silberschatz, Galvin and Gagne  2002 4.31 Operating System Concepts 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).

32 Silberschatz, Galvin and Gagne  2002 4.32 Operating System Concepts 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

33 Silberschatz, Galvin and Gagne  2002 4.33 Operating System Concepts Indirect Communication (cont…) Mailbox sharing  P 1, P 2, and P 3 share mailbox A.  P 1, sends; P 2 and P 3 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.

34 Silberschatz, Galvin and Gagne  2002 4.34 Operating System Concepts 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.

35 Silberschatz, Galvin and Gagne  2002 4.35 Operating System Concepts 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 messages Sender must wait for receiver (the link cannot have any messages waiting in it,( rendezvous)). 2.Bounded capacity – finite length of n messages Sender must wait if link full. 3.Unbounded capacity – infinite length Sender never waits.

36 Silberschatz, Galvin and Gagne  2002 4.36 Operating System Concepts Client-Server Communication Sockets Remote Procedure Calls Remote Method Invocation (Java)

37 Silberschatz, Galvin and Gagne  2002 4.37 Operating System Concepts 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.

38 Silberschatz, Galvin and Gagne  2002 4.38 Operating System Concepts 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

39 Silberschatz, Galvin and Gagne  2002 4.39 Operating System Concepts 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.

40 Silberschatz, Galvin and Gagne  2002 4.40 Operating System Concepts Execution of RPC :server

41 Silberschatz, Galvin and Gagne  2002 4.41 Operating System Concepts 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.

42 Silberschatz, Galvin and Gagne  2002 4.42 Operating System Concepts Marshalling Parameters The skeleton is responsible for unmarshalling the parameters and invoking the desired method on the server.


Download ppt "Silberschatz, Galvin and Gagne  2002 4.1 Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating."

Similar presentations


Ads by Google