15 213 The course that gives CMU its Zip Concurrent Programming November 20 2003 Topics class26 ppt Limitations of iterative servers Process based concurrent servers Event based concurrent servers Threads based concurrent servers Concurrent Programming is Hard The human mind tends to be sequential The notion of time is often misleading Thinking about all possible sequences of events in a computer system is at least error prone and frequently impossible Classical problem classes of concurrent programs Races outcome depends on arbitrary scheduling decisions elsewhere in the system Deadlock improper resource allocation prevents forward progress Lifelock Starvation Fairness external events and or system scheduling decisions can prevent sub task progress Many aspects of concurrent programming are beyond the scope of 15 213 2 15 213 F 03 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 write close 3 ret connect call read ret read close 15 213 F 03 Fundamental Flaw of Iterative Servers client 1 call connect ret connect call fgets User goes out to lunch server client 2 call accept ret accept 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 4 Concurrent servers use multiple concurrent flows to serve multiple clients at the same time 15 213 F 03 Concurrent Servers Multiple Processes Concurrent servers handle multiple requests concurrently client 1 server call accept call connect ret connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data client 2 ret accept child 1 fork call accept call read ret accept fork child 2 call read ret connect call fgets write call read write close 5 call connect end read close 15 213 F 03 Three Basic Mechanisms for Creating Concurrent Flows 1 Processes Kernel automatically interleaves multiple logical flows Each flow has its own private address space 2 I O multiplexing with select User manually interleaves multiple logical flows Each flow shares the same address space Popular for high performance server designs 3 Threads 6 Kernel automatically interleaves multiple logical flows Each flow shares the same address space Hybrid of processes and I O multiplexing 15 213 F 03 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 7 15 213 F 03 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 8 15 213 F 03 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 9 15 213 F 03 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 10 Kernel keeps reference for each socket After fork refcnt connfd 2 Connection will not be closed until refcnt connfd 0 15 213 F 03 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 I O multiplexing provides more control with less overhead 11 15 213 F 03 Event Based Concurrent Servers Using I O Multiplexing Maintain a pool of connected descriptors Repeat the following forever Use the Unix select function to block until a New connection request arrives on the listening descriptor b New data arrives on an existing connected descriptor If a add the new connection to the pool of connections If b read any available data from the connection Close connection on EOF and remove it from the pool 12 15 213 F 03 The select Function select sleeps until one or more file descriptors in the set readset are ready for reading include sys select h int select int maxfdp1 fd set readset NULL NULL NULL readset Opaque bit vector max FD SETSIZE bits that indicates membership in a descriptor set If bit k is 1 then descriptor k is a member of the descriptor set maxfdp1 Maximum descriptor in descriptor set plus 1 Tests descriptors 0 1 2 maxfdp1 1 for set membership select returns the number of ready descriptors and sets each bit of readset to indicate the ready status of its corresponding descriptor 13 15 213 F 03 Macros for Manipulating Set Descriptors void FD ZERO fd set fdset Turn off all bits in fdset void FD SET int fd fd set fdset Turn on bit fd in fdset void FD CLR int fd fd set fdset Turn off bit fd in fdset int FD ISSET int fd fdset 14 Is bit fd in fdset turned on 15 213 F 03 select Example main loop wait for connection request or stdin command If connection request then echo input line and close connection If stdin command then process printf server fflush stdout while notdone select check if the user typed something to stdin or if a connection request arrived FD ZERO readfds initialize the fd set FD SET listenfd readfds add socket fd FD SET 0 readfds add stdin fd 0 Select listenfd 1 readfds NULL NULL NULL 15 15 213 F 03 select Example cont First we check for a pending event on stdin if the user has typed a command process it if FD ISSET 0 readfds fgets buf BUFSIZE stdin switch buf 0 case c print the connection count
View Full Document