Chapter Three: Inter-Thread and Inter-Process Communication
Chapter Three: Inter-Thread and Inter-Process Communication
1
Topic to be covered
Inter-thread communication
Inter-process communication
IPC Approaches/Types
Inter-thread Communication
3
other.
section and another thread is allowed to enter (or lock) in the same
P2 P2 P3 ... P4
m
m m m
P1 P1
uni c as t m ul ti c as t
Publish/Subscribe Message model
P roc es s 1 P roc es s 2
data
A receive operation issued causes the suspension of the issuing process (process
2) until data is received to fulfill the operation.
A send operation issued causes the sending process (process 1) to suspend.
When the data sent has been received by process2, the IPC facility on host 2
sends an acknowledgment to the IPC facility on host I, and process 1 may
subsequently be unblocked.
Depending on the implementation of the IPC facility, the synchronous receive
operation may not be fulfilled until the amount of data the receiver expects to
receive has arrived.
The use of synchronous send and synchronous receive is warranted if the
application logic of both processes requires that the data sent must be received
before further processing can proceed.
Synchronous send and receive
pr o c e s s 1 pro cess 2
r u nn i ng o n ho s t 2
r u n ni ng o n ho s t 1 Event Diagram
b lo c k in g r ec eiv e s ta r ts
b lo c k in g s en d s tar ts an o p er a tio n
ex ec u tio n f lo w
ac k n o w led g em en t o f d a ta r ec eiv e d s u s p en d ed p er io d
b lo c k in g s en d r etu r n s p r o v id ed b y th e I P C f ac ility b lo c k in g r ec eiv e en d s
Syn c h r o no us Se n d an d R e c e i ve
Client Server
Sender Receiver
Asynchronous send and Synchronous receive
P rocess 2
P rocess 1 Event Diagram
b lo c k in g r e c e iv e s ta r ts
n o n b lo c k in g s e n d
o p e r a tio n
e x e c u tio n f lo w
s u s p e n d e d p e r io d
b lo c k in g r e c e iv e r e tu r n s
As yn c h r o n o u s S e n d an d
S yn c h r o n o u s R e c e i ve
Client Server
Sender Receiver
Synchronous send and Asynchronous Receive
P rocess 2
• The data requested by the receive P rocess 1
requested data.
Data from P1 arrived to P2 before P2 issues a non-blocking receive op. P2 is
notified of the arrival of data
Asynchronous send and Asynchronous receive
Without blocking on either side, the only way that the data can be
delivered to the receiver is if the IPC facility retains the data received.
The receiving process can then be notified of the data's arrival.
Alternatively, the receiving process may poll for the arrival of the
data and process it when the awaited data has arrived.
Asynchronous send and Asynchronous receive
P rocess 2
P r o c e ss 1
n o n b lo c k in g r ec eiv e is s u ed
an d retu r n ed im m ed iately
b lo c k in g s en d is s u ed
p r o c es s is n o tif ied
o f th e ar r iv al o f
d ata
ex ec u tio n f lo w
s u s p en d ed p erio d
As ync hr o no us Se nd and
As ync hr o no us R e c e i ve
S ce n a rio C
Signal A system message sent from one process to another, Most operating systems
not usually used to transfer data but instead used to
remotely command the partnered process.
Socket A data stream sent over a network interface, either to Most operating systems
a different process on the same computer or to
another computer on the network. Typically byte-
oriented, sockets rarely preserve message boundaries.
Data written through a socket requires formatting to
preserve message boundaries.
Shared Multiple processes are given access to the same All POSIX systems, Windows
memory block of memory which creates a shared buffer for
the processes to communicate with each other.
What is a socket?
Socket
• The combination of an IP address and a port number. (RFC 793 ,original
TCP specification)
• The name of the Berkeley-derived application programming interfaces
(APIs) for applications using TCP/IP protocols.
• Two types
• Stream socket : reliable two-way connected communication streams
• Datagram socket
Socket pair
Specified the two end points that uniquely identifies each TCP
connection in an internet.
4-tuple: (client IP address, client port number, server IP address, server
port number)
Socket Programming with TCP
socket( )
bind( ) server
socket( ) listen( )
client bind( )
connect( ) TCP conn. request
accept( )
send( ) TCP ACK
recv( )
recv( )
send( )
close( ) close( )
controlled by
application process
process
developer socket
socket
controlled by TCP with
TCP with
operating buffers, internet
system buffers,
variables
variables
Socket programming with TCP
keyboard monitor
inFromUser
input
stream
(inFromUser stream) , sends to Client
server via socket (outToServer Process Input stream:
process sequence of bytes
stream) output stream: into process
server reads line from socket sequence of bytes
out of process
server converts line to uppercase,
inFromServer
outToServer
output input
sends back to client stream stream
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
JAVA TCP Sockets
In Package java.net
java.net.Socket
Implements client sockets (also called just “sockets”).
An endpoint for communication between two machines.
Constructor and Methods
Socket(String host, int port): Creates a stream socket and connects it to the specified port number on
the named host.
InputStream getInputStream()
OutputStream getOutputStream()
close()
java.net.ServerSocket
Implements server sockets.
Waits for requests to come in over the network.
Performs some operation based on the request.
Constructor and Methods
ServerSocket(int port)
Socket Accept(): Listens for a connection to be made to this socket and accepts it. This method blocks
until a connection is made.
Establishing a Simple Server Using Stream
Sockets
Step 1: Create a ServerSocket
ServerSocket server = new ServerSocket( portNumber, queueLength );
Step 2: Wait for a Connection
Socket connection = server.accept();
Step 3: Get the Socket’s I/O Streams
ObjectInputStream input = new
ObjectInputStream( connection.getInputStream() );
ObjectOutputStream output = new
ObjectOutputStream( connection.getOutputStream() );
Step 4: Perform the Processing
in which the server and the client communicate via the OutputStream and
InputStream objects.
Cont…
Step 5: Close the Connection
when the transmission is complete, the server
closes the connection by invoking the close
method on the streams and on the Socket.
Establishing a simple client in Java
Step 1: Create a Socket to Connect to the Server
Socket connection = new Socket( serverAddress, port );
Step 2: Get the Socket’s I/O Streams
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class cli {
public static void main(String args[]) throws UnknownHostException, IOException{
int number,temp;
Scanner sc=new Scanner (System.in);
Socket s=new Socket("127.0.0.1",1342);
Scanner sc1=new Scanner (s.getInputStream());
System.out.println("Enter any number");
number=sc.nextInt();
PrintStream p=new PrintStream(s.getOutputStream());
p.println(number);
temp=sc1.nextInt();
System.out.println(temp);
}}
TCPServer.java
import java.io.IOException;
import java.net.ServerSocket;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class ser {
public static void main(String args[]) throws IOException{
int number,temp;
ServerSocket s1=new ServerSocket(1342);
Socket ss=s1.accept();
Scanner sc=new Scanner(ss.getInputStream());
number=sc.nextInt();
temp=number*2;
PrintStream p=new PrintStream(ss.getOutputStream());
p.println(temp);
}}
Socket Programming with UDP
UDP
Connectionless and unreliable service.
There isn’t an initial handshaking phase.
Doesn’t have a pipe.
transmitted data may be received out of order, or lost
Socket Programming with UDP
No need for a welcoming socket.
No streams are attached to the sockets.
the sending hosts creates “packets” by attaching the IP destination address
and port number to each batch of bytes.
The receiving process must unravel to received packet to obtain the
packet’s information bytes.
Client/server socket interaction: UDP
create socket,
create socket,
port=x, for clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read request from
serverSocket
write reply to
serverSocket
specifying client read reply from
host address, clientSocket
port umber close
clientSocket
Example: Java client (UDP)
keyboard monitor
inFromUser
input
stream
Client
Process
Input: receives
process
packet (TCP
received “byte
Output: sends
stream”)
packet (TCP sent
receivePacket
sendPacket
“byte stream”) UDP UDP
packet packet
client UDP
clientSocket
socket UDP
socket
In Package java.net
java.net.DatagramSocket
A socket for sending and receiving datagram packets.
Constructor and Methods
DatagramSocket(int port): Constructs a datagram socket and binds it to the
specified port on the local host machine.
void receive( DatagramPacket p)
void send( DatagramPacket p)
void close()
UDPClient.java
import java.net.*;
import java.io.*;
import java.net.*;
import java.io.*;
public class updserver {
public static void main(String args[]) throws Exception {
DatagramSocket ds=new DatagramSocket(2000);
byte [] b=new byte[100];
}
Pipes