15 213 The course that gives CMU its Zip Concurrent Programming November 18 2008 Topics Event based concurrent servers Shared variables The need for synchronization Synchronizing with semaphores lecture 23 ppt Three Basic Mechanisms for Creating Concurrent Flows 1 Processes Kernel automatically interleaves multiple logical flows Each flow has its own private address space 2 Threads Kernel automatically interleaves multiple logical flows Each flow shares the same address space 3 I O multiplexing with select 2 Programmer manually interleaves multiple logical flows All flows share the same address space Popular for high performance server designs 15 213 F 08 Appr 3 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 3 15 213 F 08 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 4 15 213 F 08 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 5 Is bit fd in fdset turned on 15 213 F 08 Overall Structure listenfd Manage Pool of Connections clientfd 0 10 1 7 2 4 3 1 4 1 5 12 6 7 8 9 6 5 Active Inactive Active listenfd Listen for requests from new clients Active clients Ones with a valid connection Use select to detect activity New request on listenfd Request by active client Required Activities 1 1 1 Never Used Adding new clients Removing terminated clients Echoing 15 213 F 08 Representing Pool of Clients echoservers c A concurrent echo server based on select include csapp h 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 int byte cnt 0 counts total bytes received by server 7 15 213 F 08 Pool Example listenfd 3 clientfd 0 10 1 7 2 4 3 1 4 1 5 12 6 7 8 9 5 maxfd 12 maxi 6 read set 3 4 5 7 10 12 Active Inactive Active 1 1 1 Never Used 8 15 213 F 08 Main Loop int main int argc char argv int listenfd connfd clientlen sizeof struct sockaddr in struct sockaddr in clientaddr static pool pool 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 if FD ISSET listenfd pool ready set connfd Accept listenfd SA clientaddr clientlen add client connfd pool check clients pool 9 15 213 F 08 Pool Initialization 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 Initially listenfd is only member of select read set p maxfd listenfd FD ZERO p read set FD SET listenfd p read set 10 15 213 F 08 Initial Pool listenfd 3 clientfd 0 1 1 1 2 1 3 1 4 1 5 1 6 7 8 9 1 maxfd 3 maxi 1 read set 3 Never Used 1 1 1 11 15 213 F 08 Main Loop int main int argc char argv int listenfd connfd clientlen sizeof struct sockaddr in struct sockaddr in clientaddr static pool pool 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 if FD ISSET listenfd pool ready set connfd Accept listenfd SA clientaddr clientlen add client connfd pool check clients pool 12 15 213 F 08 Adding Client void add client int connfd pool p int i p nready add connfd to pool p 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 13 15 213 F 08 Adding Client with fd 11 listenfd 3 clientfd 0 10 1 7 2 4 3 1 11 4 5 6 7 8 9 1 12 5 maxfd 12 maxi 6 read set 3 4 5 7 10 11 12 Active Inactive Active 1 1 1 Never Used 14 15 213 F 08 Checking Clients void check clients pool p echo line from ready descs in pool p int i connfd n char buf MAXLINE rio t rio for i 0 i p maxi p nready 0 i connfd p clientfd i rio p clientrio i 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 15 15 213 F 08 Concurrency Limitations 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 Does not return until complete line received Current design will gets stuck if partial line transmitted Bad to have network code that can get stuck if client does something weird By mistake or maliciously Would require more work to implement more robust version Must allow each read to return only part of line and reassemble lines within server 16 15 213 F 08 Pro and Cons of Event Based Designs One logical control flow Can single step with a debugger No process or thread control overhead …
View Full Document