Data communications and Networking Socket Programming Part I: Design of Server Software Reference Internetworking with TCP/p volume Client-Server Programming and Applications By Douglas E, Comer David L, Stevens
1 Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking with TCP/IP, Volume III Client-Server Programming and Applications By Douglas E. Comer, David L. Stevens
Outline Review of multithreading Server design -Iterative Connectionless -Iterative. Connection-Oriented Concurrent. Connectionless -Concurrent Connection-Oriented Multi-thread Singly threaded (not required by CoMP2330)
2 Outline • Review of multithreading • Server Design —Iterative, Connectionless —Iterative, Connection-Oriented —Concurrent, Connectionless —Concurrent, Connection-Oriented • Multi-thread • Singly threaded (not required by COMP2330)
Multithreading In modern operating systems, a process can have multiple threads They share the process's resources such as memory space, opened files. sockets. etc Advantages -Good for cPUs with multiple cores - -Can overlap different Io. E.g., a thread can wait for an input from the user, while another thread is receiving data from the network -Context switching between threads is faster than that between processes
3 Multithreading • In modern operating systems, a process can have multiple threads —They share the process’s resources such as memory space, opened files, sockets, etc. • Advantages —Good for CPUs with multiple cores —Can overlap different IO. E.g., a thread can wait for an input from the user, while another thread is receiving data from the network —Context switching between threads is faster than that between processes
Threads in ms windows a thread is executed independently of other threads All threads in a process share -Global variables -Resources that the os allocates to the process When multiple threads execute a piece of code concurrent -Each thread has its own, independent copy of the local variables associated with the code -Each has its own run-time stack of procedure activation records
4 Threads in MS Windows • A thread is executed independently of other threads. • All threads in a process share: —Global variables —Resources that the OS allocates to the process • When multiple threads execute a piece of code concurrently —Each thread has its own, independent copy of the local variables associated with the code. —Each has its own run-time stack of procedure activation records
Threads in MS Windows(Cont) A program can call beginthreado to create a new thread to execute a specified function. Then the program and the newly created thread are executed concurrently unsigned long beginthread( void(* Func )(void *), unsigned stack size, void arglist) The lst parameter: start address of the function to be executed The 2nd parameter: stack size for the new thread: 0 for system default The 3rd parameter: pointer to a list of parameters to be passed to the function NULL for no parameter Two remarks: must be included 2. The application must link with one of the multithreaded C run-time libraries E using the "/MT option of cl. exe command 5
5 Threads in MS Windows (Cont.) • A program can call _beginthread() to create a new thread to execute a specified function. Then the program and the newly created thread are executed concurrently. unsigned long _beginthread( void(* Func )(void *), unsigned stack_size, void *arglist ); The 1st parameter: start address of the function to be executed The 2nd parameter: stack size for the new thread; 0 for system default The 3rd parameter: pointer to a list of parameters to be passed to the function; NULL for no parameter Two remarks: 1. must be included 2. The application must link with one of the multithreaded C run-time libraries. E.g., using the “/MT” option of cl.exe command
Threads in MS Windows(Cont / threadsum c * int addem(int count) /* To compile: >cl threadsum.c /MT*/ nt 1. sum #include for(i=1; i<=count; 1++) int addem (int) printf(" The value of i is %d\n,1); Int main( fflush(stdout) beginthread((void ()(void)addem, 0, (void *)5); sum +=1 addem(5); return 0 printf("The sum of i is %d n', sum) fflush(stdout) return 0 6
6 Threads in MS Windows (Cont.) int addem(int count) { int i, sum; sum = 0; for(i = 1; i cl threadsum.c /MT */ #include #include #include int addem(int); int main() { _beginthread( (void (*)(void())addem, 0, (void *)5); addem(5); return 0; }
Threads in Ms Windows(Cont y ca Visual Studio. NET 2003 Command Prompt □ D: \hkbu\teaching \comp2330\2009>cl threadsum.c /MT Copyright (C) Microsoft Corporation 1984-2002. All rights reserved or80x86 Microsoft (R)32-bit C/C++ Optimizing Compiler Version 13.10.6030 f Microsoft (R> Incremental Linker Version 7.10.6030 opyright (c) Microsoft Corporation. All rights reserved out: threadsum exe threads. obj D: \ hkbu \teaching \comp2330\ 2009>threadsum D: \hkbu \teaching\comp2330\2009>threadsum The value of i is 1 he value of i is 1 The value of i is 1 The value of i The value of i is 2 he value of i is/2 The value of i i The value of i is 3 f lue of i is 2 he value of i is 3 The value of i is3 The value of i is 4 The value of i is 4 The value of i is 5 The value of The value of i is 5 The sum is 15 The value of i is 5 is 15 The sum is 15 The sum is 15 D: \hkbu \teaching \comp2330\2009> D: \hkbu \teaching \comp2330\2009>
7 Threads in MS Windows (Cont.)
Server Design Issues Connectionless vs. connection-oriented - UDP vS TCP -Reliability issue? tCP wins - Support of broadcast or multicast? UDP wins -Real time applications? UDP may win Client and server must use the same protocol
8 Server Design Issues • Connectionless vs. connection-oriented —UDP vs. TCP —Reliability issue? TCP wins. —Support of broadcast or multicast? UDP wins. —Real time applications? UDP may win. • Client and server must use the same protocol
Server Design Issues(Cont) Iterative vs. concurrent -Iterative server handles one request at a time -If another request arrives while the server is busy handling an existing request, the new request has to wait Iterative servers are easier to design, implement and maintain Iterative server is not good for a service with a long request processing time -Concurrent server handles multiple requests concurrently It can decrease the response time It can achieve high performance on a server with multiple processors It can achieve high performance by overlapping processing and IO 9
9 Server Design Issues (Cont.) • Iterative vs. concurrent —Iterative server handles one request at a time. —If another request arrives while the server is busy handling an existing request, the new request has to wait. • Iterative servers are easier to design, implement, and maintain. • Iterative server is not good for a service with a long “request processing time”. —Concurrent server handles multiple requests concurrently. • It can decrease the response time. • It can achieve high performance on a server with multiple processors. • It can achieve high performance by overlapping processing and I/O
terative server server client being served waiting clients 10
10 Iterative Server server client being served waiting clients