Title: ECT 7120: Distributed and Mobile Systems
1ECT 7120 Distributed and Mobile Systems
2Project One Introduction
- Write network programs using UNIX sockets and Sun
RPCs
3Project One Introduction (contd)
- Implement RPCs using UNIX sockets
- Exercise 1 UDP echo server and client
- Exercise 2 Coordinate system service using UDP
- Exercise 3 Coordinate system service using UDP,
with a timeout on client side - Exercise 4 TCP echo server and client
- Exercise 5 Coordinate system service using TCP
- Implement RPCs using Sun RPC package
- Exercise 6 Sun RPC echo server and client
- Exercise 7 Sun RPC Coordinate system service
4Procedures to Implement
- Write 3 procedures, incorporated into a client
and a server, for each exercise
5Exercise One
- UDP Client
- repeatedly requests a string from user
- uses DoOperation() to send the string to server
- prints reply on screen when it arrives
- UDP Server
- repeatedly receives a string using GetRequest()
- prints the received string on screen
- replies with the received string using SendReply()
6Exercise One
Prompt for a string from user
- GetRequest()
- get a string from client (on a UDP port)
- DoOperation()
- send the string to server (using UDP)
- wait for reply
Print string on screen
Blocked
- SendReply()
- echoes string back (using UDP)
CLIENT
SERVER
7Termination Conditions
- Case 1 user input string is a single q
- The client should terminate immediately
- Case 2 user input string is a single s
- The server should terminate after it echoes to
client - The client terminates after it received echo from
server
8Exercise Two
- UDP Client
- repeatedly request a string from user
- verify string as either one of the following
- a single character q (for quit client)
- a shortest path operation
- ltoperatorgtltAgtltBxgtltBygt
- ltoperatorgt is either (a, b, c, or d)
- ltAgtltBxgtltBygt are integers
- a single character s (for stop server and
quit client) - a single character p (for ping)
- use DoOperation() to send the string to server
- marshal RPC message to Message
- unmarshal result and print on screen
9Exercise Two
- UDP Server
- repeatedly receives a string using GetRequest()
- unmarshal received string
- process request
- dispatch request to different procedures
accordingly - stop() return success to client, then exit
- ping() return success to client and continue
- shortest path operation operate the request,
return result to client, and continue - reply to client using SendReply()
- marshal RPC message into Message
10Cellular Phone Network Coverage Problem
11Coordinate system service
Coordinate System A
- Conversion on cell A from coordinate system A to
system B. - e.g. a 10 0 3 Answer is 1 2
- e.g. a 19 1 2 Answer is 2 0
- Conversion on cell B from coordinate system B to
system A. - e.g. b 10 0 3 Answer is 22
- e.g. b 19 1 2 Answer is 30
- Levels of cell A and cell B.
- e.g. c 10 0 3 Answer is 2 3
- e.g. c 19 1 2 Answer is 2 3
- The shortest distance between cell A and cell B.
- e.g. d 10 0 3 Answer is 2
- e.g. d 19 1 2 Answer is 5
Coordinate System B
12Coordinate conversion
y
x
13Shortest Path Problem
C. Levels of A and B e.g. c 19 1 2 Answer
is 2 3
- D. The shortest distance between X and Y
- e.g. d 19 1 2
- Answer is 5
14RPC messages
- Message format
- lttypegtltidgtltrequest stringgt
- lttypegt is either Request or Reply
- client and server should check that requests and
replies are not confused - a client generates a new ltidgt for each call
server copies this to the reply - client should check that reply corresponds to its
request - message is in network order (htonl() or
ntohl()are used)
15Exercise Two
- Prompt for a string from user
- verify string as shortest path operation
- GetRequest()
- get the message from client
- DoOperation()
- marshal to Message
- send to server
Dispatch and process request accordingly
Blocked
- SendReply()
- Reply the answer back (using UDP)
- unmarshal and verify reply
- print answer on screen
CLIENT
SERVER
16Exercise Three
- Same as Exercise Two, except a timeout is added
to the client when it is blocked during
DoOperation() - If the client is blocked for a timeout up to 5
seconds, it resends the request up to 4 times
17Exercise Three
18Exercise Four
- Same as Exercise One, except that TCP is used in
sending the requests and replies - A TCP connection must be established before a
client can send a request to the server - A new server process is created for each client
request, such that different clients can be
served concurrently
19Exercise Four
20Termination Conditions
- The client should terminate
- after it receives echo from server when the user
input string is a single s - The spawned server process should terminate
- after it echoes to client when the user input
string is a single s - NOTE the listening server process is never
terminated
21Exercise Five
- Same as Exercise Two, except that TCP is used
instead of UDP - A timeout function already exists for TCP
communications
22Exercise Five
Listen on a TCP port fork a new server process
for each client connection
- Prompt for a string from user
- verify string as single arithmetic operation
- GetRequest()
- get the message from client
- DoOperation()
- make TCP connection
- marshal to Message
- send to server
Dispatch and process request accordingly
- SendReply()
- marshal and send reply to client
- unmarshal and verify reply
- print answer on screen
CLIENT
SERVER
23Exercise Six
- Implement the echo client/server (TCP) in
Exercise Four using Sun RPC package - Write the server interface using RPC language
- Generate client and server stubs from the
interface (using rpcgen) - Write the client and server programs, using the
interface and the stubs - Compile the programs
- You do not need to implement the stop server
function
24Sun RPC
- Hides the details of sockets from the application
programmers point of view - dynamic binding of server ports
- supports both TCP and UDP
- which protocol to use can be determined by user
at compile time
25Overview of Sun RPC
Client process
server process
Client routines
server routines
Client stub
server stub
RPC runtime
RPC runtime
process
kernel
Network routines
Network routines
26Exercise Seven
- Implement the shortest path operation service
(TCP) in Exercise Five using Sun RPC package - You do not need to implement the stop server
function
27Processing Creation
- The server should be able to serve two or more
clients concurrently - You may need to create new server processes to
serve clients - This can be done by the system call fork()
- include ltsys/types.hgt
- include ltunistd.hgt
- pid_t fork(void)
28Process Creation
- fork() creates a new process identical to the
calling process, but returns a different value to
the system call itself - to the created (child) process return 0
- to the calling (parent) process return the
process id of the created process - return lt 0 if there is error
29Process Creation
. . . p fork() . . .
. . . p fork() . . .
p1234
p0
Calling/parent process process ID 1233
Created/child process process ID 0
30Process Creation
- Example program fork.c
- Child termination while parent keeps running
causes child process to become zombie process
which merely takes up system resources - You can use signal() or wait() to avoid child
processes to become zombies - Examples fork2.c, fork3.c
31Process Creation
- include ltsignal.hgt
- void (signal (int sig,
- void (disp)(int)))(int)
- Changes the signal handler for a particular
signal (e.g., SIGCHLD, SIGALRM) - include ltsys/types.hgt
- include ltsys/wait.hgt
- pid_t wait(int stat_loc)
- Waits for a child process to terminate
32Timeouts
- Two ways to do timeouts
- select() refer to UDPsock.c or TCPsock.c
- changing the signal handler of SIGALRM using
signal() and setting up a timed alarm signal,
using alarm() - Example program sigalrm.c, sigalrm2.c
- - END -