Вы находитесь на странице: 1из 64

Interprocess Communication (IPC)

Thecharacteristicsofprotocolsfor
communicationbetweenprocessesina
distributedsystem

Exploring the Middleware Layers

Applications,services
RMIandRPCand
RequestReplyProtocol
MarshallingandExternalDataRepresentation
UDPandTCP
OperatingSystem

Characteristics of IPC
Messagepassingbetweenapairofprocesses
supportedbySENDandRECEIVEoperations

Synchronoussendingandreceivingprocesses
synchronizeeverymessage,andBLOCK

AsynchronoussendingisNONBLOCKING,
receivingcanbebothBLOCKINGandNON
BLOCKING

Nonblockingreceivesarecomplex,somost
systemsemploytheblockingformofreceive

Other IPC Characteristics


Messagedestinationstypicallyspecifiedas
address/portpairs(endpoints)

ReliabilitybothreliableandunreliableIPCsare
possible

Orderingoften,applicationsrequireSENDER
ORDERINGtobemaintained

Example IPC Mechanism - Sockets

socket

anyport

agreedport

socket

message
client

server
otherports

Internetaddress=138.37.94.248

Internetaddress=138.37.88.249

UDP Datagram Communication


DatagramssentwithoutACKsorretries

Messagesizesareoftenprenegotiated

Fragmentationcanoccur

Blockingsendsandreceivesarecommon
timeoutscanbeused,butthesecanbetricky

Datagramdiscardingoccurswhennoreceiving
processiswaiting

UDP's Failure Model


OmissionFailuresmessagesdropped,
checksumerrors,lackofbufferspace

Bothsendomissionsandreceiveomissionscan
occur

Orderingmessagescanarriveoutoforder

ApplicationsthatuseUDPneedtoprovidetheir
ownchecks

Usages of UDP
Applicationsthatdonotsufferfromthe
overheadsassociatedwithguaranteedmessage
delivery

DNS

VoIP

Example UDP Client in Java


