Module 4: Processes - College of the Holy Cross
Download
Report
Transcript Module 4: Processes - College of the Holy Cross
Operating Systems
Lecture 12
Communicating over a Network
Read Ch 4.6
Operating System Concepts
4.1
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Recall IPC
Interprocess communication (IPC) accomplished by
message passing.
Direct Communication: Processes identify each
other by name.
Indirect communication: Processes use a mailbox or
a port.
Operating System Concepts
4.2
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Communication over a network
Communication between processes over a network is
accomplished with sockets.
A socket is defined as an endpoint for communication.
Communication consists between a pair of sockets.
A socket is the concatenation of IP address and port
number.
The socket 161.25.19.8:1625 refers to port 1625 on
host 161.25.19.8
Operating System Concepts
4.3
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Client-Server Architecture
The server "listens" to a specified port for incoming
client requests.
When a request is received, the server accepts the
connection from the client socket.
"Well known" port numbers:
telnet: 23
ftp: 21
http: 80
All ports numbers below 1024 are considered well
known. They are used for standard services.
Operating System Concepts
4.4
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Example
Suppose there is a client process on host machine, X, with IP
address, 146.86.5.20
The client contacts a web server at IP address, 161.25.19.8.
Question: What is the port number for the server?
Host, X, assigns the client a port, e.g. 1625.
Result: Connection between 2 sockets:
Client on host X
Web Server:
146.86.5.20:1625
Q: What is this socket?
Packets of information travel between the hosts and are
delivered to the appropriate process by way of the port.
Operating System Concepts
4.5
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Socket Communication
Operating System Concepts
4.6
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Implementation in Java
Java provides a Socket class for establishing
communication. (This makes is easier to implement
than in C/C++)
Example: Implementation of a time-of-day server
Clients request the time of day from the server
The server listens to port 5155 (could choose
any port number above 1024).
Operating System Concepts
4.7
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Server Side Application
public class Server {
public static void main( String [ ] args) {
Socket client = null;
//will hold socket for client
PrintWriter pout = null;
//allows easy communication
ServerSocket sock = null; //Holds server socket
...
sock = new ServerSocket(5155); //Port for server socket
while (true) {
//Listen for requests
client = sock.accept( );
//Waits for client to contact (blocked)
//accept( ) returns socket from client
//PrintWriter allows easy communication through socket
pout = new PrintWriter(client.getOutputStream( ), true);
pout.println(new java.util.Date().toString()); //write time of day to
//client
pout.close();
client.close();
...
}
}
Operating System Concepts
4.8
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Client Side Application
public class Client {
public static void main(String[ ] args) {
InputStream in = null;
//for input stream from socket
BufferedReader bin = null;
//for reading in from socket
Socket sock = null;
//socket for contact with server
...
sock = new Socket("127.0.0.1", 5155); //server socket
in = sock.getInputStream( );
//Set up for reading
bin = new BufferedReader(new inputStreamReader(in));
String line;
while ((line = bin.readln( )) != null)
System.out.println(line);
...
// Holds input line
//read to end
//print out line
}
}
//(Note: IP address 127.0.0.1 indicates the local host)
Operating System Concepts
4.9
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Remote Procedure Calls
Remote procedure call (RPC) abstracts procedure calls
between processes on networked systems.
Messages are well structured (not just data packets).
Addressed to RPC daemon listening to a port on the
remote system.
Contain identifier of function to be executed.
Contain parameters to pass to that function.
The port is identified by a number at the start of the message
packet.
A system has one network address, but may have many
ports.
To request a specific service, must address message to the
proper port.
Operating System Concepts
4.10
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Invoking an RPC
Invoking a procedure on a remote host is the same as invoking one
locally (The details are hidden).
Stubs – client-side proxy for the actual procedure on the server.
Procedure for executing a remote procedure call:
The client invokes the remote procedure
The RPC system calls the stub, passing it the parameters provided
in the procedure call.
The client-side stub locates the port on the server.
The stub marshalls the parameters. (Packages them into a form that
can be sent over the network).
The stub transmits a message to the server using message passing.
The server-side stub receives this message, unpacks the marshalled
parameters, and performs the procedure on the server.
If necessary, return values are passed back to the client.
Operating System Concepts
4.11
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Which Port to Use?
With ordinary procedure calls there is a binding between
the name of the procedure and the address of the
procedure.
With an RPC, there is binding between the client and
server ports.
How does the client know which port to use?
Predetermined binding: RPC has fixed port address.
Dynamic binding: O.S. provides a matchmaker daemon
(rendezvous daemon) on a fixed RPC port.
The client sends a message to the matchmaker and
requests the port address of the RPC.
The matchmaker returns the port number.
The RPC call is sent to that port.
Operating System Concepts
4.12
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Execution of RPC
Operating System Concepts
4.13
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Remote Method Invocation
Remote Method Invocation (RMI) is a Java mechanism
similar to RPCs.
RMI allows a Java program on one machine to invoke a
method on a remote object.
RMI works between Java Virtual Machines. Could be two
JVM's on the same machine or on different machines.
Operating System Concepts
4.14
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
RPC vs. RMI
Difference between an RPC and an RMI:
RPC
RMI
Procedural:
Only allows procedures
or functions to be called.
Object Based:
Can invoke a method on
a remote object.
Parameters: Ordinary
data structures
Parameters: Can pass
objects as parameters.
Allows for distributed
Java applications
Operating System Concepts
4.15
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Process for RMI
1. A stub on the client machine is invoked.
2. The stub sends a parcel with the name of the method
and the marshalled parameters.
3. A skeleton on the server unmarshall's the parameters
and invokes the method.
4. The skeleton marshall's the return value into a parcel
and returns it to the client.
5. The stub on the client unmarshall's the return value
and passes it to the client.
Operating System Concepts
4.16
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005
Marshalling Parameters
Example: Client invokes someMethod(A, B) on a Server object.
The method returns a boolean object.
Operating System Concepts
4.17
Silberschatz, Galvin and Gagne 2002
Modified for CSCI 399, Royden, 2005