Interprocess Communications Andy Wang COP 5611 Advanced Operating

  • Slides: 76
Download presentation
Interprocess Communications Andy Wang COP 5611 Advanced Operating Systems

Interprocess Communications Andy Wang COP 5611 Advanced Operating Systems

Outline n IPC fundamentals n UNIX sockets n Remote procedural call n Shared memory

Outline n IPC fundamentals n UNIX sockets n Remote procedural call n Shared memory and large memory spaces n IPC in Windows NT

IPC Fundamentals n What is IPC? n Mechanisms to transfer data between processes n

IPC Fundamentals n What is IPC? n Mechanisms to transfer data between processes n Why is it needed? n Not all procedures can be easily built in a single process

Why do processes communicate? n To share resources n Client/server paradigms n Inherently distributed

Why do processes communicate? n To share resources n Client/server paradigms n Inherently distributed applications n Reusable software components n Other good software engineering reasons

The Basic Concept of IPC n A process needs to send data to a

The Basic Concept of IPC n A process needs to send data to a receiving process Sender wants to avoid details of receiver’s condition n Receiver wants to get the data in an organized way n

IPC from the OS Point of View Private address space OS address space Process

IPC from the OS Point of View Private address space OS address space Process A Process B

Fundamental IPC Problem for the OS n Each process has a private address space

Fundamental IPC Problem for the OS n Each process has a private address space n Normally, no process can write to another process’s space n How to get important data from process A to process B?

OS Solutions to IPC Problem n Fundamentally, two options 1. Support some form of

OS Solutions to IPC Problem n Fundamentally, two options 1. Support some form of shared address space n Shared memory 2. Use OS mechanisms to transport data from one address space to another n Files, messages, pipes, RPC

Fundamental Differences in OS Treatment of IPC Solutions n Shared memory n OS has

Fundamental Differences in OS Treatment of IPC Solutions n Shared memory n OS has job of setting it up n And perhaps synchronizing n But not transporting data n Messages, etc n OS involved in every IPC n OS transports data

Desirable IPC Characteristics n Fast n Easy to use n Well defined synchronization model

Desirable IPC Characteristics n Fast n Easy to use n Well defined synchronization model n Versatile n Easy to implement n Works remotely

IPC and Synchronization n Synchronization is a major concern for IPC n Allowing sender

IPC and Synchronization n Synchronization is a major concern for IPC n Allowing sender to indicate when data is transmitted n Allowing receiver to know when data is ready n Allowing both to know when more IPC is possible

IPC and Connections n IPC mechanisms can be connectionless or require connection Connectionless IPC

IPC and Connections n IPC mechanisms can be connectionless or require connection Connectionless IPC mechanisms require no preliminary setup n Connection IPC mechanisms require negotiation and setup before data flows n

Connectionless IPC n Data simply flows n Typically, no permanent data structures shared in

Connectionless IPC n Data simply flows n Typically, no permanent data structures shared in OS by sender and receiver + Good for quick, short communication - Less efficient for large, frequent communications - Each communication takes more OS resources per byte

Connection-oriented IPC n Sender and receiver pre-arrange IPC delivery details n OS typically saves

Connection-oriented IPC n Sender and receiver pre-arrange IPC delivery details n OS typically saves IPC-related info for them n Pros/cons pretty much the opposites of connectionless IPC

Basic IPC Mechanisms n File system n Message-based n Procedure call n Shared memory

Basic IPC Mechanisms n File system n Message-based n Procedure call n Shared memory

IPC Through the File System n Sender writes to a file n Receiver reads

IPC Through the File System n Sender writes to a file n Receiver reads from it n But when does the receiver do the read? n Often synchronized with file locking or lock files n Special types of files can make file-based IPC easier

File IPC Diagram Process A Data Process B

File IPC Diagram Process A Data Process B

Message-based IPC n Sender formats data into a formal message n With some form

Message-based IPC n Sender formats data into a formal message n With some form of address for receiver n OS delivers message to receiver’s message input queue (might signal too) n Receiver (when ready) reads a message from the queue n Sender might or might not block

Message-based IPC Diagram OS Process A Data sent from A to B B’s message

Message-based IPC Diagram OS Process A Data sent from A to B B’s message queue Process B

Procedure Call IPC n Uses same procedure call interface as intraprocess Data passed as

Procedure Call IPC n Uses same procedure call interface as intraprocess Data passed as parameters n Info returned via return values n n Complicated since destination procedure is in a different address space n Generally, calling procedure blocks till call returns

