Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.

Similar presentations


Presentation on theme: "Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create."— Presentation transcript:

1 Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create their own socket. –Communication is within a single machine or between two machines i.e. communication between unrelated processes. –In either case, two sockets must be connected before they can transfer data. There are two kinds of sockets: –Stream –Datagram

2 Inter-Process Communication 9.2 Socket Communication Sending Process Receiving Process Socket Connection Network } } User Space Kernel Space

3 Inter-Process Communication 9.3 Socket Addresses A socket will have an address bound to it. The address format depends on the communication domain. There are three domains (or Address formats): –AF_UNIX The Unix domain –AF_NS XEROX Network Services (NS). –AF_INET Internet sockets –TCP/IP Internet Addresses (Transmission Control Protocol, Internet Protocol) –Originated by DARPA

4 Inter-Process Communication 9.4 Socket Addresses (cont.) The UNIX address domain: –uses an ordinary file path name as an address –Example: bind(s, “. / mySocket”) connect(s, “. / mySocket”) –These are only valid with a single machine. The Internet address domain: –TCP (for stream sockets) –UDP (for datagram sockets) –The address is a pair (machineID, port_#) machineID is an IP address (e.g.: 129.97.114.12) port_# is an integer uniquely identifying a port on that machine. Issue: The problem of finding out the address of the other party –Within a single machine we use conventional file system mechanisms –For inter-machine communication we use symbolic names and name servers. (a hierarchy of name servers actually).

5 Inter-Process Communication 9.5 Socket Types Stream Sockets –duplex –several naming schemes –connection oriented –no message boundaries –reliable and ordered. –Done by TCP in the Internet domain –Pipes in the Unix domain can be implemented in terms of 2 sockets. Datagram sockets –duplex –several naming schemes –datagram-oriented –unreliable and unordered. Raw Sockets –provide very low level access to protocols supporting the other types.

6 Inter-Process Communication 9.6 Flowchart for Datagram Sockets socket bind sendtorecvfrom close

7 Inter-Process Communication 9.7 The Unix Datagram Socket Interface This is a description of how process 1 would receive data from process 2. –somewhat simplified Process 1: sd = socket(addressDomain, SOCK_DGRAM); –addressDomain: Unix or IP –the call creates a socket descriptor Same name space as the file descriptors. bind(sd, myAddress, length); –associates a name with the socket so that another process can communicate with it recvfrom(sd, buf, buflength, sourceAddress); –the address of the sender is returned in sourceAddress –this syscall will block if there is no message waiting.

8 Inter-Process Communication 9.8 The Unix Datagram Socket Interface (cont.) Process 2: sd = socket(addressDomain, SOCK_DGRAM); sendto(sd, buf, buflength, targetAddress); –the send causes an address to be assigned so a reply can be addressed or the sender may explicitly bind before the sendto –targetAddress is the name supplied as myAddress by process 1 –sendto will block if the receiving socket has no space for the message. –There is a sample of working datagram code on the CS354 website.

9 Inter-Process Communication 9.9 Flowcharts for Stream Sockets socket bind connect sendrecv shutdown close socket bind accept sendrecv shutdown close listen Active Passive

10 Inter-Process Communication 9.10 Client-Server & Stream Sockets The socket s2 is the “new” sd generated after the accept. –A fork will then be used to generate a child to handle the new socket. S Process 2 Process 3 Process 1 Queue of connection requests S S 2 S 3 Socket

11 Inter-Process Communication 9.11 The Unix Stream Socket Interface This is a description of how process 1 would receive data from process 2. Process 1 (the passive process): sd = socket(addressDomain, SOCK_STREAM); bind(sd, myAddress); –associates a name with the socket so that another process can communicate with it listen(sd, backlog); –establish a queue of pending connections –tells the kernel it is ready to accept connections –backlog represents the max number of connections to queue nsd = accept(sd, sourceAddress); –accept blocks if there are no connection requests queued –the address of the sender is returned in sourceAddress –accept creates a new socket –processes may use read and write on a socket descriptor after the connection has been established recv(nsd, buf, buflength); –this syscall will block if there is no message waiting send(nsd, buf, bufLength); –send blocks if the receiver has no space

12 Inter-Process Communication 9.12 The Unix Stream Socket Interface (cont.) Process 2 (the active process): sd = socket(addressDomain, SOCK_STREAM); connect(sd, targetAddress); –connect to an existing bound socket send(sd, buf, bufLength); recv(sd, buf, buflength); –the active process may also execute bind, but if not, sender’s socket is assigned an address when the send occurs shutdown(sd, whichDirOrBoth); –inhibits further sends, receives, or both close(sd); –releases the socket descriptor connect() and accept() block until the partner executes –the active and passive processes rendezvous when both a connect and a listen have occurred


Download ppt "Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create."

Similar presentations


Ads by Google