Title: CSS434: Parallel
1CSS490 Message Passing Textbook Ch2 - 3
Instructor Munehiro Fukuda These slides were
compiled from the course textbook, the reference
books, and the instructorç—´ original materials.
2OSI 7 Layers
Site A
Site B
Application protocol
7
Application
rsh, ftp, Telnet
Application
Presentation protocol
Presentation
6
Dealing with heterogeneity And cryptography
Presentation
Session protocol
5
Session
Dialog control (rarely supported)
Session
Transport protocol
Transport
4
UDP, TCP
Transport
Network protocol
Network
Network
3
IP
Data link protocol
IEEE802.2 connection or connectionless
Data link
Data link
2
Ethernet
Physical protocol
Physical
1
Physical
Network
3Physical/Data Link LayerExample CSMA/CD and
Token Ring
- IEEE802.3 CSMA/CD (Carrier sense multiple access
with collision detection) - Listening to the shared medium
- Transmitting a data packet
- Detecting collision on the medium
- Deferring and retransmitting a packet in 2k釦ime
base collision window - IEEE802.5 Token Ring
- Receiving a free token from my (left) neighbor
- Attaching a data packet to the token
- Forwarding the token to my (right) neighbor
- Detaching a packet from the token if it is
addressed here
1 listen
3. detect
2 transmit
?
1. Free token
2. Attach
4. Detach
3. busy token
4Network LayerExample IP
Transportation layer
Datagram
fragmentation
reassembly
Data link layer
Best-effort deliver semantics
Class D for broadcasting
5Transport LayerExample1 UDP
- User Datagram Protocol
- Connectionless
- May be lost
- No FIFO order
- Multicast feature
- Unix datagram
- Example TFTP, rwho
client
server
socket()
socket()
Create a sock descriptor
bind()
bind()
Bind it to an IP address
recvfrom()
sendto()
Blocks until data received
sendto()
recvfrom()
6Transport LayerExample2 TCP
client
server
- Transport Control Protocol
- Connection-oriented
- Reliable
- FIFO order
- No Multicast feature
- Unix stream socket
- Example ftp, http, rsh all major applications
socket()
socket()
Create a sock descriptor
bind()
Bind it to an IP address
liseten()
Declare this is connection-oriented
accept()
Wait for a connection
Connection established
connect()
Blocks until connection established
read()
write()
read()
wrte()
7Application LayerExample RSH
Client
Server
inetd
shell
TCP connection request
rshd
Command rsh ls- l
shell
TCP connection Inherited all the way To a child
Command ls -l
8Socket Programming Socket.h
include ltiostreamgt extern "C" include
ltsys/types.hgt // for sockets include
ltsys/socket.hgt include ltnetinet/in.hgt include
ltarpa/inet.hgt include ltnetdb.hgt // for
gethostbyname( ) include ltunistd.hgt // for
close( ) include ltstring.hgt // for bzero(
) define NULL_FD -1 define MAXSIZE 20 class
Socket public Socket( int ) Socket( )
int getClientSocket( char ) int
getServerSocket( ) private int port int
clientFd int serverFd
9Socket Programming Socket.cpp (Client)
// Fill in the structure "sendSockAddr" with the
server address. sockaddr_in sendSockAddr
bzero( (char)sendSockAddr, sizeof( sendSockAddr
) ) sendSockAddr.sin_family AF_INET
//Address Family Internet sendSockAddr.sin_addr.
s_addr inet_addr( inet_ntoa( (struct
in_addr)host-gth_addr_list ) )
sendSockAddr.sin_port htons( port )
// Open a TCP socket (an Internet strem socket).
if( ( clientFd socket( AF_INET, SOCK_STREAM, 0
) ) lt 0 ) cerr ltlt "Cannot open a client TCP
socket." ltlt endl return NULL_FD //
Connect to the server. while ( connect(
clientFd, (sockaddr)sendSockAddr, sizeof(
sendSockAddr ) ) lt 0 ) // Connected return
clientFd
include "Socket.h" SocketSocket( int port )
port( port ), clientFd( NULL_FD ),
serverFd( NULL_FD ) SocketSocket( )
if ( clientFd ! NULL_FD ) close( clientFd
) if ( serverFd ! NULL_FD ) close(
serverFd ) int SocketgetClientSocket( char
ipName ) // Get the host entry
corresponding to ipName struct hostent host
gethostbyname( ipName ) if( host NULL )
cerr ltlt "Cannot find hostname." ltlt endl
return NULL_FD
10Socket Programming Socket.cpp (Server)
int SocketgetServerSocket( ) if ( serverFd
NULL_FD ) // Server not ready
sockaddr_in acceptSockAddr // Open a TCP
socket (an internet stream socket). if( (
serverFd socket( AF_INET, SOCK_STREAM, 0 ) ) lt
0 ) cerr ltlt "Cannot open a server TCP
socket." ltlt endl return NULL_FD
// Bind our local address so that the client
can send to us bzero( (char)acceptSockAddr,
sizeof( acceptSockAddr ) )
acceptSockAddr.sin_family AF_INET //
Address Family Internet acceptSockAddr.sin_add
r.s_addr htonl( INADDR_ANY )
acceptSockAddr.sin_port htons( port )
if( bind( serverFd, (sockaddr)acceptSockA
ddr, sizeof( acceptSockAddr ) ) lt 0 )
cerr ltlt "Cannot bind the local address to the
server socket." ltlt endl return NULL_FD
listen( serverFd, 5 ) //
Read to accept new requests int newFd
NULL_FD sockaddr_in newSockAddr socklen_t
newSockAddrSize sizeof( newSockAddr ) if(
( newFd accept( serverFd, (sockaddr)newSockAd
dr, newSockAddrSize ) ) lt 0 ) cerr ltlt
"Cannot accept from another host." ltlt endl
return NULL_FD return newFd
11Socket Programming Main
include "Socket.h" define PORT 10000 // You are
given a specific pot from the instructor int
main( int argc, char argv ) Socket sock(
PORT ) int fd if ( argc 1 ) // I'm a
server while ( true ) if ( ( fd
sock.getServerSocket( ) ) NULL_FD ) return
-1 char recvMessageMAXSIZE read(
fd, recvMessage, MAXSIZE ) cout ltlt
recvMessage ltlt endl close( fd )
if ( argc 2 ) // I'm a client if ( (
fd sock.getClientSocket( argv1 ) ) NULL_FD
) return -1 char sendMessageMAXSIZE
cin gtgt sendMessage write( fd,
sendMessage, MAXSIZE ) return 0
12Blocking/Non-Blocking Communication
- Blocking communication
- TCP, UDP, and other communication packages
- Client blocked only when the destination buffer
is full - Server blocked if no message has arrived from
the client. - Rendezvous
- Client blocked for a server to receive a message
- Server blocked if no message has arrived from
the client. - Non-blocking communication
- Server does not want to be blocked when
- It may receive a message from a different client.
- It has another jobs to be done such as
computation or message transmission. - Some synchronization is necessary later.
13Synchronization in Non-Blocking Communication
- Polling
- Periodically check if a socket is ready to read
data - Example
- sd socket( AF_INET, SOCKET_STREAM, 0)
- set_fl(sd, O_NONBLOCK) // set the socket as
non-blocking - struct pollfd pfd
- pfd.fd sd
- poll( pfd, 1, timeout ) // poll the socket
status - Interrupt
- Notified from the system when a socket is ready
to read data - Example
- sd socket(AF_INET, SOCKET_STREAM, 0)
- signal(SIGIO, sigio_func) // set a future
interrupt to call sigio_func( ) - fcntl(sd, F_SETOWN, getpid( )) // ask OS to
deliver this fd interrupt to me - fcntl(sd, F_SETFL, FASYNC) // set this fd
asynchronous - int sigio_func( ) // invoked upon an interrupt
14Buffering
- No Buffering
- A message remains on the sender until the
receiver issues receive( ). - Rendezvous
- Performance drawback
- Single Message Buffer
- The sender can send at most one message even if
the receiver has not issued receive( ). - Stop-and-wait protocol
- A message can be kept read in advance.
- What if the sender has multiple messages?
- Finite-Bound Buffer
- Unsuccessful communication - Go-Back-N Technique
- Flow-controlled communication - sliding window in
TCP - Socket capable of changing its buffer size with
setsockopt( )
message
message
message
message
message
message
15Process Addressing
- Explicit address
- machine id local id
- Example TCP/IP and UDP/IP use IP port
- Demerit No process migration allowed
- machine id local id the last machine id
- Process migration allowed
- Messages forwarded along links to the final
destination - Receiver informing sender of its last machine id
- Sender using this info from the following
messages - Implicit addressing
- System-wide id (function name)
- Example RPC
- Name server required
16Failure Handling
- Loss of request message
- Loss of response message
- Unsuccessful execution of request
- Do we really need acknowledgment messages?
17Idempotency
server
server
client
- A pair of request and response is enough to
handle faults - Idempotency assumed
- At-least one semantics
- Last-one semantics
client
request
Timeout
request
request 2
Timeout
response
request 3
Timeout
response
request 4
reesponse 2
18Exactly-One Semantics
- What if errors in the banking system
- New semantics required
- Exactly-one semantics
- Server must keep track of the request sequence
Withdraw 100
Withdraw995 100
1000-100 900
1000-100 900 for Trans995
Not received
Not received
Withdraw 100
Withdraw995 100
Trans995 completed No subtraction
900-100 800!
100 received
100 received
19Exercises (No turn-in)
- Why do we need layered network protocols?
- When implementing TCP with datagram, what do we
have to take care of? - Consider the pros and cons of polling and
interrupt in non-blocking communication. - Consider an example inducing an accidental system
hang-up (named a deadlock) in no-buffering
communication. - Which of the following operations are idempotent?
- cin gtgt data
- ifstream infile(妬nput.txt