Iterative servers 15 213 The course that gives CMU its Zip Iterative servers process one request at a time client 1 Concurrent Servers server call connect client 2 call accept call connect ret connect ret accept write call read December 4 2001 ret read close Topics Limitations of iterative servers Process based concurrent servers Threads based concurrent servers Event based concurrent servers ret read close class28 ppt server client 2 2 CS 213 F 01 Concurrent servers Concurrent servers handle multiple requests concurrently call accept client 1 server Server blocks call read waiting for data from Client 1 Client 1 blocks waiting for user to type in data call connect Solution use concurrent servers instead Concurrent servers use multiple concurrent flows to serve multiple clients at the same time ret accept 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 3 CS 213 F 01 class28 ppt ret connect call fgets write call read write close class28 ppt call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch client 2 call accept call connect ret accept User goes out to lunch call read close ret connect call fgets ret connect write The fundamental flaw of iterative servers call connect call accept ret accept class28 ppt client 1 close 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 cont 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 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 7 CS 213 F 01 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 Kernel keeps reference for each socket After fork refcnt connfd 2 Connection will not be closed until refcnt connfd 0 9 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 Server must close its copy of connfd class28 ppt Pros and cons of process based designs CS 213 F 01 Threads provide more efficient flows with easier sharing of data between the flows class28 ppt Traditional view of a process Process process context code data and stack Process context Program context Data registers Condition codes Stack pointer SP Program counter PC CS 213 F 01 Alternate view of a process Process thread code data and kernel context Thread main thread Code data and stack Code and Data stack SP shared libraries shared libraries stack SP brk run time heap read write data Thread context brk run time heap read write data Kernel context VM structures Descriptor table brk pointer 10 PC read only code data PC Data registers Condition codes Stack pointer SP Program counter PC read only code data 0 Kernel context 0 VM structures Descriptor table brk pointer class28 ppt 11 CS 213 F 01 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 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 Thread 2 peer thread T4 shared libraries T1 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 P1 shared code data and kernel context Thread 2 context Data registers Condition codes SP2 PC2 sh T5 13 foo bar CS 213 F 01 class28 ppt 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 14 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 Thread C How threads and processes are different Concurrent A B A C Sequential B C 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 Time class28 ppt sh T3 VM structures Descriptor table brk pointer class28 ppt sh 15 CS 213 F 01 class28 ppt 16 CS 213 F
View Full Document