CSCE 515: Concurrent Server Computer Network Programming Design Alternatives ------ Advanced Socket Programming Wenyuan Xu Department of Computer Science and Engineering University of South Carolina Ref: Dave Hollinger Ref: UNP Chapter 7, 11, 30 Concurrent Server One child per client Design Alternatives • Traditional Unix server: One child process per client � TCP: after call to accept(), call fork(). � UDP: after recvfrom(), call fork(). Spawn one thread per client � Each process needs only a few sockets. Preforking multiple processes � Small requests can be serviced in a small amount of time. Prethreaded Server • Parent process needs to clean up after children!!!! (call wait() ). CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming One thread per client Example main(int argc, char **argv) main(int argc, char **argv) • Almost like using fork - call pthread_create { listenfd=socket(…) { pthread_t tid; Bind(listenfd…) instead. Listen(listenfd,LISTENQ); listenfd=socket(…) Signal(SIGGHLD, sig_chld); Bind(listenfd…) • Using threads makes it easier (less Signal(SIGINT,sig_int); Listen(listenfd,LISTENQ); For( ; ;) { For( ; ;) { overhead) to have sibling processes share connfd = Accept(listenfd, connfd = …); Accept(listenfd, …); information. if ( (pid = Fork())==0) { Pthread_creat(&tid, NULL, &doit, (void *) Close(listendf); connfd); doit(connfd); • Sharing information must be done } Close(connfd); } exit(0); carefully (use pthread_mutex) static void doit (void *arg) } { } … Close(connfd); Close( (int) arg); } return NULL; Process version } Thread version CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Pre fork() ’d Server Pre fork() ’d TCP Server • Creating a new process for each client is • Initial process creates socket and binds to expensive. well known address. • Process now calls fork() a bunch of • We can create a bunch of processes, each times. of which can take care of a client. • All children call accept(). • The next incoming connection will be • Each child process is an iterative server. handed to one child. CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Example listen() main(int argc, char **argv) main(int argc, char **argv) { listenfd=socket(…) { listenfd=socket(…) Sum of both queues Bind(listenfd…) cannot exceed backlog Bind(listenfd…) Listen(listenfd,LISTENQ); Listen(listenfd,LISTENQ); Server Signal(SIGGHLD, sig_chld); Signal(SIGGHLD, sig_chld); Signal(SIGINT,sig_int); accept Signal(SIGINT,sig_int); For( ; ;) { connfd = Accept(listenfd, …); for(i=0;i<nchildren;i++) { if ( (pid = Fork())==0) { if ((pid = Fork())==0) { Close(listendf); Completed connection queue for(;;) { 3-way doit(connfd); handshake connfd = Accept(listenfd, Close(connfd); complete TCP …); exit(0); } Incomplete connection queue doit(connfd); } Close(connfd); Close(connfd); } } \\for } } \\if & for } arriving SYN Process version Prefork version CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Preforking Sockets library vs. system call • As the book shows, having too many • A preforked TCP server won’t usually work preforked children can be bad. the way we want if sockets is not part of the kernel: • Using dynamic process allocation instead � calling accept() is a library call, not an atomic of a hard-coded number of children can operation. avoid problems. • We can get around this by making sure • The parent process just manages the only one child calls accept() at a time children, doesn’t worry about clients. using some locking scheme. CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
What’s the best server design Prethreaded Server for my application? • Same benefits as preforking. • Many factors: • Can also have the main thread do all the � expected number of simultaneous clients. calls to accept() and hand off each client � Transaction size (time to compute or lookup to an existing thread. the answer) � Variability in transaction size. � Available system resources (perhaps what resources can be required in order to run the service). CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Server Design • Socket Options • Posix name/address conversion • It is important to understand the issues and options. � It's important to know about some of • Knowledge of queuing theory can be a big these topics, although it might not be help. apparent how and when to use them. • You might need to test a few alternatives to determine the best design. � Details are in the book - we are just trying to get some idea of what can be done. CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Socket Options � Various attributes that are used to determine the behavior of sockets. Socket Options � Setting options tells the OS/Protocol Stack the behavior we want. � Support for generic options (apply to all sockets) and protocol specific options. CSCE515 – Computer Network Programming
Read-Only Socket Options Option types � Some options are readable only (we can’t � Many socket options are Boolean flags set the value). indicating whether some feature is enabled (1) or disabled (0). � Other options are associated with more complex types including int, timeval, in_addr, sockaddr , etc. CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming getsockopt() Setting and Getting option values int getsockopt( int sockfd, getsockopt() gets the current value of a int level, socket option. int optname, void *opval, setsockopt() is used to set the value of a socklen_t *optlen); socket option. level specifies whether the option is a general option or a protocol specific #include <sys/socket.h> option (what level of code should interpret the option). CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Socket and IP-layer socket options setsockopt() Level Optname Get Set Flag Data type int setsockopt( int sockfd, SOL_SOCKET SO_ERROR Y N int int level, SO_LINGER Y Y linger int optname, SO_KEEPALIVE Y Y Y int const void *opval, IPPRORO_IP IP_HDRINCL Y Y Y Int socklen_t optlen); IP_TOS Y Y Y int IPPROTO_TCP TCP_MAXSEG Y Y int TCP_NODELAY Y Y Y int CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Example: SO_LINGER SO_LINGER � Specifies how the close function operates for a Value is of type: connection-oriented protocol. struct linger { int l_onoff; /* 0 = off */ #include <unistd.h> int l_linger; /* time in seconds */ int close(int socketfd ); }; � Used to control whether and how long a � Decrease the reference count for the descriptor call to close will wait for pending ACKS. � If the reference count is 0: � send any data that is already queued to be sent to the � connection-oriented sockets only. other end � Normal TCP Connection termination sequence CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming SO_LINGER usage SO_LINGER � l_onoff = 1 & l_linger =0 � By default, calling close() on a TCP socket will return immediately. � TCP aborts the connections when it is closed � The closing process has no way of knowing whether or not the peer � l_onoff = 1 & l_linger != 0 received all data. � close return if either: � Setting SO_LINGER means the closing � all the data is sent and acked process can determine that the peer � the linger time has expired. machine has received the data (but not that the data has been read() !). � Check an example CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming shutdown() vs SO_LINGER shutdown Summary � Starts TCP’s normal connection termination sequence, regardless of the reference count � close returns immediately without waiting #include <sys/socket.h> at all int shutdown(int sockfd , int howto ); � close lingers until the ACK of our FIN is received � howto � SHUT_RD: the read half of the connection is closed � shutdown followed by a read waits until � SHUT_WR: the write half of the connection is closed we receive the peer’s FIN � SHUT_RDWR: the read half and the write half of the connection are both closed CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Recommend
More recommend