CSIE,NTUT,Taiwan1
Server Software Design
Examples
Chuan-Ming Liu
Computer Science and Information Engineering
Spring 2004,NTUT
TAIWAN
CSIE,NTUT,Taiwan2
Examples
Iterative,Connectionless Server (UDP)
Iterative,Connected-Oriented Server
(TCP)
Concurrent,Connection-Oriented
Server (TCP)
Iterative,Connectionless
Server (UDP)
CSIE,NTUT,Taiwan4
Creating Passive Socket
To hide the allocation of socket,use
passiveUDP (passiveUDP.c)
passiveTCP (passiveTCP.c)
two high-level procedures
Both procedures call passivesock() to
perform socket allocation –
passivesock.c
CSIE,NTUT,Taiwan5
Port Privilege
To use well-known services (lower port
number),the server process needs
special privilege
Two servers on a given computer can
not use the same port number at the
same time
How to overcome the port privilege?
Using portbase
CSIE,NTUT,Taiwan6
Portbase
Programmer does not need to modify
all references to port numbers – safety
Solves the problem in general way –
generality
Solving the possible port conflicts – no
conflicts
CSIE,NTUT,Taiwan7
Process Structure
server
OS
AP
Socket at well-known port
used for all communication
CSIE,NTUT,Taiwan8
Example
The UDPtimed.cexample
Iterative,Connection-
Oriented Server (UDP)
CSIE,NTUT,Taiwan10
Creating Passive Socket
Use passiveTCP()to allocate a
stream socket and bind it to the well-
known port
Two arguments in passiveTCP:
Name of service
Size of the queue for requests
CSIE,NTUT,Taiwan11
Why Iterative?
Each request for the time is simple and
easy to process
Server does not need to optimize for
speed
CSIE,NTUT,Taiwan12
Process Structure
server
OS
AP
Socket for
connection
requests
Socket for
an individual
connection
CSIE,NTUT,Taiwan13
Example
The TCPdaytimed.cexample
The call to accept blocks the server thread until
a connection arrives
When a connection request arrives
TCP protocol engages 3-way handshake to
establish a connection
After completing the handshake,the system
allocates a new socket and the call to accept
returns
The server continues to execute
CSIE,NTUT,Taiwan14
Connection Termination
Daytime server can close a connection
Usually,the termination is decided by
clients
Allowing a client to control connection
duration can be dangerous because it
allows clients to control resource use
Avoid vulnerability when designing
servers
Concurrent,Connection-
Oriented Servers (TCP)
CSIE,NTUT,Taiwan16
Multiple Single-threaded
Processes
Recall Algorithm 8.4 (Comer)
OS’s support for concurrent processing
Master process for connection requests
Slave processes for handling
communication with clients
Each process contains a single thread
CSIE,NTUT,Taiwan17
Echo Service
Echo server responds to each client
Alternates between reading and writing
Closes the connection when it
encounters an end-of-file condition
Iterative or concurrent?
CSIE,NTUT,Taiwan18
Process Structure
Master
OS
Application
Processes
(or threads)
Socket for
connection
requests
Sockets for
individual
connections
slave1 slave2 slaven
CSIE,NTUT,Taiwan19
Example – TCPechod.c
The call to accept blocks the server thread
until a connection arrives
Note the call to fork(),after the fork()
Master process needs to close the child socket
The new child process needs to close the master
socket
Sockets only disappear in the processes not the
system
Before system deallocates a socket,all the
processes own the socket need to close it
CSIE,NTUT,Taiwan20
Exit call
Linux interprets the exitcall as a
request to terminate the process and
uses the argument as a process exit
code
Conventionally,a process uses exit
code zero to denote normal termination
CSIE,NTUT,Taiwan21
Errant Processes
Incompletely terminated processes due
to the dynamic allocation of processes
Linux solves this by sending a signal
to the parent whenever a child process
exits
CSIE,NTUT,Taiwan22
Errant Processes
Existing process remains in a zombie
state until the parent executes a wait3
system call
Server needs to catch the child
termination signal and executes a
signal handling function
CSIE,NTUT,Taiwan23
Signal
signal( SIGCHLD,reaper)informs
the OS
The master process should execute reaper
function whenever it receives a signal that a child
process has exited
reaperfunction calls wait3() system call
To complete terminate a child that exits
Wait3 blocks until one or more children exit
WNOHANG argument specifies that wait3
should not block waiting for a process to exit