importjava.net.*;
importjava.io.*;
publicclassUDPClient{
publicstaticvoidmain(Stringargs[]){
//argsgivemessagecontentsandserverhostname
DatagramSocketaSocket=null;
try{
aSocket=newDatagramSocket();
byte[]m=args[0].getBytes();
InetAddressaHost=InetAddress.getByName(args[1]);
intserverPort=6789;

DatagramPacketrequest=newDatagramPacket(m,args[0].length(),aHost,
serverPort);
aSocket.send(request);

byte[]buffer=newbyte[1000];
DatagramPacketreply=newDatagramPacket(buffer,buffer.length);
aSocket.receive(reply);
System.out.println("Reply:"+newString(reply.getData()));
}catch(SocketExceptione){System.out.println("Socket:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(aSocket!=null)aSocket.close();}
}
}

Example UDP Server in Java


importjava.net.*;
importjava.io.*;
publicclassUDPServer{
publicstaticvoidmain(Stringargs[]){
DatagramSocketaSocket=null;
try{

aSocket=newDatagramSocket(6789);
byte[]buffer=newbyte[1000];

while(true){

DatagramPacketrequest=newDatagramPacket(buffer,
buffer.length);

aSocket.receive(request);

DatagramPacketreply=newDatagramPacket(request.getData(),

request.getLength(),request.getAddress(),request.getPort());
aSocket.send(reply);
}
}catch(SocketExceptione){System.out.println("Socket:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(aSocket!=null)aSocket.close();}
}
}

TCP Streamed Communication


Streamofbytestransferredfromsendertoreceiver

Characteristicsofthenetworkarehidden/transparent
toapplications

Messagessizescanbesmallorlarge

AnACKschemedealswithlostmessages

Flowcontrolmechanismsthrottlefastsenders

Messageduplicationishandled,orderingis
maintained

Messagedestinationsare"streamendpoints"

More of TCP
Whenestablishingcommunication,onesideis
theclient,theotheristheserver

Thereafter,bothcanoperateaspeers,ifneeds
be

Pairsofsocketsareconnectedbypairsof
streams,oneforinput,theotherforoutput

TCP's Failure Model


Checksumsdetectandrejectcorruptpackets

Sequencenumbersdetectandrejectduplicate
packets

Timeoutsandretransmissionsdealwithlost
packets

TCPisnottotallyreliable,asitdoesnot
guaranteedeliveryofmessagesinthefaceofall
possibledifficulties

TCP's Unreliability
Whenaconnectionisbroken,aprocessis
notifiedifitattemptstoreadorwrite

Hasthenetworkfailedorhastheprocessatthe
otherendpointfailed?

Whereareprevioussentmessagesactually
received?

Example TCP Client in Java


importjava.net.*;
importjava.io.*;
publicclassTCPClient{
publicstaticvoidmain(Stringargs[]){
//argumentssupplymessageandhostnameofdestination
Sockets=null;
try{

intserverPort=7896;

s=newSocket(args[1],serverPort);
DataInputStreamin=newDataInputStream(s.getInputStream());
DataOutputStreamout=
newDataOutputStream(s.getOutputStream());
out.writeUTF(args[0]);
//UTFisastringencodingseeSn4.3
Stringdata=in.readUTF();
System.out.println("Received:"+data);
}catch(UnknownHostExceptione){
System.out.println("Sock:"+e.getMessage());
}catch(EOFExceptione){System.out.println("EOF:"+e.getMessage());

}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(s!=null)try{s.close();}catch(IOExceptione)
{System.out.println("close:"+e.getMessage());}}

}
}

Example TCP Server in Java


importjava.net.*;
importjava.io.*;
publicclassTCPServer{
publicstaticvoidmain(Stringargs[]){
try{
intserverPort=7896;
ServerSocketlistenSocket=newServerSocket(serverPort);
while(true){
SocketclientSocket=listenSocket.accept();
Connectionc=newConnection(clientSocket);
}
}catch(IOExceptione){System.out.println("Listen:"+e.getMessage());}
}
}
classConnectionextendsThread{
DataInputStreamin;
DataOutputStreamout;
SocketclientSocket;
publicConnection(SocketaClientSocket){
try{
clientSocket=aClientSocket;
in=newDataInputStream(clientSocket.getInputStream());
out=newDataOutputStream(clientSocket.getOutputStream());
this.start();
}catch(IOExceptione){System.out.println("Connection:"+e.getMessage());}
}
publicvoidrun(){
try{
//anechoserver
Stringdata=in.readUTF();

out.writeUTF(data);
}catch(EOFExceptione){System.out.println("EOF:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{try{clientSocket.close();}catch(IOExceptione){/*closefailed*/}}
}
}

IPC and Data

ExternalDataRepresentationandMarshalling

The Problem
Runningprograms(processes)arerepresented
as(binary)datastructures

Informationinmessagesisrepresentedasa
sequenceofbytes

Howdowetransformoneintotheotherandvice
versa?

Flattening
Datastructuresmustbeflattenedintoa
sequenceofbytesbeforetransmissionand
rebuiltonreceipt

Byteordering(littleorbigendian?)isanissue

Characterencodings(ASCII,Unicode)arean
issue,too

Exchanging Binary Data


Valuesareconvertedtoanagreedexternal
format

Valuesaretransmittedinthesender'sformat;the
recipientconvertsthatvaluesifnecessary

Anagreedstandardfortherepresentationofdata
structuresandprimitivevaluesiscalledan
"externaldatarepresentation"

Marshalling and Unmarshalling


Marshallingtakingacollectionofdataitemsand
assemblingthemintoaformsuitablefor
transmissioninamessage

Unmarshallingdisassemblingamessageon
arrivaltoproduceanequivalentcollectionofdata
itemsatthedestination

Three Alternative Approaches


CORBA'sCommonDataRepresentation(CDR)
canbeusedwithavarietyofprogramming
technologies

Java'sObjectSerializationworksonlywithinthe
Javaenvironment

XML(ExtensibleMarkupLanguage)atextual
formatforrepresentingstructureddatathatworks
withanyprogrammingtechnology

CORBA's CDR
CDRcanrepresent15primitivetypesanda
rangeofcompositetypes

Bothlittleandbigendiansupportisprovided
sendersindicateinwhichorderingthemessage
istransmitted

FloatingpointnumbersusetheIEEEstandard

Charactersarerepresentedinacodesetagreed
betweenthesenderandreceiver

DatatypeinformationisNOTtransmitted

CORBA CDR's Composite Types

Type
sequence
string
array
struct
enumerated
union

Representation
length(unsignedlong)followedbyelementsinorder
length(unsignedlong)followedbycharactersinorder(canalso
canhavewidecharacters)
arrayelementsinorder(nolengthspecifiedbecauseitisfixed)
intheorderofdeclarationofthecomponents
unsignedlong(thevaluesarespecifiedbytheorderdeclared)
typetagfollowedbytheselectedmember

CORBA CDR - Example Message


indexin
sequenceofbytes
03
47
811
1215
1619
2023
2427

4bytes
5
"Smit"
"h___"
6
"Lond"
"on__"
1934

notes
onrepresentation
lengthofstring
Smith
lengthofstring
London
unsignedlong

TheflattenedformrepresentsaPersonstructwithvalue:{Smith,London,1934}

Marshalling in CORBA
CORBA'sInterfaceDefinitionLanguage(IDL)is
usedto"automatically"producemarshallingand
unmarshallingoperations

TheCORBAIDLcompilerenablesthegeneration
oftherequiredcomponents

Example CORBA IDL


structPerson
{
stringname;
stringplace;
unsignedlongyear;
};

Java's Object Serialization


Theterm"serialization"referstotheactivityof
flatteninganobjectoraconnectedsetofobjects
intoaserialformthatissuitableforstoringon
diskortransmittinginamessage

Considerthiscode:

Personp=newPerson("Smith","London",1934);

Serialized Form of "p"

Explanation

Serializedvalues
Person

8byteversionnumber

h0

classname,versionnumber

intyear

java.lang.String java.lang.String number,typeandnameof


name:
place:
instancevariables

1934

5Smith

6London

h1

valuesofinstancevariables

Thetrueserializedformcontainsadditionaltypemarkers;h0andh1arehandles

Extensible Markup Language (XML)


A"markuplanguage"referstoatextualencoding
thatrepresentsbothatextanddetailsastoits
structureoritsappearance

HTMLwasdesignedtodescribetheappearance
ofwebpages

XMLwasdesignedtodescribestructured
documentsandmarkuplanguages

XML Characteristics
XMLis"extensible"inthesensethatuserscan
definetheirowntags

XMLis"selfdescribing"

XMLwasintendedtobeusedbymultiple
applicationsfordifferentpurposes

XMLis"textual",socanbeeasilyreadby
humansandcomputers

Example XML (Elements and Attributes)

<personid="123456789">
<name>Smith</name>
<place>London</place>
<year>1934</year>
<!acomment>
</person>

More XML
ThenamesusedinXMLareuserdefinedand
followthenormalnamingconventions

Binarydatais(typically)representedin"base64"

XML Parsing and Well Formed Documents


Everystarttaghasamatchingendtag

Alltagsarenestedcorrectly

AllXMLdocumentshaveasinglerootelement
withinwhichallotherelementsareenclosed

TheCDATAnotationallowsfortheinclusionof
specialcharacters

XML Prologs

<?XMLversion="1.0"encoding="UTF8"
standalone="yes"?>

XML Namespaces
Asetofnamesforacollectionofelementtypes
andattributes

Thenamespaceconventionallowsanapplication
tomakeuseofmultiplesetsofexternaldefinitions
indifferentnamespaceswithouttheriskofname
clashes

Example XML Namespace

<personpers:id="123456789"xmlns:pers="http://www.cdk4.net/person">
<pers:name>Smith</pers:name>
<pers:place>London</pers:place>
<pers:year>1934</pers:year>
</person>

XML Schemas
Definestheelementsandattributesthatcan
appearinadocument

Defineshowtheelementsarenested,theorder
andnumberofelements

Defineswhetherornotanelementisemptyor
canincludetext

Foreachelement,theschemadefinesthetype
anddefaultvalue

XML Schema Example


<xsd:schemaxmlns:xsd=URLofXMLschemadefinitions
>
<xsd:elementname="person"type="personType"/>
<xsd:complexTypename="personType">
<xsd:sequence>
<xsd:elementname="name"type="xs:string"/>
<xsd:elementname="place"type="xs:string"/>
<xsd:elementname="year"type="xs:positiveInteger"/>
</xsd:sequence>
<xsd:attributename="id"type="xs:positiveInteger"/>
</xsd:complexType>
</xsd:schema>

Valid XML Documents

AnXMLdocumentthatisdefinedtoconformto
aparticularschemamayalsobevalidatedby
meansofthatschema(usingoneofthemany
programmingAPIs)

Client-Server Communication
Normally,requestreplycommunicationis
synchronousbecausetheclientprocessblocks
untilthereplyarrivesfromtheserver

Itcanalsobereliableasthereplyfromtheserver
iseffectivelyanacknowledgmenttotheclient

Itispossibletobuildaclientserverprotocolover
areliableorunreliableprotocol

Avoiding Unnecessary Overhead


ACKsareunnecessarywhenrequestsare
followedbyreplies

Establishingaconnectioninvolves(atleast)two
extrapairsofmessagesinadditiontothe
requestreplymessages

Flowcontrolisoverkill,asmostinvocationspass
onlysmallargumentsandresults

Request-Reply Communications
Client

doOperation

Server

Request
message

(wait)

(continuation)

Reply
message

getRequest
selectobject
execute
method
sendReply

The Request-Reply Protocol


publicbyte[]doOperation(RemoteObjectRefo,intmethodId,byte[]arguments)
sendsarequestmessagetotheremoteobjectandreturnsthereply.
Theargumentsspecifytheremoteobject,themethodtobeinvokedandthe
argumentsofthatmethod.
publicbyte[]getRequest();
acquiresaclientrequestviatheserverport.
publicvoidsendReply(byte[]reply,InetAddressclientHost,intclientPort);
sendsthereplymessagereplytotheclientatitsInternetaddressandport.

The Request-Reply Message Structure

messageType

int(0=Request,1=Reply)

requestId

int

objectReference

RemoteObjectRef

methodId

intorMethod

arguments

arrayofbytes

Request-Reply Communication
Characteristics
Whatisthefailuremodel?(Canomissionsoccur?
Ismessageorderingmaintained?)

Aretimeoutsemployedonoperations?

Howareduplicaterequestmessageshandled?

Howarelostreplymessageshandled?

Isahistoryofrequests(andreplies)maintained
oneitherend?

Idempotent Operations

Anoperationisidempotentifitcanbe
executedoneormoretimeswithoutside
effects

An Example Request-Reply
Protocol - HTTP
Allowsfortheinvocationofmethodsonweb
resources

Contentnegotiationisalsosupported

Passwordstyleauthenticationisavailable

How HTTP Works


ImplementedoverTCP

InitiallyemployedasimpleConnectRequest
ReplyClosecycle

Thisprovedtobeexpensiveandinefficient

LatestversionofHTTPsupports"persistent
connections"

HTTP Requests and Replies


R'n'RsaremarshalledintoASCIIstrings

Resourcescanbebytesequencesandmay
becompressed

MultipurposeInternetMailExtensions(MIME)
supportsmultipartmessagesofvarying
formats

HTTP Methods
GET(whichisgenerallyidempotent)

HEAD,POST,PUT,DELETE,OPTIONSand
TRACE(arethemostwidelysupported)

HTTP Request and Reply Messages


method
GET

URLorpathname

HTTPversion

headers messagebody

//www.dcs.qmw.ac.uk/index.html HTTP/1.1

HTTPversion
HTTP/1.1

statuscode reason headers messagebody


200

OK

resourcedata

Group Communication

Thepairwiseexchangeofmessagesisrarely
thebestmodelforcommunicationfromone
processtoagroupofotherprocesses

Group Communication - Multicasting


Themembershipofthegroupistransparentto
thesender

Multicastmessagesprovideauseful
infrastructureforconstructingdistributedsystems

Uses of Multicasting
Faulttolerancebasedonreplicatedservers

Findingthediscoveryserviceinspontaneous
networking

Betterperformancethroughreplicateddata

Propagationofeventnotifications

Group Communications with


IP Multicasting
IPMulticastisbuiltontopofIP

ThesendertransmitsasingleIPpackettoaset
ofcomputersthatformamulticastgroup

Thesenderdoesnotknowtherecipients
identitiesnorhowbigthegroupis

TheclassDaddressspacewithinIPisreserved
forIPMulticast

Characteristics of IP Multicast
AvailablewithUDPonly

IdentifiedbyanIPaddress/portnumberend
point

Applicationscanjoinamulticastgroupby
openingasockettotheendpoint

Multicastaddressrange224.0.0.1through
224.0.0.255

IP Multicast's Failure Model


SameasforUDPdatagrams

Multicastssufferfromomissionfailures

Notallofthegroupmembersreceiveeverything

Reliablemulticastingispossibleoverheadsare
high

Example Multicast Peer in Java


importjava.net.*;
importjava.io.*;
publicclassMulticastPeer{
publicstaticvoidmain(Stringargs[]){

//argsgivemessagecontents&destinationmulticastgroup(e.g."228.5.6.7")
MulticastSockets=null;

try{

InetAddressgroup=InetAddress.getByName(args[1]);

s=newMulticastSocket(6789);

s.joinGroup(group);

byte[]m=args[0].getBytes();

DatagramPacketmessageOut=
newDatagramPacket(m,m.length,group,6789);

s.send(messageOut);
//getmessagesfromothersingroup

byte[]buffer=newbyte[1000];

for(inti=0;i<3;i++){

DatagramPacketmessageIn=
newDatagramPacket(buffer,buffer.length);

s.receive(messageIn);

System.out.println("Received:"+newString(messageIn.getData()));

s.leaveGroup(group);

}catch(SocketExceptione){System.out.println("Socket:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(s!=null)s.close();}
}

Multicasting Reliability and Ordering


Suffersfromomissionfailures!

Recipientsmaydropmessagesduetofullbuffers

Adatagramlostatonemulticastrouterprevents
allthoseroutersbeyondfromreceivingthe
datagram

Amulticastroutercanfail

Messageordering"errors"canresultintwo
routersreceivingasequenceofmulticastsina
verydifferentordertothatwhichwassent

Cast Study - UNIX IPC

TheSocketsystemcallslayeredoverthe
InternetTCPandUDPprotocols

Socket Datagram Communications

Sendingamessage

Receivingamessage

s=socket(AF_INET,SOCK_DGRAM,0)

s=socket(AF_INET,SOCK_DGRAM,0)

bind(s,ClientAddress)

bind(s,ServerAddress)

sendto(s,"message",ServerAddress)

amount=recvfrom(s,buffer,from)

ServerAddressandClientAddressaresocketaddresses

Socket Stream Communications

Requestingaconnection
s=socket(AF_INET,SOCK_STREAM,0)
connect(s,ServerAddress)

Listeningandacceptingaconnection
s=socket(AF_INET,SOCK_STREAM,0)
bind(s,ServerAddress);
listen(s,5);
sNew=accept(s,ClientAddress);

write(s,"message",length)

n=read(sNew,buffer,amount)

ServerAddressandClientAddressaresocketaddresses

IPC Summary - Exchange Protocols


Therequest(R)protocolnovalueneedbe
returnedtotheclient

Therequestreply(RR)protocolspecialACKs
arenotrequired,thereplyandsubsequentnew
requestssufficeasACKs

Therequestreplyackreply(RRA)protocol
usedwhentheservermaintainsahistoryof
messages;the"ackreply"allowstheserverto
removeitemsfromitshistory

Вам также может понравиться