client server programming paradigm
play

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


  1. 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 divided into two pieces: ■ ■ client and client and server server request Server Client response Server waits for requests from clients and serves them. • The server can either handle requests iteratively, or concurrently (by spawning child processes ) Client asks a server to do some work and send back the results. • 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 2 ל ' סשת א רדא " ג The Computer Communication Cource 1

  2. 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 3 ל ' סשת א רדא " ג 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() 4 ל ' סשת א רדא " ג The Computer Communication Cource 2

  3. 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 5 ל ' סשת א רדא " ג 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; ◆ 6 ל ' סשת א רדא " ג The Computer Communication Cource 3

  4. 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 ◆ 7 ל ' סשת א רדא " ג 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 ◆ 8 ל ' סשת א רדא " ג The Computer Communication Cource 4

  5. Implementation of Thread Pool Reader Thread: Reader Thread: ■ ■ t = read(); /* reads tasks */ t = read(); /* reads tasks */ ◆ ◆ synchronized (taskQueue synchronized ( 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 = taskQueue taskQueue.get(); .get(); • t = ✦ } } ✦ } } 9 ל ' סשת א רדא " ג The Computer Communication Cource http://www-106.ibm.com/developerworks/java/library/j-jtp0730.html public class WorkQueue WorkQueue public class ■ ■ { { ■ ■ 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(Runnable public void execute( Runnable r) { r) { ■ ■ synchronized(queue) { synchronized(queue) { ■ ■ queue.addLast addLast(r); (r); queue. ■ ■ queue.notify(); queue.notify(); ■ ■ } } ■ ■ } } ■ ■ 10 ל ' סשת א רדא " ג The Computer Communication Cource 5

  6. PoolWorker p private class PoolWorker extends Thread { rivate class PoolWorker extends Thread { ■ ■ public void run() { public void run() { ■ ■ Runnable r; Runnable r; ■ ■ while (true) { while (true) { ■ ■ synchronized(queue) { synchronized(queue) { ■ ■ while (queue.isEmpty isEmpty()) { ()) { while (queue. ■ ■ queue.wait(); queue.wait(); } } ■ ■ r = (Runnable r = ( 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 (RuntimeException } catch ( RuntimeException e) { e) { ■ ■ // You might want to log something here // You might want to log something here ■ ■ } } } } } } } } ■ ■ 11 ל ' סשת א רדא " ג The Computer Communication Cource 6

Recommend


More recommend