Carnegie Mellon Programming for Concurrency 15 213 18 243 Introduction to Computer Systems 25th Lecture 22 April 2010 Instructors Bill Nace and Gregory Kesden c 1998 2010 All Rights Reserved All work contained herein is copyrighted and used by permission of the authors Contact 15 213 staff cs cmu edu for permission or for more information Carnegie Mello Today Limitations of iterative servers Process based concurrent servers Threads based concurrent servers Event based concurrent servers Carnegie Mello 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 Example who gets the last seat on the airplane Deadlock improper resource allocation prevents forward progress Example traffic gridlock Livelock Starvation Fairness external events and or system scheduling decisions can prevent sub task progress Example people always jump in front of you in line Many aspects are beyond the scope of 15 213 18 243 Carnegie Mello Echo Server Operation Client Server socket socket bind open listenfd open clientfd listen connect Client Server Session Connection request rio writen rio readlineb rio readlineb close accept rio writen EOF rio readlineb close Await connection request from next client Carnegie Mello Iterative Servers client 1 server call connect call accept client 2 call connect ret connect ret accept read call write ret write close close call accept ret accept read close ret connect call write ret write close Carnegie Mello Fundamental Flaw of Iterative Servers client 1 server client 2 call accept call connect ret connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data ret accept Server blocks waiting for data from Client 1 call read call connect Client 2 blocks waiting to complete its connection request until after lunch Carnegie Mello Concurrent Servers Multiple Processes 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 call connect 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 end read close Carnegie Mello Three Basic Mechanisms for Creating Concurrent Flows Carnegie Mello Review Sequential Server int int main int main int argc argc char char argv argv int int listenfd listenfd connfd connfd int int port port atoi argv 1 atoi argv 1 struct sockaddr in struct sockaddr in clientaddr clientaddr int int clientlen clientlen sizeof clientaddr sizeof clientaddr listenfd listenfd Open listenfd port Open listenfd port while while 1 1 connfd connfd accept listenfd accept listenfd SA SA clientaddr clientaddr clientlen clientlen echo connfd echo connfd Close connfd Close connfd exit 0 exit 0 Carnegie Mello Inner Echo Loop void void echo int echo int connfd connfd size t size t n n char buf MAXLINE char buf MAXLINE rio t rio t rio rio Rio readinitb rio Rio readinitb rio connfd connfd while n Rio readlineb rio while n Rio readlineb rio buf buf MAXLINE MAXLINE 0 0 printf server printf server received received d d bytes n bytes n n n Rio writen connfd buf n Rio writen connfd buf n Carnegie Mello Echo Server accept Illustrated listenfd 3 Client Server clientfd Connection request Client listenfd 3 Server clientfd listenfd 3 Client clientfd Server connfd 4 1 Server blocks in accept waiting for connection request on listening descriptor listenfd 2 Client makes connection request by calling and blocking in connect 3 Server returns connfd from accept Client returns from connect Connection is now established between clientfd and connfd Carnegie Mello Today Limitations of iterative servers Process based concurrent servers Threads based concurrent servers Event based concurrent servers Carnegie Mello Process Based Concurrent Server int int main int main int argc argc char char argv argv int int listenfd listenfd connfd connfd int int port port atoi argv 1 atoi argv 1 struct sockaddr in struct sockaddr in clientaddr clientaddr int int clientlen sizeof clientaddr clientlen sizeof clientaddr Signal SIGCHLD Signal SIGCHLD sigchld handler sigchld handler listenfd Open listenfd port listenfd Open listenfd port while while 1 1 connfd connfd Accept listenfd Accept listenfd SA SA clientaddr clientaddr clientlen clientlen ifif Fork Fork 0 0 Close listenfd Close listenfd Child Child closes closes its its listening listening socket socket echo connfd echo connfd Child Child services services client client Close connfd Child closes connection Close connfd Child closes connection with with client client exit 0 exit 0 Child Child exits exits Close connfd Close connfd Parent Parent closes closes connected connected socket socket important important Server forks a separate process to deal with each client No communication between child processes Carnegie Mello Process Based Concurrent Server cont d void void sigchld handler int sigchld handler int sig sig while while waitpid 1 waitpid 1 0 0 WNOHANG WNOHANG 0 0 return return Carnegie Mello Process Execution Model Client 1 data connfd Client Client 11 Server Server Proces Proces ss Client 2 data connfd Connection Requests listenfd Client Client 22 Server Server Proces Proces ss Listenin Listenin gg Server Server Each client handled by independent process No shared state between them When child created each has copy of listenfd and connfd Parent must close connfd child must close listenfd Carnegie Mello Implementation Issues Carnegie Mello 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 1 7 Carnegie Mello Today Limitations of iterative servers Process based concurrent servers Threads based concurrent servers Event based concurrent servers Carnegie Mello Traditional View of a Process Process process context code data and stack Process context Code data and stack Program Program context context Data Data registers registers Condition Condition codes codes
View Full Document