Three Basic Mechanisms for Creating Concurrent Flows 15 213 The course that gives CMU its Zip 1 Processes Concurrent Programming November 30 2007 2 Threads Topics Kernel automatically interleaves multiple logical flows Each flow has its own private address space Kernel automatically interleaves multiple logical flows Each flow shares the same address space 3 I O multiplexing with select Event based concurrent servers Shared variables The need for synchronization Synchronizing with semaphores Application manually interleaves multiple logical flows Each flow shares the same address space Popular for high performance server designs 15 213 F 07 2 lecture 25 ppt Appr 3 Event Based Concurrent Servers Using I O Multiplexing The select Function select sleeps until one or more file descriptors in the set readset ready for reading Maintain a pool of connected descriptors include sys select h Repeat the following forever int select int maxfdp1 fd set readset NULL NULL NULL Use the Unix select function to block until z a New connection request arrives on the listening descriptor 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 z 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 z Close connection on EOF and remove it from the pool 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 15 213 F 07 3 15 213 F 07 4 Macros for Manipulating Set Descriptors Overall Structure listenfd void FD ZERO fd set fdset fdset void FD SET int fd fd fd set fdset fdset Turn on bit fd in fdset void FD CLR int fd fd fd set fdset fdset Turn off bit fd in fdset int FD ISSET int fd fd fdset fdset 5 Manage Pool of Connections clientfd Turn off all bits in fdset Is bit fd in fdset turned on 0 10 1 7 2 4 3 1 4 1 5 12 6 5 7 1 8 1 9 1 15 213 F 07 6 Page 1 Active Inactive Use select to detect activity Active listenfd Listen for requests from new clients Active clients Ones with a valid connection New request on listenfd Request by active client Required Activities Never Used Adding new clients Removing terminated clients Echoing 15 213 F 07 Representing Pool of Clients Pool Example listenfd 3 echoservers c A concurrent echo server based on select include csapp h clientfd 0 10 1 7 typedef struct represents a pool of connected descriptors int maxfd largest descriptor in read set fd set read set set of all active descriptors fd set ready set subset of descriptors ready for reading int nready number of ready descriptors from select int maxi highwater index into client array int clientfd FD SETSIZE set of active descriptors rio t clientrio FD SETSIZE set of active read buffers pool 2 4 3 1 int byte cnt 0 counts total bytes received by server 4 1 5 12 6 5 7 1 8 1 9 1 maxfd 12 maxi 6 read set 3 4 5 7 10 12 Active Inactive Active Never Used 15 213 F 07 7 15 213 F 07 8 Main Loop Pool Initialization int main int argc char argv int listenfd connfd clientlen sizeof struct sockaddr in struct sockaddr in clientaddr static pool pool initialize the descriptor pool void init pool int listenfd pool p Initially there are no connected descriptors int i p maxi 1 for i 0 i FD SETSIZE i p clientfd i 1 listenfd Open listenfd argv 1 init pool listenfd pool while 1 pool ready set pool read set pool nready Select pool maxfd 1 pool ready set NULL NULL NULL Initially listenfd is only member of select read set p maxfd listenfd FD ZERO p read set FD SET listenfd p read set if FD ISSET listenfd pool ready set connfd Accept listenfd SA clientaddr clientlen add client connfd pool check clients pool 15 213 F 07 9 Initial Pool Main Loop listenfd 3 clientfd 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 15 213 F 07 10 int main int argc char argv int listenfd connfd clientlen sizeof struct sockaddr in struct sockaddr in clientaddr static pool pool maxfd 3 maxi 1 read set 3 listenfd Open listenfd argv 1 init pool listenfd pool while 1 pool ready set pool read set pool nready Select pool maxfd 1 pool ready set NULL NULL NULL Never Used if FD ISSET listenfd pool ready set connfd Accept listenfd SA clientaddr clientlen add client connfd pool check clients pool 11 15 213 F 07 12 Page 2 15 213 F 07 Adding Client void add client int connfd pool p int i p nready Adding Client with fd 11 listenfd 3 add connfd to pool p clientfd for i 0 i FD SETSIZE i Find available slot if p clientfd i 0 p clientfd i connfd Rio readinitb p clientrio i connfd FD SET connfd p read set Add desc to read set if connfd p maxfd Update max descriptor num p maxfd connfd if i p maxi Update pool high water mark p maxi i break if i FD SETSIZE Couldn t find an empty slot app error add client error Too many clients 0 10 1 7 2 4 3 1 11 4 1 5 12 6 5 7 1 8 1 9 1 maxfd 12 maxi 6 read set 3 4 5 7 10 11 12 Active Inactive Active Never Used 15 213 F 07 13 15 213 F 07 14 Checking Clients Concurrency Limitations void check clients pool p echo line from ready descs in pool p int i connfd n char buf MAXLINE rio t rio if connfd 0 FD ISSET connfd p ready set p nready if n Rio readlineb rio buf MAXLINE 0 byte cnt n Rio writen connfd buf n for i 0 i p maxi p nready 0 i connfd p clientfd i rio p clientrio i Does not return until complete line received If the descriptor is ready echo a text line from it if connfd 0 FD ISSET connfd p ready set p nready if n Rio readlineb rio buf MAXLINE 0 byte cnt n Rio writen connfd buf n else EOF detected remove descriptor from pool Close connfd FD CLR connfd p read set p clientfd i 1 Current design will hang up if partial line transmitted Bad to have network code that can hang up if client does something weird Would require more work to implement more robust version z By mistake or maliciously z Must allow each read to return only part of line and reassemble lines within server 15 213 F 07 15 15 213 F 07 16 Pro and Cons of Event Based Designs A …
View Full Document