Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSS490 Message Passing
Textbook Ch2 - 3
Instructor: Munehiro Fukuda
These slides were compiled from the course textbook, the reference books, and the
instructor’s original materials.
Winter, 2004
CSS490 Message Passing
1
OSI 7 Layers
Site
A
7
Application
6
Presentation
5
Session
4
Transport
3
Network
2
1
Data link
Physical
Site
B
Application protocol
Presentation protocol
Session protocol
Transport protocol
Network protocol
Data link protocol
Physical protocol
Application
Presentation
Session
rsh, ftp, Telnet
Dealing with heterogeneity
And cryptography
Dialog control
(rarely supported)
Transport
UDP, TCP
Network
IP
Data link
IEEE802.2
connection or connectionless
Ethernet
Physical
Network
Winter, 2004
CSS490 Message Passing
2
Physical/Data Link Layer
Example: CSMA/CD and Token Ring
1 listen
3. detect
Ⅹ
2 transmit
1. Free token
2. Attach
3. busy token
Winter, 2004
IEEE802.3: CSMA/CD (Carrier sense
multiple access with collision detection)
1.
Listening to the shared medium
2.
Transmitting a data packet
3.
Detecting collision on the medium
4.
Deferring and retransmitting a packet
in 2k–time base collision window
IEEE802.5: Token Ring
1.
Receiving a free token from my (left)
neighbor
2.
Attaching a data packet to the token
3.
Forwarding the token to my (right)
neighbor
4.
Detaching a packet from the token if it
is addressed here
4. Detach
CSS490 Message Passing
3
Network Layer
Example: IP
Transportation layer
Datagram
Class A
fragmentation
SDL
reassembly
SDL
IP packet ID and size
Destination IP address
Source IP address
IP packet ID and size
Destination IP address
Source IP address
0 Net #
Host #
Octet 2 – 4
(1,677,716)
Octet 1
0-127
Class B
10
Octet 1
128-191
Net #
Host #
Octet 2
Octet 3 – 4
(65,536)
Net #
Host#
Class C
110
Data link layer
Best-effort deliver semantics
Octet 1
128-191
Octet 2 – 3
Octet 4
(256)
Class D: for broadcasting
Winter, 2004
CSS490 Message Passing
4
Transport Layer:
Example1: UDP
client
server
socket()
Create a sock descriptor
socket()
bind()
Bind it to an IP address
bind()
recvfrom()
sendto()
Blocks until data received
recvfrom()
Winter, 2004
User Datagram Protocol
Connectionless
May be lost
No FIFO order
Multicast feature
Unix datagram
Example: TFTP, rwho
sendto()
CSS490 Message Passing
5
Transport Layer:
Example2: TCP
client
server
Transport Control Protocol
Connection-oriented
Bind it to an IP address
Reliable
bind()
FIFO order
Declare this is connection-oriented liseten()
No Multicast feature
Unix stream socket
Wait for a connection
accept()
Example: ftp, http, rsh all
Connection established
connect()
Blocks until connection established major applications
socket()
Create a sock descriptor
socket()
write()
read()
read()
wrte()
Winter, 2004
CSS490 Message Passing
6
Application Layer
Example: RSH
Client
shell
Server
TCP connection
request
Command
rsh ls- l
inetd
rshd
TCP connection
Inherited all the way
To a child
shell
Command
ls -l
Winter, 2004
CSS490 Message Passing
7
Socket Programming: Socket.h
#include <iostream>
extern "C"
{
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
}
// for sockets
// for gethostbyname( )
// for close( )
// 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;
};
Winter, 2004
CSS490 Message Passing
8
Socket Programming:
Socket.cpp (Client)
#include "Socket.h"
Socket::Socket( int port )
: port( port ), clientFd( NULL_FD ),
serverFd( NULL_FD ) {
}
Socket::~Socket( ) {
if ( clientFd != NULL_FD )
close( clientFd );
if ( serverFd != NULL_FD )
close( serverFd );
}
int Socket::getClientSocket( char ipName[] ) {
// 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->h_addr_list ) );
sendSockAddr.sin_port
= htons( port );
// Open a TCP socket (an Internet strem socket).
if( ( clientFd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {
cerr << "Cannot open a client TCP socket." << endl;
return NULL_FD;
}
// Connect to the server.
while ( connect( clientFd, (sockaddr*)&sendSockAddr,
sizeof( sendSockAddr ) ) < 0 );
// Get the host entry corresponding to ipName
struct hostent* host = gethostbyname( ipName );
// Connected
if( host == NULL ) {
return clientFd;
cerr << "Cannot find hostname." << endl;
}
return NULL_FD;
}
Winter, 2004
CSS490 Message Passing
9
Socket Programming:
Socket.cpp (Server)
int Socket::getServerSocket( ) {
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 ) ) < 0 ) {
cerr << "Cannot open a server TCP socket." << 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_addr.s_addr = htonl( INADDR_ANY );
acceptSockAddr.sin_port
= htons( port );
if( bind( serverFd, (sockaddr*)&acceptSockAddr,
sizeof( acceptSockAddr ) ) < 0 ) {
cerr << "Cannot bind the local address to the server socket." << 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*)&newSockAddr, &newSockAddrSize ) ) < 0 ) {
cerr << "Cannot accept from another host." << endl;
return NULL_FD;
}
return newFd;
Winter, 2004
CSS490 Message Passing
10
Socket 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 recvMessage[MAXSIZE];
read( fd, recvMessage, MAXSIZE );
cout << recvMessage << endl;
close( fd );
}
}
if ( argc == 2 ) { // I'm a client
if ( ( fd = sock.getClientSocket( argv[1] ) ) == NULL_FD )
return -1;
char sendMessage[MAXSIZE];
cin >> sendMessage;
write( fd, sendMessage, MAXSIZE );
}
return 0;
}
Winter, 2004
CSS490 Message Passing
11
Blocking/Non-Blocking
Communication
Blocking communication
TCP, UDP, and other communication packages
Rendezvous
Client: blocked only when the destination buffer is full
Server: blocked if no message has arrived from the client.
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.
Winter, 2004
CSS490 Message Passing
12
Synchronization in NonBlocking 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 }
Winter, 2004
CSS490 Message Passing
13
Buffering
message
message
message
message
message
message
Winter, 2004
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( )
CSS490 Message Passing
14
Process 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
Winter, 2004
CSS490 Message Passing
15
Failure Handling
client
server
request
ack
server
client
request
timeout
request 2
response
response
response 2
request
request
ack
response
Winter, 2004
ack
timeout
request 2
timeout
Loss of request
message
Loss of response
message
Unsuccessful
execution of request
Do we really need
acknowledgment
messages?
CSS490 Message Passing
16
Idempotency
server
client
request
server
request
client
Timeout
request 2
response
Timeout
request 3
A pair of request and
response is enough to
handle faults
Idempotency assumed:
Timeout
At-least one semantics
Last-one semantics
response
request 4
reesponse 2
Winter, 2004
CSS490 Message Passing
17
Exactly-One Semantics
Withdraw $100
Withdraw995 $100
$1000-$100
= $900
Not received
Withdraw $100
$1000-$100
= $900 for Trans995
Not received
Withdraw995 $100
Trans995 completed
No subtraction
$900-$100
=$800!
$100 received
Winter, 2004
$100 received
CSS490 Message Passing
What if errors in
the banking
system
New semantics
required:
Exactly-one
semantics
Server must
keep track of
the request
sequence
18
Exercises (No turn-in)
1.
2.
3.
4.
5.
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?
1.
cin >> data;
2.
ifstream infile(“input.txt”); infile.seek( );
3.
cout << data;
4.
int a = 1, b = 2, c; c = a + b;
5.
int c = 1; c++;
Winter, 2004
CSS490 Message Passing
19