Concurrent Servers December 4, 2001Topics• Limitations of iterative servers• Process-based concurrent servers• Threads-based concurrent servers• Event-based concurrent serversclass28.ppt15-213“The course that gives CMU its Zip!”CS 213 F’01– 2 –class28.pptIterative serversIterative servers process one request at a time.client 1 server client 2call connectcall acceptret connectret acceptcall connectcall readwriteret readcloseclosecall acceptret connectcall readret readclosewriteret acceptcloseCS 213 F’01– 3 –class28.pptThe fundamental flaw of iterative serversSolution: use concurrent servers instead.• Concurrent servers use multiple concurrent flows to serve multipleclients at the same time.client 1 server client 2call connectcall acceptcall readret connectret acceptcall connectcall fgetsUser goesout to lunchClient 1 blockswaiting for userto type in dataClient 2 blockswaiting to completeits connection request until afterlunch!Server blockswaiting fordata fromClient 1CS 213 F’01– 4 –class28.pptConcurrent serversclient 1 server client 2call connectcall acceptcall readret connectret acceptcall connectcall fgetsforkchild 1User goesout to lunchClient 1blockswaiting foruser to typein datacall acceptret connectret acceptcall fgetswriteforkcall readchild 2writecall readend readcloseclose...Concurrent servers handle multiple requests concurrently.CS 213 F’01– 5 –class28.pptThree basic mechanisms for creatingconcurrent flows1. Processes• Kernel provides multiple control flows with separate addressspaces.• Standard Unix process control and signals.2. Threads• Kernel provides multiple control flows (threads) running in oneprocess.–Each thread has its own stack and register values.–All threads share the same address space and open files.• POSIX threads (Pthreads) interface.3. I/O multiplexing with select()• Manually interleave the processing of multiple open connections.• Use Unix select() function to notice pending socket activity.• Form of manual, application-level concurrency.• Popular for high-performance server designs.CS 213 F’01– 6 –class28.pptProcess-based concurrent server/* * echoserverp.c - A concurrent echo server based on processes * Usage: echoserverp <port> */#include <ics.h>#define BUFSIZE 1024void echo(int connfd);void handler(int sig);int main(int argc, char **argv) { int listenfd, connfd; int portno; struct sockaddr_in clientaddr; int clientlen = sizeof(struct sockaddr_in); if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } portno = atoi(argv[1]); listenfd = open_listenfd(portno);CS 213 F’01– 7 –class28.pptProcess-based concurrent server (cont) Signal(SIGCHLD, handler); /* parent must reap children! */ /* main server loop */ while (1) { connfd = Accept(listenfd, (struct sockaddr *) &clientaddr, &clientlen)); if (Fork() == 0) { Close(listenfd); /* child closes its listening socket */ echo(connfd); /* child reads and echoes input line */ Close(connfd); /* child is done with this client */ exit(0); /* child exits */ } Close(connfd); /* parent must close connected socket! */ }}CS 213 F’01– 8 –class28.pptProcess-based concurrent server (cont)/* handler - reaps children as they terminate */void handler(int sig) { pid_t pid; int stat; while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) ; return;}CS 213 F’01– 9 –class28.pptImplementation issues with process-based designsServer should restart accept call if it is interrupted bya transfer of control to the SIGCHLD handler• Not necessary for systems with POSIX signal handling.–Our Signal wrapper tells kernel to automatically restart accept• Required for portability on some older Unix systems.Server must reap zombie children• to avoid fatal memory leak.Server must close its copy of connfd.• Kernel keeps reference for each socket.• After fork, refcnt(connfd) = 2.• Connection will not be closed until refcnt(connfd)=0.CS 213 F’01– 10 –class28.pptPros and cons of process-based designs+ Handles multiple connections concurrently+ Clean sharing model• descriptors (no)• file tables (yes)• global variables (no)+ Simple and straightforward.- Additional overhead for process control.- Nontrivial to share data between processes.• Requires IPC (interprocess communication) mechanisms–FIFO’s (named pipes), System V shared memory and semaphoresThreads provide more efficient flows with easiersharing of data between the flowsCS 213 F’01– 11 –class28.pptTraditional view of a processshared librariesrun-time heap0read/write dataProcess = process context + code, data, and stackProgram context: Data registers Condition codes Stack pointer (SP) Program counter (PC)Kernel context: VM structures Descriptor table brk pointerCode, data, and stackread-only code/datastackSPPCbrkProcess contextCS 213 F’01– 12 –class28.pptAlternate view of a processshared librariesrun-time heap0read/write dataProcess = thread + code, data, and kernel contextThread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Code and Dataread-only code/datastackSPPCbrkThread (main thread)Kernel context: VM structures Descriptor table brk pointerCS 213 F’01– 13 –class28.pptA process with multiple threadsshared librariesrun-time heap0read/write dataThread 1 context: Data registers Condition codes SP1 PC1 Shared code and dataread-only code/datastack 1Thread 1 (main thread)Kernel context: VM structures Descriptor table brk pointerMultiple threads can be associated with a process• Each thread has its own logical control flow (sequence of PC values)• Each thread shares the same code, data, and kernel context• Each thread has its own thread id (TID)Thread 2 context: Data registers Condition codes SP2 PC2stack 2Thread 2 (peer thread)CS 213 F’01– 14 –class28.pptLogical view of threadsThreads associated with a process form a pool of peers.• Unlike processes which form a tree hierarchyP0P1sh sh shfoobarT1Process hierarchyThreads associated with process fooT2T4T5T3shared code, dataand kernel contextCS 213 F’01– 15 –class28.pptConcurrent thread executionTwo threads run concurrently (are concurrent) if theirlogical flows overlap in time.Otherwise, they are sequential.Examples:• Concurrent: A & B, A&C• Sequential: B & CTimeThread A Thread B Thread
View Full Document