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: <process. h> 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. <process.h> 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