1CSIE,NTUT,TAIWAN
Client Server Model
Chuan-Ming Liu
Computer Science and Information Engineering
Spring 2004,NTUT
TAIWAN
CSIE,NTUT,TAIWAN 2
Client-Server Model
Software Design
Concurrent Processing
CSIE,NTUT,TAIWAN 3
Motivation
Scenario:
A user tries to start two programs on separate
machines and have them communicate.
Program 1 starts
Send message to its peer
Not running; RefuseNo response; exit
Program 2 starts
No connection can be set up
CSIE,NTUT,TAIWAN 4
Client-Server Model
One side in any pair of communicating
application must start execution and wait for
the other side to contact it.
Since the client-server model places
responsibility for rendezvous problem on
application,TCP/IP does not need to provide
mechanisms that automatically create a
running program when a message arrives.
Instead,a program must be waiting to accept
communication before any request arrive.
CSIE,NTUT,TAIWAN 5
Terminology and Concepts
Clients and Servers
Privilege and Complexity
Standard v.s,Nonstandard Client Software
Parameterization of Clients
Connectionless v.s,Connection-oriented Servers
Stateless v.s,Stateful Servers
Identifying a Client
CSIE,NTUT,TAIWAN 6
Clients and Servers
Client
* An application that initiates peer-to-peer
communication,e.g,web browser
* Easier to build than servers
* System privileges usually unnecessary
Server
Program that waits for incoming
communication requests from a client
CSIE,NTUT,TAIWAN 7
Privilege and Complexity
Server software often needs to access to objects
that OS protects
Server can not rely on the usual OS check
since its privilege status allow to access any file
Security issues:
– Authentication
– Authorization
– Data Security
– Privacy
– Protection
CSIE,NTUT,TAIWAN 8
Privilege and Complexity
The combination of special privileges and
concurrent operation usually makes servers more
difficult to design and implementation
CSIE,NTUT,TAIWAN 9
Standard v.s,Nonstandard Client
Software
Standard application services
– Defined by TCP/IP
– Assigned well-known,universally recognized
protocol port
Non-standard application services
– All other services which is not standard
– Or,locally-defined application services
CSIE,NTUT,TAIWAN 10
Standard v.s,Nonstandard Client
Software
Standard application service examples
– Remote login,TELNET protocol
– E-mail client,SMTP or POP protocol
– File transfer client,FTP protocol
– Web browser,HTTP protocol
Non-standard application service examples
– Music or video transfer
– Voice communication
– Distributed database access
CSIE,NTUT,TAIWAN 11
Parameterization of clients
Generality for client software
e.g,TELNET protocol can use a port number
as an input parameter
Fully parameterized client
application allows more input parameters
When designing client application,include
parameters that allow the user to fully specify
the destination machine and port number.
CSIE,NTUT,TAIWAN 12
Connectionless v.s,Connection-
Oriented Servers
TCP/IP provides two types of interaction
between client and server,
– Connectionless style
– Connection-oriented style
The distinction is critical TCP
UDP
CSIE,NTUT,TAIWAN 13
Connectionless v.s,Connection-
Oriented Servers
TCP:
* full reliability
* verifying the data arrives
* automatically retransmits segments
* checksum over the data
* data arriving in order
* no duplicated packets
* control the flow
* reporting the underlying network problem to sender
CSIE,NTUT,TAIWAN 14
Connectionless v.s,Connection-
Oriented Servers
UDP
* no guarantees about reliable delivery
* software contains code to detect and correct
errors occurred by transmission
* work well when the underlying network
running well
* aware of testing when using UDP
CSIE,NTUT,TAIWAN 15
Connectionless v.s,Connection-
Oriented Servers
TCP is preferable to UDP
– TCP simplify programming
– TCP relieves the programmers of responsibility for
detecting and correcting errors
– Adding reliability to UDP is a nontrivial work
CSIE,NTUT,TAIWAN 16
Connectionless v.s,Connection-
Oriented Servers
Application programs use UDP only if
– Application protocol specifies the UDP must be
used
– Application protocol relies on hardware broadcast
or multicast for delivery
– Overhead for reliability is unnecessary
CSIE,NTUT,TAIWAN 17
Stateless v.s,Stateful Servers
State information
A server maintains about the status of ongoing
interaction with clients
Servers that do not keep any state information
are called stateless servers; otherwise,called
stateful servers.
CSIE,NTUT,TAIWAN 18
Stateless v.s,Stateful Servers
Keeping a small amount of information in a
server can reduce the size of messages that the
client and server exchange and allow the server
to response quickly
The point for statefulness is efficiency
The motivation for statelessness lies in protocol
reliability
CSIE,NTUT,TAIWAN 19
Stateless File Server Example
disk
File server
client
Waits for client
to access
stores or extracts data
from the server
message
Item Description
op
name
pos
size
data
Operation (read or write)
Name of the file
Position in the file
Number of bytes to transfer
Present only in write operation
CSIE,NTUT,TAIWAN 20
Stateful File Server Example
disk
File server
client
Waits for client
to access
stores or extracts data
from the server
reads
Item Description
op
name
pos
size
data
Operation (read or write)
Name of the file
Position in the file
Number of bytes to transfer
Present only in write operation
CSIE,NTUT,TAIWAN 21
Stateful File Server Example
Client File Name Current
Position
Last
Operation
1
2
3
4
test.program.c
tcp.book.text
dept.budget.txt
teris.txt
0
456
38
125
read
read
write
read
State information table
CSIE,NTUT,TAIWAN 22
Identifying a Client
Stateful servers use two general approaches to
identify clients
– Endpoints
– Handles
Endpoint identification
– Operate automatically
– Relies on transport protocol,not application protocol
– Endpoint information may change
CSIE,NTUT,TAIWAN 23
Identifying a Client
server
Transport protocol
IP address and
port number
using the endpoint
information to look
up the state table
CSIE,NTUT,TAIWAN 24
Identifying a Client
Handles
– remains constant across multiple transport
connections
– A small integer
– Independent of the underlying transport protocol
Change of transport connection does not invalidate
handles
Visibility to the application – drawback
CSIE,NTUT,TAIWAN 25
Identifying a Client
Back to stateful server
– Can not retain state forever
– Application protocol requires termination
– Using endpoint identification can be confused by a crash
The point of state is efficient
– Reducing the amount of data transferred
– Intuitive design
– Difficulty
Maintain the correctness when delay,duplication
allowed
State information may be incorrect when computer
restarts
CSIE,NTUT,TAIWAN 26
Identifying a Client
In general,in a real internet,where machines
crash and reboot,and messages can be lost,
delayed,duplicated,or delivered out of order,
stateful designs lead to complex application
protocols that are difficult to design,
understand,and implement correctly.
CSIE,NTUT,TAIWAN 27
Statelessness is a Protocol Issues
Statelessness or not centers on the application
protocol more than implementation
The issue of statelessness focuses on whether
the application protocol assumes the
responsibility for reliable delivery
Idempotent – design issue for statelessness
operation always has the same result
CSIE,NTUT,TAIWAN 28
Servers as Clients
Programs do not fit exactly into the definition
of client or server
Avoid circular dependencies among servers
Internetclient
time server
file server
CSIE,NTUT,TAIWAN 29
Concurrent Processing
Concurrency in Networks
Concurrency in Servers
Terminology and Concepts
Example for Concurrent Process Creation
Executing New Code
Context Switching and Protocol Software
Design
Concurrency and Asynchronous I/O
CSIE,NTUT,TAIWAN 30
Concurrency in Network
Concurrency
– provides much of the power behind client-server
interaction
– makes the software difficult to design and build
CSIE,NTUT,TAIWAN 31
Concurrency in Network
Concurrency = simultaneous computing
– Time sharing scheme
– Multiprocessing
Concurrent processing is fundamental to
distributed computing and occurs in
– Network
– A computer system
virtually or practically
CSIE,NTUT,TAIWAN 32
Concurrency in Network
In network
D
C
B
A
CSIE,NTUT,TAIWAN 33
Concurrency in Network
In a computer system
A time sharing system
User 1 User 2 User 3
ftp rlogin http
CSIE,NTUT,TAIWAN 34
Concurrency in Network
Besides,the set of all clients on a set of
machines can execute concurrently
Internet
c5c4
c1
c3 c2
CSIE,NTUT,TAIWAN 35
Concurrency in Network
Client software does not require any special
attention or effort on the part of the
programmer to make it usable concurrently
i.e,each individual client operates much like
any conventional program
CSIE,NTUT,TAIWAN 36
Concurrency in Servers
In contrast to a client,concurrency within a
server require considerable effort
Internet
c5c4
c1
c3 server
CSIE,NTUT,TAIWAN 37
Terminology and Concepts
Process (task,or job)
Local and global variables
Procedure calls
CSIE,NTUT,TAIWAN 38
Process
Fundamental unit of computation
An address space + at least one thread of
execution
Information associated with a process
– instruction pointer (associated with a thread)
– identity of the user
– compiled program
– memory locations for the program text and data
CSIE,NTUT,TAIWAN 39
Process
The process concept includes only the active
execution of a computation,not the static
version of the program
In a uniprocessor architecture,the single CPU
can only execute one thread at any time
instance
– To achieve concurrency by switch CPU among all
threads rapidly
Concurrent execution
CSIE,NTUT,TAIWAN 40
Process
On a multiprocessor machine,all CPUs can
execute simultaneously
Keep in mind,the key issue is
transparency
CSIE,NTUT,TAIWAN 41
Sharing of Local and Global Variables
When multiple threads execute a piece of code
concurrently,each thread has its own
independent copy of the variables associated
with the code
Each process receives a separate copy of
global variables; if multiple threads execute
within a single process,they each have a copy
of local variables,but all share the process
copy of the global variables
CSIE,NTUT,TAIWAN 42
Procedure calls
In a procedure-oriented language,like C,
executed code can contain calls to
subprograms (procedures or functions)
Using a stack to handle procedure call
When multiple threads execute a piece of code
concurrently,each has its own run-time stack
of procedure activation records
CSIE,NTUT,TAIWAN 43
Example on Concurrency Process
Run sum.c on-line
System function fork
– Divides the running program into two identical
processes
– Starts a thread executing the new process in the
same place of the code
Inserting fork into sum.c and run sum_fork.c
CSIE,NTUT,TAIWAN 44
Example
CSIE,NTUT,TAIWAN 47
Concurrency Process
In a uniprocess computer,OS allocates the
available CPU power for a short time to each
threads in round-robin fashion
Timesliceing mechanism
Allocate the available processing equally among all
available threads
CSIE,NTUT,TAIWAN 48
Single-Threaded Process Assumption
fork()
– not duplicate all of the threads
– create a copy of a running process and the new
process contains exactly one thread
The newly created process by fork() is said to be
a singly-threaded process
Although a process can contain multiple threads,
the newly created process that results from a
call to fork is singly-threaded.
CSIE,NTUT,TAIWAN 49
Processes Diverge
New process generated by fork
– Not absolutely identical to the original process
– fork() return a value to the original process and the
new process
0 to the new process
A positive integer to the original process to identify the
newly created process
– The value returned to the original is called a
process identifier or process id (pid)
CSIE,NTUT,TAIWAN 50
Processes Diverge
#include <stdlib.h>
#include <stdio.h>
main(){
int pid; /* child process id */
pid = fork();
if (pid !=0){ /* parent process */
printf("The parent process prints this.\n");
} else {
printf("The child process prints this.\n");
}
exit(0); /* terminate the program */
}
CSIE,NTUT,TAIWAN 51
CSIE,NTUT,TAIWAN 52
Executing New Code
UNIX provide s a mechanism that allows any process
to execute an independent,separate-compiled
program
The mechanism consists of a system call,execve,that
replace the code that the currently executing process
runs with the code from the new program
Way to new process that executes the object code
from a file,a process call fork and execve
execve is very important for server to handle diverse
services
CSIE,NTUT,TAIWAN 53
Context Switching
When the OS switches one thread to another,a
context switch occurs
Context switching between threads in a
process requires less overhead than switching
between a thread in one process and a thread in
another
Context switching needs CPU; therefore,is
counted as an overhead to support concurrent
processing
CSIE,NTUT,TAIWAN 54
Context Switching
To avoid unnecessary overhead,protocol
software should be designed to minimize the
context switching
CSIE,NTUT,TAIWAN 55
Concurrency and Asynchronous I/O
OS allows a single application to initiate and
control concurrent I/O
System call select,is such an operation which
is used to find out which I/O device becomes
ready first.