File IPC Diagram main () {. call(); . . . Data as parameters Data

File IPC Diagram main () {. call(); . . . Data as parameters Data as return values . . . server(); . . . } Process A Process B

Shared Memory IPC n Different processes share a piece of memory n Either physically

Shared Memory IPC n Different processes share a piece of memory n Either physically or virtually n Communications via normal reads/writes n May need semaphores or locks n In or associated with the shared memory

Shared Memory IPC Diagram main () {. x = 10. . . write variable

Shared Memory IPC Diagram main () {. x = 10. . . write variable x x: 10 read variable x . . . print(x); . . . } Process A Process B

Synchronizing in IPC n How do sending and receiving process synchronize their communications? n

Synchronizing in IPC n How do sending and receiving process synchronize their communications? n Many possibilities n Based on which process block when n Examples that follow in message context, but more generally applicable

Blocking Send, Blocking Receive n Both sender and receiver block n Sender blocks till

Blocking Send, Blocking Receive n Both sender and receiver block n Sender blocks till receiver receives n Receiver blocks until sender sends n Often called message rendezvous

Non-Blocking Send, Blocking Receive n Sender issues send, can proceed without waiting to discover

Non-Blocking Send, Blocking Receive n Sender issues send, can proceed without waiting to discover fate of message n Receiver waits for message arrival before proceeding n Essentially, receiver is message-driven

Non-Blocking Send, Non-Blocking Receive n Neither party blocks n Sender proceeds after sending message

Non-Blocking Send, Non-Blocking Receive n Neither party blocks n Sender proceeds after sending message n Receiver works until message arrives n Either receiver periodically checks in nonblocking fashion n Or some form of interrupt delivered

Addressing in IPC n How does the sender specify where the data goes? n

Addressing in IPC n How does the sender specify where the data goes? n In some cases, the mechanism makes it explicit (e. g. , shared memory and RPC) n In others, there are options

Direct Addressing n Sender specifies name of the receiving process n Using some form

Direct Addressing n Sender specifies name of the receiving process n Using some form of unique process name n Receiver can either specify name of expected sender n Or take stuff from anyone

Indirect Addressing n Data is sent to queues, mailboxes, or some other form of

Indirect Addressing n Data is sent to queues, mailboxes, or some other form of shared data structure n Receiver performs some form of read operations on that structure n Much more flexible than direct addressing

Duality in IPC Mechanisms n Many aspects of IPC mechanisms are duals of each

Duality in IPC Mechanisms n Many aspects of IPC mechanisms are duals of each other n These mechanisms have the same power n n First recognized in context of messages vs. procedure calls IPC mechanisms can be simulated by each other

So which IPC mechanism to build/choose/use? n Depends on model of computation n And

So which IPC mechanism to build/choose/use? n Depends on model of computation n And on philosophy of user n In particular cases, hardware or existing software may make one perform better

Typical UNIX IPC Mechanisms n Different versions of UNIX introduced different IPC mechanisms Pipes

Typical UNIX IPC Mechanisms n Different versions of UNIX introduced different IPC mechanisms Pipes n Message queues n Semaphores n Shared memory n Sockets n RPC n

Pipes n Only IPC mechanism in early UNIX systems (other than files) Uni-directional n

Pipes n Only IPC mechanism in early UNIX systems (other than files) Uni-directional n Unformatted n Uninterpreted n Interprocess byte streams n n Accessed in file-like way

Pipe Details n One process feeds bytes into pipe n A second process reads

Pipe Details n One process feeds bytes into pipe n A second process reads the bytes from it n Potentially blocking communication mechanism n Requires close cooperation between processes to set up n Named pipes allow more flexibility

Pipes and Blocking n Writing more bytes than pipe capacity blocks the sender n

Pipes and Blocking n Writing more bytes than pipe capacity blocks the sender n Until the receiver reads some of them n Reading bytes when none are available blocks the receiver n Until the sender writes some n Single pipe with bounded capacity can’t cause deadlock

UNIX Message Queues n Introduced in System V Release 3 UNIX n Like pipes,

UNIX Message Queues n Introduced in System V Release 3 UNIX n Like pipes, but data organized into messages n Message component include n Type identifier n Length n Data

Semaphores n Also introduced in System V Release 3 UNIX n Mostly for synchronization

Semaphores n Also introduced in System V Release 3 UNIX n Mostly for synchronization only n Since they only communicate one bit of information n Often used in conjunction with shared memory

UNIX Shared Memory n Also introduced in System V Release 3 n Allows two

UNIX Shared Memory n Also introduced in System V Release 3 n Allows two or more processes to share some memory segments n With some control over read/write permissions n Often used to implement threads packages for UNIX

Sockets n Introduced in 4. 3 BSD n A socket is an IPC channel

Sockets n Introduced in 4. 3 BSD n A socket is an IPC channel with generated endpoints n Great flexibility in its characteristics n Intended as building block for communication n Endpoints established by the source and destination processes

UNIX Remote Procedure Calls n Procedure calls from one address space to another n

UNIX Remote Procedure Calls n Procedure calls from one address space to another n On the same or different machines n Requires cooperation from both processes n In UNIX, often built on sockets n Often used in client/server computing

More on Sockets n Created using the socket() system call n Specifying domain, type,

More on Sockets n Created using the socket() system call n Specifying domain, type, and protocol n Sockets can be connected or connectionless n Each side responsible for proper setup/access

Socket Domains n the socket domain describes a protocol family used by the socket

Socket Domains n the socket domain describes a protocol family used by the socket n Generally related to the address family n Domains can be: n Internal protocols n Internet protocols n IMP (interface message processors) link layer protocols

Socket Types n The socket type describes what the socket does n Several types

Socket Types n The socket type describes what the socket does n Several types are defined SOCK_STREAM n SOCK_DGRAM n SOCK_SEQPACKET n SOCK_RAW n SOCK_RDM n

Socket Protocols n This parameter specifies a particular protocol to be used by the

Socket Protocols n This parameter specifies a particular protocol to be used by the socket n Must match other parameters n Not all protocols usable with all domains and types n Generally, only one protocol per socket type available

Some Examples of Sockets n Socket streams n Socket sequential packets n Socket datagrams

Some Examples of Sockets n Socket streams n Socket sequential packets n Socket datagrams

Socket Streams n Of type SOCK_STREAM n Full-duplex reliable byte streams n Like 2

Socket Streams n Of type SOCK_STREAM n Full-duplex reliable byte streams n Like 2 -way pipes n Requires other side to connect

Socket Sequential Packets n Similar to streams n But for fixed-sized packets n So

Socket Sequential Packets n Similar to streams n But for fixed-sized packets n So reads always return a fixed number of bytes n Allow easy use of buffers

Socket Datagrams n Like sequential packets n But non-reliable delivery n Which implies certain

Socket Datagrams n Like sequential packets n But non-reliable delivery n Which implies certain simplifications n And lower overhead n send(), rather than write(), used to send data

Socket Options n Connection or connectionless n Blocking or non-blocking n Out-of-band information n

Socket Options n Connection or connectionless n Blocking or non-blocking n Out-of-band information n Broadcast n Buffer sizes (input and output) n Routing options n And others

Binding Sockets n Binding prepares a socket for use by a process n Sockets

Binding Sockets n Binding prepares a socket for use by a process n Sockets are typically bound to local names n For IPC on a single machine n Often accessed using descriptors n Requires clean-up when done n Binding can be to IP addresses, as well

Connecting to Sockets n Method for setting up the receiving end of a socket

Connecting to Sockets n Method for setting up the receiving end of a socket n In local domain, similar to opening file n Multiple clients can connect to a socket n Program establishing socket can limit connections

Remote Procedural Call n Method of calling procedures in other address spaces n Either

Remote Procedural Call n Method of calling procedures in other address spaces n Either on the same machine n Or other machines n Attempts to provide interface just like local procedure call n Request/reply communications model

RPC Case Studies n RPC in Cedar n UNIX RPC

RPC Case Studies n RPC in Cedar n UNIX RPC

Semantics of RPC n Similar to regular procedure call 1. 2. 3. 4. 5.

Semantics of RPC n Similar to regular procedure call 1. 2. 3. 4. 5. Calling procedure blocks Parameters transferred to called procedure Called procedure computes till it returns Return value delivered to calling procedure Calling procedure continues

High-Level RPC Mechanics n Hide details from applications n Clients pass requests to stub

High-Level RPC Mechanics n Hide details from applications n Clients pass requests to stub programs n Client-end stub sends request to server stub n Server-end stub calls user-level server n Results travel in reverse direction n Network transport or OS actually moves data

Diagram of RPC in Action Client (caller) call Server (callee) reply Client stub call

Diagram of RPC in Action Client (caller) call Server (callee) reply Client stub call Server stub OS or Network

What do the stubs do? n Stubs handle complex details like: n Marshaling arguments

What do the stubs do? n Stubs handle complex details like: n Marshaling arguments n Message construction n Data format translation n Finding the server process

Setting Up RPC n Caller must have a way to find the called procedure

Setting Up RPC n Caller must have a way to find the called procedure n But it can’t be found at link time n Unlike local procedure n Potential servers must make their presence known

Registering Servers n Either register the server at a “well-known” port n Or register

Registering Servers n Either register the server at a “well-known” port n Or register it with a name server n Which in turn is at a “well-known” port n Calling procedure “addresses” RPC to that port

Binding to a Service n A client process binds to the service it wants

Binding to a Service n A client process binds to the service it wants n Two major sub-problem: n Naming n Location

Binding: The Naming Problem n How does a caller name the server program? n

Binding: The Naming Problem n How does a caller name the server program? n Depends on the RPC mechanism n And perhaps the protocol or type within it n Do you name the server explicitly? n Or do you name the service and let some other authority choose which server?

Binding: The Location Problem n Where is the remote server? n Some naming schemes

Binding: The Location Problem n Where is the remote server? n Some naming schemes make it explicit n Some don’t n If it’s not explicit, system must convert symbolic names to physical locations

Binding in Cedar RPC n Client applications bind to a symbolic name n Composed

Binding in Cedar RPC n Client applications bind to a symbolic name n Composed of: n n Type Instance n Type is which kind of service n Instance is which particular implementer

Locating Cedar RPC Service n Names do not contain physical location n So Cedar

Locating Cedar RPC Service n Names do not contain physical location n So Cedar consults a database of services n Services register with database n Binding call automatically looks up location in database

Binding in UNIX RPC n bind() system call used by servers n Allows servers

Binding in UNIX RPC n bind() system call used by servers n Allows servers to bind naming/location information to particular socket n connect() system call used by clients n Using information similar to that bound by the server n Automatic code generation hides details

UNIX Binding Information n Like most socket operations, it’s flexible n Fill in all

UNIX Binding Information n Like most socket operations, it’s flexible n Fill in all or part of a socket address data structure n Create an appropriate socket n Then call bind to link socket to address information n connect works similarly

UNIX Binding Example n On server side, struct sockaddr_un sin; int sd; strcpy(sin. sun_path,

UNIX Binding Example n On server side, struct sockaddr_un sin; int sd; strcpy(sin. sun_path, ”. /socket”); sd = socket(AF_UNIX, SOCK_STREAM, 0); bind(sd, &sin, sizeof(sin));

UNIX Binding Example, Con’t n For client side, struct sockaddr_un sin; int sd; strcpy(sin.

UNIX Binding Example, Con’t n For client side, struct sockaddr_un sin; int sd; strcpy(sin. sun_path, “. /socket”); sd = socket(AF_UNIX, SOCK_STREAM, 0); connect(sd, &sin, sizeof(sin));

Locating Remote Services in UNIX RPC n Similar to Cedar methods n Register services

Locating Remote Services in UNIX RPC n Similar to Cedar methods n Register services with the portmapper n The portmapper is typically called through automatically generated code n the portmapper runs on each machine n Another service (e. g. , NIS) deals with intermachine requests

Using RPC n Once it’s bound, how does the client use RPC? n Just

Using RPC n Once it’s bound, how does the client use RPC? n Just call the routines n As if they were local n And the results come back

What’s happening under the covers? n When a client calls a remote routine, he

What’s happening under the covers? n When a client calls a remote routine, he really calls a local stub program n Stub program packages up request to remote server n And sends it to local transport code n When reply arrives, local transport code returns results to stub n Which returns to client program

What happens at the server side? n A request comes in to the RPC

What happens at the server side? n A request comes in to the RPC transport code n It routes it to the appropriate server stub n Which converts it into a local procedure call n Which is made within the context of the server

Conceptual Diagram of RPC call_procedure(); client program continues Message from client stub to server

Conceptual Diagram of RPC call_procedure(); client program continues Message from client stub to server stub Client stub converts message to return value Network transport Message received by server stub Return value formatted as message to client stub call_procedure(); return(…);

Transport for RPC n In Cedar, special-purpose RPC transport protocol n In UNIX RPC,

Transport for RPC n In Cedar, special-purpose RPC transport protocol n In UNIX RPC, can use either UDP or TCP for transport n Typically protocol is chosen automatically by stub generator program

Other RPC Issues n Mostly related to intermachine RPC n Data format conversions n

Other RPC Issues n Mostly related to intermachine RPC n Data format conversions n Security and authentication n Locating remote services n Multipacket RPC