Interprocess Communication Interprocess Communication Outline Interprocess Communication 1 IPC Theory What is IPC? IPC via Shared Memory Eike Ritter 1 IPC via Message Passing 2 Sockets,RPC, and RMI Sockets Modified: October 29, 2012 Remote Procedure Calls 3 IPC in Practice Shared Memory in POSIX systems Lecture 14: Operating Systems with C/C++ IPC in Mac OS X and Windows XP/Vista School of Computer Science, University of Birmingham, UK 1 Based on material by Matt Smart and Nick Blundell Interprocess Communication Interprocess Communication IPC Theory IPC Theory What is IPC? What is IPC? Interprocess Communication Communication Models: Message Passing and shared Memory Processes within a system may be independent or cooperating Cooperating process can affect or be affected by other processes. Reasons for cooperating processes: Information sharing, e.g. shared files Computation speed-up (sometimes, depending on hardware) Modularity Convenience Cooperating processes need some mechanism of interprocess communication (IPC), which is provided by their OS. a – Message passing via the kernel. Two models of IPC b – Use of shared memory: more efficient, since less (or Shared memory no) context switching, though complicated by shared data Message passing concurrency issues.
Interprocess Communication Interprocess Communication IPC Theory IPC Theory IPC via Shared Memory IPC via Shared Memory Producer-Consumer Problem: Bounded-Buffer Producer Process Code Solution // Produce items until the cows come home. Useful problem for understanding issues of processes that while (TRUE) { cooperated via shared memory. // Produce an item. producer process produces information into some buffer Item next_produced = [some new item to add to buffer]; that is consumed by some consumer process. Note, this example does not take into consideration // Wait one place behind the next item to be consumed - so we don’t important concurrency issues, which we will explore in a // write to items that have yet to be consumed. later lecture. while (((in + 1) % BUFFER_SIZE) == out); // <- Spin on condition // Store the new item and increment the ’in’ index. #define BUFFER_SIZE 10 buffer[in] = next_produced; // Define the thing we wish to store. typedef struct { in = (in + 1) % BUFFER_SIZE; ... } } Item; // Define a buffer of items with two indexes: in and out. // Buffer is empty when in==out; // full when ((in+1)%BUFFER_SIZE)==out Item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Interprocess Communication Interprocess Communication IPC Theory IPC Theory IPC via Shared Memory IPC via Message Passing Consumer Process Code IPC via Message Passing Mechanism for processes to communicate and to synchronize their actions while (true) { // Wait until there is something to consume. Message system - processes communicate with each while (in == out); // <- Spin on condition other without resorting to shared variables // Get the next item from the buffer IPC facility in the OS provides two operations: Item next_item = buffer[out]; [process next_item] send(message) receive(message) // Increment the out index. If two processes wish to communicate, they need to: out = (out + 1) % BUFFER_SIZE; } establish a communication link between them exchange messages via send/receive Implementation of communication link/channel physical (e.g., shared memory, hardware bus) - we will not worry about the physical implementation here. logical (e.g., abstract channel that may utilise one of many physical technologies, such ethernet, wireless, and several protocol layers, such as IP/UDP | TCP)
Interprocess Communication Interprocess Communication IPC Theory IPC Theory IPC via Message Passing IPC via Message Passing Implementation Questions Direct Communication How are links established? Under direct communication, processes must name each Can a link be associated with more than two processes? other explicitly : How many links can there be between every pair of send (P, message) - send a message to process P communicating processes? receive(Q, message) - receive a message from process Q What is the capacity of a link? (But, we could also only name the recipient) Is the size of a message that the link can accommodate Properties of communication link fixed or variable? Links are established automatically Is a link unidirectional (one way) or bi-directional (two A link is associated with exactly one pair of communicating way)? processes Between each pair there exists exactly one link The link may be unidirectional, but is usually bi-directional Either way, we suffer a lack of modularity from processes communicating this way Interprocess Communication Interprocess Communication IPC Theory IPC Theory IPC via Message Passing IPC via Message Passing Indirect Communication Indirect Communication More flexible, and so more common communication Operations mechanism. create a new mailbox Messages are sent to and received from mailboxes (a.k.a send and receive messages through mailbox ports) destroy a mailbox Each mailbox has a unique id Primitives are defined as: Processes can communicate only if they share a mailbox create() - returns the ID of a new mailbox ( i.e. if they know the ID of the mailbox) send(A, message) - send a message to mailbox A Properties of communication link receive(A, message) - receive a message from mailbox A destroy(A) - destroy mailbox A 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 (send but not receive) or bi-directional (send and receive)
Interprocess Communication Interprocess Communication IPC Theory IPC Theory IPC via Message Passing IPC via Message Passing Indirect Communication Synchronisation Mailbox sharing Message passing may be either blocking or non-blocking Blocking is considered synchronous P 1 , P 2 , and P 3 share mailbox A P 1 , sends; P 2 and P 3 receive Blocking send has the sender block until the message is Who gets the message - P 2 or P 3 ? received Blocking receive has the receiver block until a message is Solutions available Allow a link to be associated with at most two processes Non-blocking is considered asynchronous Allow only one process at a time to execute a receive operation Non-blocking send has the sender send the message and Allow the system to select arbitrarily the receiver. Sender is continue notified who the receiver was. Non-blocking receive has the receiver receive a valid message or null If both send and receive are implemented as blocking, we get a rendezvous Interprocess Communication Interprocess Communication IPC Theory Sockets,RPC, and RMI IPC via Message Passing Sockets Buffering Sockets Queue of messages attached to the link; implemented in A socket is defined as an abstract endpoint for one of three ways communication, and is named as the concatenation of IP address and port Zero capacity - 0 messages (a.k.a rendezvous) It is abstract in the sense the the particular network medium Sender must wait for receiver is hidden from the application programmer ( e.g. wireless, Bounded capacity - finite length of n messages wired, network protocols, etc. ) Sender must wait if link full The socket 161.25.19.8:1625 refers to port 1625 on host Unbounded capacity - infinite length 161.25.19.8 Sender never waits Communication happens between a pair of sockets Applications read from and write to sockets.
Interprocess Communication Interprocess Communication Sockets,RPC, and RMI Sockets,RPC, and RMI Sockets Remote Procedure Calls Socket Communication Remote Procedure Calls Remote procedure call (RPC) abstracts procedure calls between processes on networked systems RPC is built on top of some message-based communication channel ( e.g. Sockets) Obviously, one process cannot call a function directly on another process (they are in a different address space), especially a remote one, so there is a trick to this: Client uses a stub - a client-side proxy for the actual procedure on the server The client-side stub locates the server and marshalls the parameters ( e.g. native datatypes of the caller) into some universally understood form ( e.g. XML, big-endian, little-endian, etc. ) The server-side skeleton (the reciprocal of the stub) receives this message, unpacks the marshalled parameters, and executes the requested procedure on the server Interprocess Communication Interprocess Communication Sockets,RPC, and RMI Sockets,RPC, and RMI Remote Procedure Calls Remote Procedure Calls Remote Procedure Calls RPC Architecture The whole point of RPC is to reduce the complexity of network programming by giving the illusion that we can simply call methods on remote machines, without worrying about how to structure the low-level messages. And since message structuring is automated by the RPC middleware (a fancy name for software that tries to hide complexity of networking), it is very easy to extend a distributed system by adding new procedures and datatypes.
Recommend
More recommend