Concurrent Programming November 20, 2003Concurrent Programming is Hard!Iterative ServersFundamental Flaw of Iterative ServersConcurrent Servers: Multiple ProcessesThree Basic Mechanisms for Creating Concurrent FlowsProcess-Based Concurrent ServerProcess-Based Concurrent Server (cont)Slide 9Implementation Issues With Process-Based DesignsPros and Cons of Process-Based DesignsEvent-Based Concurrent Servers Using I/O MultiplexingThe select FunctionMacros for Manipulating Set Descriptorsselect Exampleselect Example (cont)Slide 17Event-based Concurrent Echo ServerEvent-based Concurrent Server (cont)Slide 20Slide 21Slide 22Pro and Cons of Event-Based DesignsTraditional View of a ProcessAlternate View of a ProcessA Process With Multiple ThreadsLogical View of ThreadsConcurrent Thread ExecutionThreads vs. ProcessesPosix Threads (Pthreads) InterfaceThe Pthreads "hello, world" ProgramExecution of Threaded“hello, world”Thread-Based Concurrent Echo ServerThread-Based Concurrent Server (cont)Issues With Thread-Based ServersPros and Cons of Thread-Based DesignsConcurrent ProgrammingNovember 20, 2003Concurrent ProgrammingNovember 20, 2003TopicsTopicsLimitations of iterative serversProcess-based concurrent serversEvent-based concurrent serversThreads-based concurrent servers15-213“The course that gives CMU its Zip!”class26.ppt– 2 –15-213, F’03Concurrent Programming is Hard!Concurrent Programming is Hard!The human mind tends to be sequentialThe human mind tends to be sequentialThe notion of time is often misleadingThe notion of time is often misleadingThinking about all possible sequences of events in a computer Thinking about all possible sequences of events in a computer system is at least error prone and frequently impossiblesystem is at least error prone and frequently impossibleClassical problem classes of concurrent programs:Classical problem classes of concurrent programs:Races: outcome depends on arbitrary scheduling decisions elsewhere in the systemDeadlock: improper resource allocation prevents forward progressLifelock / Starvation / Fairness: external events and/or system scheduling decisions can prevent sub-task progressMany aspects of concurrent programming are beyond the Many aspects of concurrent programming are beyond the scope of 15-213scope of 15-213– 3 –15-213, F’03Iterative ServersIterative ServersIterative servers process one request at a time.Iterative servers process one request at a time.client 1 server client 2call connectcall acceptret connectret acceptcall connectcall readwriteret readcloseclosecall acceptret connectcall readret readclosewriteret acceptclose– 4 –15-213, F’03Fundamental Flaw of Iterative ServersFundamental Flaw of Iterative ServersSolution: use Solution: use concurrent servers concurrent servers instead.instead.Concurrent servers use multiple concurrent flows to serve multiple clients at the same time.client 1 server client 2call connectcall acceptcall readret connectret acceptcall connectcall fgetsUser goesout to lunchClient 1 blockswaiting for userto type in dataClient 2 blockswaiting to completeits connection request until afterlunch!Server blockswaiting fordata fromClient 1– 5 –15-213, F’03Concurrent Servers:Multiple ProcessesConcurrent Servers:Multiple ProcessesConcurrent servers handle multiple requests concurrently.Concurrent servers handle multiple requests concurrently.client 1 server client 2call connectcall acceptcall readret connectret acceptcall connectcall fgetsforkchild 1User goesout to lunchClient 1 blockswaiting for user to type in datacall acceptret connectret acceptcall fgetswriteforkcall readchild 2writecall readend readcloseclose...– 6 –15-213, F’03Three Basic Mechanisms for Creating Concurrent FlowsThree Basic Mechanisms for Creating Concurrent Flows1. Processes1. ProcessesKernel automatically interleaves multiple logical flows.Each flow has its own private address space.2. I/O multiplexing with 2. I/O multiplexing with select()select()User manually interleaves multiple logical flows.Each flow shares the same address space.Popular for high-performance server designs.3. Threads3. ThreadsKernel automatically interleaves multiple logical flows.Each flow shares the same address space.Hybrid of processes and I/O multiplexing!– 7 –15-213, F’03Process-Based Concurrent ServerProcess-Based Concurrent Server/* * echoserverp.c - A concurrent echo server based on processes * Usage: echoserverp <port> */#include <ics.h>#define BUFSIZE 1024void 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);– 8 –15-213, F’03Process-Based Concurrent Server (cont)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! */ }}– 9 –15-213, F’03Process-Based Concurrent Server(cont)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;}– 10 –15-213, F’03Implementation Issues With Process-Based DesignsImplementation Issues With Process-Based DesignsServer should restart Server should restart acceptaccept call if it is interrupted by call if it is interrupted by a transfer of control to the SIGCHLD handlera transfer of control to the SIGCHLD handlerNot 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 childrenServer must reap zombie childrento avoid fatal memory leak.Server must Server must closeclose its copy of its copy of connfdconnfd..Kernel keeps reference for each socket.After fork,
View Full Document