VivekHotti DC Exp1 PDF
VivekHotti DC Exp1 PDF
VivekHotti DC Exp1 PDF
Hotti
BE-Comps-A
Batch : A2
Roll : 37
EXPERIMENT 01
AIM : A Program To Demonstrate Interprocess Communication in Client
Server Environment using Java.
THEORY:
In computer science, inter-process communication or interprocess communication (IPC) refers
specifically to the mechanisms an operating system provides to allow the processes to manage
shared data. Typically, applications can use IPC, categorized as clients and servers, where the
client requests data and the server responds to client requests.Many applications are both clients
and servers, as commonly seen in distributed computing.
IPC is very important to the design process for microkernels and nano kernels, which reduce the
number of functionalities provided by the kernel. Those functionalities are then obtained by
communicating with servers via IPC, leading to a large increase in communication when
compared to a regular monolithic kernel. IPC interfaces generally encompass variable analytic
framework structures. These processes ensure compatibility between the multi-vector protocols
upon which IPC models rely.
An IPC mechanism is either synchronous or asynchronous. Synchronization primitives may be
used to have synchronous behavior with an asynchronous IPC mechanism.
It acts as a type of endpoint for receiving or sending the data in a network. It is correct for data
sent between processes on the same computer or data sent between different computers on the
same network. Hence, it is used by several types of operating systems.
An operating system can implement both methods of communication. First, we will discuss the
shared memory methods of communication and then message passing. Communication between
processes using shared memory requires processes to share some variable, and it completely
depends on how the programmer will implement it. One way of communication using shared
memory can be imagined like this: Suppose process1 and process2 are executing simultaneously,
and they share some resources or use some information from another process. Process1 generates
information about certain computations or resources being used and keeps it as a record in shared
memory. When process2 needs to use the shared information, it will check in the record stored in
shared memory and take note of the information generated by process1 and act accordingly.
Processes can use shared memory for extracting information as a record from another process as
well as for delivering any specific information to other processes.
Inter-process communication (IPC) is used for programs to communicate data to each other and
to synchronize their activities. Semaphores, shared memory, and internal message queues are
common methods of inter-process communication.IPC is a method for two or more separate
programs or processes to communicate with each other. This avoids using real disk-based files
and the associated I/O overhead to pass information. Like a file, you must first create or open the
resource, use it and close it. Like real files, the resources have an owner, a group, and
permissions. Until you remove the resource it continues to exist. Unlike real disk-based files,
semaphores, message queues and shared memory do not persist across reboots.
INPUT:
IPCClient
import java.net.*;
import java.io.*;
public class IPCClient
{
public static void main (String args[])
{
try
{
Socket s = new Socket("localhost",1200);
DataOutputStream dos = new
DataOutputStream(s.getOutputStream());
DataInputStream dis = new
DataInputStream(s.getInputStream());
InputStreamReader isr =new
InputStreamReader(System.in);
System.out.println(" \n \t******* CLIENT PROCESS
STARTED ******* ");
System.out.println(" \n ******* PLEASE ENTER THE
VALUES OF Number 1 AND Number 2 TO PASS THEM TO SERVER PROCESS
******* \n");
BufferedReader br=new BufferedReader(isr);
int a=Integer.parseInt(br.readLine());
System.out.println("Number 1 ------>"+a);
dos.writeInt(+a);
int b=Integer.parseInt(br.readLine());
System.out.println("Number 2 ------>"+b);
dos.writeInt(+b);
int result=dis.readInt();
System.out.println("\n.......CLIENT PROCESS HAS
RECIEVED RESULT FROM SERVER.......\n");
System.out.println("\n THE ADDITION OF " + a +" AND
"+ b +" IS "+result);
s.close();
}
catch (Exception e)
{
System.out.println("Exception is "+e);
}
}
}
IPCServer
import java.net.*;
import java.io.*;
public class IPCServer
{
public static void main (String args[])
{
System.out.println("\n **** INTERPROCESS COMMUNICATION
****\n");
System.out.println("\n *** SERVER PROCESS STARTED ***\n");
System.out.println("\n * SERVER IS READY AND WAITING TO
RECIEVE DATA FROM CLIENT PROCESS ON PORT " +1200);
try
{
ServerSocket ss = new ServerSocket(1200);
Socket clientSocket = ss.accept();
System.out.println("\n * Client is connected with IP
address " +clientSocket.getInetAddress() + " and port number " +
clientSocket.getPort());
DataOutputStream dos = new
DataOutputStream(clientSocket.getOutputStream());
DataInputStream dis = new
DataInputStream(clientSocket.getInputStream());
int a=dis.readInt();
System.out.println("\n SERVER RECEIVED");
System.out.println("\n Number 1 ------>"+a);
int b=dis.readInt();
System.out.println("\n Number 2 ------>"+b);
int c=a+b;
dos.writeInt(c);
System.out.println("\n SERVER PROCESS HAS EXECUTED
REQUESTED PROCESS AND SENT RESULT "+c+" TO THE CLIENT \n");
clientSocket.close();
System.out.println("\n SERVER PROCESS
EXITING.............");
ss.close();
}
catch (Exception e) {
System.out.println("Exception: "+e);
}
}
}
OUTPUT:
CONCLUSION: