15 213 The course that gives CMU its Zip Concurrent Servers December 4 2001 Topics class28 ppt Limitations of iterative servers Process based concurrent servers Threads based concurrent servers Event based concurrent servers Iterative servers Iterative servers process one request at a time client 1 server call connect call accept client 2 call connect ret connect ret accept write call read ret read close close call accept ret accept ret connect call read write ret read close close class28 ppt 2 CS 213 F 01 The fundamental flaw of iterative servers client 1 call connect server client 2 call accept ret connect ret accept call fgets User goes out to lunch Server blocks call read waiting for data from Client 1 Client 1 blocks waiting for user to type in data call connect Client 2 blocks waiting to complete its connection request until after lunch Solution use concurrent servers instead Concurrent servers use multiple concurrent flows to serve multiple clients at the same time class28 ppt 3 CS 213 F 01 Concurrent servers Concurrent servers handle multiple requests concurrently client 1 server client 2 call accept call connect call connect ret connect ret accept call fgets User goes out to lunch Client 1 blocks waiting for user to type in data fork child 1 call accept call read ret accept fork child 2 call read write call read write close class28 ppt ret connect call fgets 4 end read close CS 213 F 01 Three basic mechanisms for creating concurrent flows 1 Processes Kernel provides multiple control flows with separate address spaces Standard Unix process control and signals 2 Threads Kernel provides multiple control flows threads running in one process 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 class28 ppt 5 CS 213 F 01 Process based concurrent server echoserverp c A concurrent echo server based on processes Usage echoserverp port include ics h define BUFSIZE 1024 void 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 class28 ppt 6 CS 213 F 01 Process 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 class28 ppt 7 CS 213 F 01 Process 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 class28 ppt 8 CS 213 F 01 Implementation issues with process based designs Server should restart accept call if it is interrupted by a 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 class28 ppt 9 CS 213 F 01 Pros 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 semaphores Threads provide more efficient flows with easier sharing of data between the flows class28 ppt 10 CS 213 F 01 Traditional view of a process Process process context code data and stack Code data and stack Process context Program context Data registers Condition codes Stack pointer SP Program counter PC stack SP shared libraries brk run time heap read write data Kernel context VM structures Descriptor table brk pointer class28 ppt PC read only code data 0 11 CS 213 F 01 Alternate view of a process Process thread code data and kernel context Code and Data Thread main thread shared libraries stack SP brk run time heap read write data Thread context PC Data registers Condition codes Stack pointer SP Program counter PC read only code data 0 Kernel context VM structures Descriptor table brk pointer class28 ppt 12 CS 213 F 01 A process with multiple threads Multiple 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 1 main thread Shared code and data Thread 2 peer thread shared libraries stack 1 stack 2 run time heap read write data Thread 1 context Data registers Condition codes SP1 PC1 read only code data 0 Kernel context Thread 2 context Data registers Condition codes SP2 PC2 VM structures Descriptor table brk pointer class28 ppt 13 CS 213 F 01 Logical view of threads Threads associated with a process form a pool of peers Unlike processes which form a tree hierarchy Threads associated with process foo Process hierarchy P0 T2 T4 T1 P1 shared code data and kernel context sh T5 sh sh T3 foo bar class28 ppt 14 CS 213 F 01 Concurrent thread execution Two threads run concurrently are concurrent if their logical flows overlap in time Otherwise they are sequential Thread A Examples Thread B Thread C Concurrent A B A C Sequential B C Time class28 ppt 15 CS 213 F 01 Threads vs processes How threads and processes are similar Each has its own logical control flow Each can run concurrently Each is context switched How threads and processes are different Threads share code and data processes typically do not Threads are somewhat less expensive than processes process control creating and reaping is twice as expensive as thread control Linux Pentium III numbers 20K cycles to create and reap a process 10K cycles to create and reap a thread class28 ppt 16 CS 213 F
View Full Document