Скачать презентацию Data Communications and Networking Socket Programming Part II Скачать презентацию Data Communications and Networking Socket Programming Part II

d6b3e07ea61cd8b0925ebbdc226f4ab6.ppt

  • Количество слайдов: 24

Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking 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 1

Outline • Review of multithreading • Server Design —Iterative, Connectionless —Iterative, Connection-Oriented —Concurrent, Connectionless Outline • Review of multithreading • Server Design —Iterative, Connectionless —Iterative, Connection-Oriented —Concurrent, Connectionless —Concurrent, Connection-Oriented • Multi-thread • Singly threaded (not required by COMP 2330) 2

Multithreading • In modern operating systems, a process can have multiple threads —They share 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

Threads in MS Windows • A thread is executed independently of other threads. • 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. 4

Threads in MS Windows (Cont. ) • A program can call _beginthread() to create 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 1 st parameter: start address of the function to be executed The 2 nd parameter: stack size for the new thread; 0 for system default The 3 rd 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. 5

Threads in MS Windows (Cont. ) /* threadsum. c */ /* To compile: > Threads in MS Windows (Cont. ) /* threadsum. c */ /* To compile: > cl threadsum. c /MT */ int addem(int count) { int i, sum; #include #include #include sum = 0; for(i = 1; i <= count; i++) { int addem(int); printf(“The value of i is %dn”, i); int main() { _beginthread( (void (*)(void())addem, 0, (void *)5); addem(5); return 0; } fflush(stdout); sum += i; } printf( “The sum of i is %d n”, sum); fflush(stdout); return 0; } 6

Threads in MS Windows (Cont. ) 7 Threads in MS Windows (Cont. ) 7

Server Design Issues • Connectionless vs. connection-oriented —UDP vs. TCP —Reliability issue? TCP wins. 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 (Cont. ) • Iterative vs. concurrent —Iterative server handles one request 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. 9

Iterative Server server client being served waiting clients 10 Iterative Server server client being served waiting clients 10

Concurrent Server 11 Concurrent Server 11

(1) Iterative, Connectionless Server Algorithm 1. Create a datagram (UDP) socket and bind to (1) Iterative, Connectionless Server Algorithm 1. Create a datagram (UDP) socket and bind to the well-known address for the service being offered. 2. Repeatedly receive the next request from a client, formulate a response, and send a reply back to the client according to the application protocol. It is the most common form of connectionless server, used especially for services that require a trivial amount of processing for each request. Example: http: //www. comp. hkbu. edu. hk/~comp 2330/example/timeudpsrv. c http: //www. comp. hkbu. edu. hk/~comp 2330/example/udpechosrv 01. c 12

(2) Iterative, Connection-Oriented Server Algorithm 1. 2. 3. 4. 5. Create a stream (TCP) (2) Iterative, Connection-Oriented Server Algorithm 1. 2. 3. 4. 5. Create a stream (TCP) socket and bind to the well-known address for the service being offered. Place the socket in passive mode, making it ready for use by a server. Accept the next connection request from the socket, and obtain a new socket for the connection. Repeatedly receive a request from the client, formulate a response, and send a reply back to the client according to the application protocol. When finished with a particular client, close the connection and return to step 3 to accept a new connection. A less common server type used for services that require a trivial amount of processing for each request, but for which reliable transport is necessary. Example: http: //www. comp. hkbu. edu. hk/~comp 2330/example/daytimetcpsrv. c 13

(3) Concurrent, Connectionless Server Algorithm • Master 1. Create a socket and bind to (3) Concurrent, Connectionless Server Algorithm • Master 1. Create a socket and bind to the well-known address for the service being offered. Leave the socket unconnected. • Master 2. Repeatedly call recvfrom to receive the next request from a client, and create a new slave thread to handle the response. • Slave 1. Receive a specific request upon creation as well as access to the socket. • Slave 2. Form a reply according to the application protocol and send it back to the client using sendto. • Slave 3. Exit (i. e. , a slave thread terminates after handling one request). It is an uncommon type in which the server creates a new thread to handle each request. 14

(4) Concurrent, Connection. Oriented Server Algorithm • Master 1. Create a socket and bind (4) Concurrent, Connection. Oriented Server Algorithm • Master 1. Create a socket and bind to the well-known address for the service being offered. Leave the socket unconnected. • Master 2. Place the socket in passive mode, making it ready for use by a server. • Master 3. Repeatedly call accept to receive the next request from a client, and create a new slave thread to handle the response. • Slave 1. Receive a connection request (i. e. , socket for the connection) upon creation. • Slave 2. Interact with the client using the connection: receive request(s) and send back response(s). • Slave 3. Close the connection and exit. The slave thread exists after handling all requests from one client. The most general type of server, and the most common implementation. Example: http: //www. comp. hkbu. edu. hk/~comp 2330/example/tcpechosrv 01. c 15

Thread Structure 1. The master server thread doesn’t communicate with clients directly. It merely Thread Structure 1. The master server thread doesn’t communicate with clients directly. It merely waits at the well-known port for the next connection request (accept()). 2. Once a request has arrived, the master server thread returns the socket descriptor, and creates a slave thread to handle the connection. The new socket descriptor is passed to the slave thread as a parameter. 16

(5) Concurrent, Connection-Oriented Server Algorithm: single thread 1. 2. 3. 4. 5. Create a (5) Concurrent, Connection-Oriented Server Algorithm: single thread 1. 2. 3. 4. 5. Create a stream socket and bind to the well-known port for the service. Add socket to the list of those on which I/O is possible. Use select to wait for I/O on existing sockets. If original socket is ready, use accept to obtain the next connection, and add the new socket to the list of those on which I/O is possible. If some socket other than the original is ready, use recv to obtain the next request, form a response, and use send to send the response back to the client. Continue processing with step 2 above. The most general type of server, and a less common implementation. Example: http: //www. comp. hkbu. edu. hk/~comp 2330/example/tcpechosrv 02. c 17

Thread Structure 1. The single thread maintains all the sockets, including the one listening Thread Structure 1. The single thread maintains all the sockets, including the one listening socket and all other connected sockets. 2. The single thread use select() to monitor all the interested sockets: event-driven! 3. If nothing happens, select() just blocks itself. If select() returns, it means some event(s) happened and we need to response! 18

Thread vs. select() • There is a debate between thread programming and event-based programming. Thread vs. select() • There is a debate between thread programming and event-based programming. • J. K. Ousterhout, “Why Threads Are a Bad Idea (for most purposes), ” Presentation given at the 1996 Usenix Annual Technical Conference, January 1996. —http: //home. pacbell. net/ouster/ • R. Behren, J. Condit, and E. Brewer, “Why Events Are a Bad Idea (for high-concurrency servers), ” Usenix Hot. OS, May 2003. (from University of California at Berkeley) 19

Appendix: select() in MS Windows • The normal Blocking I/O Model: application recvfrom() kernel Appendix: select() in MS Windows • The normal Blocking I/O Model: application recvfrom() kernel system call no datagram ready wait for data Process blocks in call to recvfrom() datagram ready copy datagram return OK process the datagram copy complete copy data from kernel to user 20

Appendix: select() in MS Windows application Process blocks in call to select(), waiting for Appendix: select() in MS Windows application Process blocks in call to select(), waiting for one of possibly many sockets to become readable select() kernel system call no datagram ready wait for data return readable recvfrom() Process blocks while data copied into application buffer system call return OK datagram ready copy datagram copy complete copy data from kernel to user process the datagram 21

Appendix: select() in MS Windows int select ( int nfds, fd_set * readfds, fd_set Appendix: select() in MS Windows int select ( int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, const struct timeval * timeout ); select() returns the number of ready file descriptors if successful, 0 if the time limit was reached. From winsock 2. h: #ifndef FD_SETSIZE #define FD_SETSIZE 64 #endif typedef struct fd_set { u_int fd_count; /* how many are SET? */ SOCKET fd_array[FD_SETSIZE]; /* an array of SOCKETs */ } fd_set; fd_set is used to record an array of socket descriptors that we are interested in. select() can monitor three sets of sockets, each for a different purpose: read, write, and exception. select() tells the OS to wait for any one of multiple events to occur, and to wake up the process only when one or more of these events occurs, or when a specified amount of time has passed. 22

Appendix: select() in MS Windows • A set of macros are defined to manipulate Appendix: select() in MS Windows • A set of macros are defined to manipulate fd_set • Example: fd_set readfd; socket s; FD_ZERO(&readfd); /* initialize readfd to empty */ FD_SET(s, &readfd); /* add s to readfd */ FD_CLR(s, &readfd); /* remove s from readfd */ FD_ISSET(s, &readfd); /* check if s belongs to readfd */ You can check the definition of these macros in winsock 2. h ! 23

Appendix: select() in MS Windows When will select() return? 1. For socket descriptors in Appendix: select() in MS Windows When will select() return? 1. For socket descriptors in readfds: • • If it is a listening socket, an arrival of a new TCP connection request can cause select() to return. For this case, normally the program will call accept() to create a new socket, and put that new socket into the readfds (or writefds) using FD_SET. If it is a connected socket, the arrival of new data may cause select() to return. Normally the program can call read() to get the data. If read() returns 0, it means the client has closed the socket. 2. For socket descriptors in writefds: • Normally it means data are ready to be sent. 24