Client-Server Programming Paradigm 1 ' " The Computer - - PDF document

client server programming paradigm
SMART_READER_LITE
LIVE PREVIEW

Client-Server Programming Paradigm 1 ' " The Computer - - PDF document

Client-Server Programming Paradigm 1 ' " The Computer Communication Cource The Client-Server Programming Paradigm most networking applications can be divided into two pieces: most networking applications can be


slide-1
SLIDE 1

1

1ל 'סשת א רדא"ג The Computer Communication Cource

Client-Server Programming Paradigm

2ל 'סשת א רדא"ג The Computer Communication Cource

The Client-Server Programming Paradigm

■ ■

most networking applications can be divided into two pieces: most networking applications can be divided into two pieces: client client and and server server

  • Client is usually short-lived process, communicates with one server

at a time, simpler design

  • Server usually runs forever, communicates with multiple clients at

any given moment, design is complex Server waits for requests from clients and serves them.

  • The server can either handle requests iteratively,
  • r concurrently (by spawning child processes )

Client asks a server to do some work and send back the results.

request response Server Client

slide-2
SLIDE 2

2

3ל 'סשת א רדא"ג The Computer Communication Cource

Client Design Alternatives

■ Stop-and-wait ◆ while(1) {send request; wait for answer} ■ Interactive: may accept on-line input from the user (e.g.,

through GUI):

◆ I/O multiplexing is needed (select()) ◆ 2 separate processes (fork()) or threads:

✦ process/thread 1: accept client input, send to the

server

✦ process/thread 2: receive server reply, present to

the client

4ל 'סשת א רדא"ג The Computer Communication Cource

Server Design Alternatives

■ Iterative server: one client at a time ■ Concurrent server: multiple clients at a time ◆ concurrent server with fork():

✦ 1 child process per client vs. pre-forked server

◆ concurrent server with threads:

✦ 1 thread per client vs. pre-threaded server

◆ concurrent server with select()

slide-3
SLIDE 3

3

5ל 'סשת א רדא"ג The Computer Communication Cource

The UNIX Support

■ UNIX offers a rich collection of design alternatives for

networking application developers (easy to get lost :-)

◆ support for process control

✦ fork(), exec*(), wait*(), etc.

◆ Support for asynchronous event driven programming

✦ sigaction(), sigprocmask(),sigsuspend(), etc

◆ support for I/O

✦ blocked, non-blocked, asynchronous ✦ I/O multiplexing: select()

◆ user/kernel level thread library: POSIX pthreads 6ל 'סשת א רדא"ג The Computer Communication Cource

Concurrency Strategies

■ ■

Thread utilization Thread utilization

◆ ◆ Thread per request/message

Thread per request/message

◆ ◆ Thread per connection

Thread per connection

◆ ◆ Non concurrent

Non concurrent

■ ■

Thread creation Thread creation

◆ ◆ On

On-

  • demand;

demand;

◆ ◆ Thread pool;

Thread pool;

slide-4
SLIDE 4

4

7ל 'סשת א רדא"ג The Computer Communication Cource

Thread On demand

■ ■

When there is a new Task create a new thread. When there is a new Task create a new thread.

■ ■

Pros: Pros:

◆ ◆ Easy to implement and debug,

Easy to implement and debug,

◆ ◆ Less synchronization

Less synchronization

■ ■

Cons: Cons:

◆ ◆ Time / Resources of Creation

Time / Resources of Creation

◆ ◆ Time spent in Synchronization

Time spent in Synchronization

◆ ◆ Time of Destruction

Time of Destruction

8ל 'סשת א רדא"ג The Computer Communication Cource

Thread pool

■ ■

When there is a new Task associate a thread with It When there is a new Task associate a thread with It

■ ■

Pros: Pros:

◆ ◆ Less response time

Less response time

◆ ◆ No Creation/Destruction

No Creation/Destruction

■ ■

Cons: Cons:

◆ ◆ Resource allocation

Resource allocation

◆ ◆ A lot of synchronization

A lot of synchronization

slide-5
SLIDE 5

5

9ל 'סשת א רדא"ג The Computer Communication Cource

Implementation of Thread Pool

■ ■

Reader Thread: Reader Thread:

◆ ◆

t = read(); /* reads tasks */ t = read(); /* reads tasks */

◆ ◆

synchronized ( synchronized (taskQueue taskQueue) { ) {

✦ ✦ taskQueue

taskQueue.add(t); .add(t);

◆ ◆

} }

◆ ◆

pool.notify(); pool.notify();

■ ■

Execution Thread Execution Thread

◆ ◆

run() { run() {

✦ ✦ pool.wait()

pool.wait()

✦ ✦ synchronized (

synchronized (taskQueue taskQueue) { ) {

  • t =

t = taskQueue taskQueue.get(); .get();

✦ ✦ }

} } }

10ל 'סשת א רדא"ג The Computer Communication Cource

http://www-106.ibm.com/developerworks/java/library/j-jtp0730.html

■ ■

public class public class WorkQueue WorkQueue

■ ■

{ {

■ ■

private final private final int nThreads int nThreads; ;

■ ■

private final private final PoolWorker PoolWorker[] threads; [] threads;

■ ■

private final private final LinkedList LinkedList queue; queue;

■ ■

public void execute( public void execute(Runnable Runnable r) { r) {

■ ■

synchronized(queue) { synchronized(queue) {

■ ■

queue. queue.addLast addLast(r); (r);

■ ■

queue.notify(); queue.notify();

■ ■

} }

■ ■

} }

slide-6
SLIDE 6

6

11ל 'סשת א רדא"ג The Computer Communication Cource

PoolWorker

■ ■

p private class PoolWorker extends Thread { rivate class PoolWorker extends Thread {

■ ■

public void run() { public void run() {

■ ■

Runnable Runnable r; r;

■ ■

while (true) { while (true) {

■ ■

synchronized(queue) { synchronized(queue) {

■ ■

while (queue. while (queue.isEmpty isEmpty()) { ()) {

queue.wait(); queue.wait();

■ ■

} }

■ ■

r = ( r = (Runnable Runnable) queue. ) queue.removeFirst removeFirst(); ();

■ ■

} }

■ ■

// If we don't catch // If we don't catch RuntimeException RuntimeException, ,

■ ■

// the pool could leak threads // the pool could leak threads

■ ■

try { r.run(); try { r.run();

■ ■

} catch ( } catch (RuntimeException RuntimeException e) { e) {

■ ■

// You might want to log something here // You might want to log something here

■ ■

} } } } } } } }