DOC PREVIEW
CMU CS 15213 - Concurrent Servers

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

15 213 The course that gives CMU its Zip Concurrent Servers Topics class29 ppt May 3 2001 Baseline iterative server Process based concurrent server Threads based concurrent server select based concurrent server Error handling sockets wrappers To simplify our code we will use error handling wrappers of the form int Accept int s struct sockaddr addr int addrlen int rc accept s addr addrlen if rc 0 unix error Accept return rc void unix error char msg printf s s n msg strerror errno exit 0 class29 ppt 2 CS 213 S 01 Echo client revisited echoclient c A simple connection based echo client usage echoclient host port include ics h define BUFSIZE 1024 int main int argc char argv int sockfd struct sockaddr in serveraddr struct hostent server char hostname int portno char buf BUFSIZE client socket server socket addr struct server s DNS entry server s domain name server s port number check command line arguments if argc 3 fprintf stderr usage s hostname port n argv 0 exit 0 hostname argv 1 portno atoi argv 2 class29 ppt 3 CS 213 S 01 Echo client cont create the socket sockfd Socket AF INET SOCK STREAM 0 initialize the server s socket address struct server Gethostbyname hostname bzero char serveraddr sizeof serveraddr serveraddr sin family AF INET bcopy char server h addr char serveraddr sin addr s addr server h length serveraddr sin port htons portno request a connection to the server Connect sockfd struct sockaddr serveraddr sizeof serveraddr class29 ppt 4 CS 213 S 01 Echo client cont get a message line from the user printf Please enter msg bzero buf BUFSIZE fgets buf BUFSIZE stdin send message line to server and read its echo Write sockfd buf strlen buf bzero buf BUFSIZE Read sockfd buf BUFSIZE printf Echo from server s buf Close sockfd exit 0 class29 ppt 5 CS 213 S 01 open streamsock helper function int open streamsock int portno int listenfd optval 1 struct sockaddr in serveraddr create a socket descriptor listenfd Socket AF INET SOCK STREAM 0 Setsockopt listenfd SOL SOCKET SO REUSEADDR const void optval sizeof int accept requests to any IP addr portno bzero char serveraddr sizeof serveraddr serveraddr sin family AF INET serveraddr sin addr s addr htonl INADDR ANY serveraddr sin port htons unsigned short portno Bind listenfd struct sockaddr serveraddr sizeof serveraddr Make it a listening socket ready to accept conn requests Listen listenfd 5 return listenfd class29 ppt 6 CS 213 S 01 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 call read call fgets call write call read ret read close call accept ret accept call read ret read call write close class29 ppt 7 ret read call write close call write ret connect call fgets call write call read ret read close CS 213 S 01 Iterative echo server echoserveri c iterative echo server Usage echoserveri port include ics h define BUFSIZE 1024 void echo int connfd int main int argc char argv int listenfd connfd int portno struct sockaddr in clientaddr int clientlen sizeof struct sockaddr in check command line args if argc 2 fprintf stderr usage s port n argv 0 exit 0 portno atoi argv 1 class29 ppt 8 CS 213 S 01 Iterative echo server cont open the listening socket listenfd open streamsock portno main server loop while 1 connfd Accept listenfd struct sockaddr clientaddr clientlen echo connfd Close connfd echo read and echo a line from a client connection void echo int connfd int n char buf BUFSIZE bzero buf BUFSIZE n Read connfd buf BUFSIZE printf server received d bytes s n buf Write connfd buf strlen buf class29 ppt 9 CS 213 S 01 Pros and cons of iterative servers simple can process only one request at a time one slow client can hold up thousands of others Example echo clients and server client 1 call connect ret connect call fgets User goes out to lunch server call accept ret accept Server blocks call read waiting for data from Client 1 Client 1 blocks waiting for user to type in data class29 ppt client 2 10 call connect Client 2 blocks waiting to complete its connection request until after lunch CS 213 S 01 Concurrent servers Concurrent servers process multiple requests concurrently The basic idea is to use multiple control flows to handle multiple requests Example concurrent server designs Fork a new child process for each request Create a new thread for each request Pre fork a pool of child processes to handle requests not discussed Pre create a pool of threads to handle requests not discussed Manually interleave the processing for multiple open connections Uses Linux select function to notice pending socket activity Form of application level concurrency class29 ppt 11 CS 213 S 01 Example Concurrent echo server 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 child 1 fork call accept call read ret accept fork child 2 call read ret connect call fgets write call read write close class29 ppt call connect 12 end read close CS 213 S 01 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 streamsock portno class29 ppt 13 CS 213 S 01 Process based server Signal SIGCHLD handler parent must reap children cont main server loop while 1 for complete portability must restart if interrupted by call to SIGCHLD handler if connfd accept listenfd struct sockaddr clientaddr clientlen 0 if errno EINTR continue go back else unix error accept if Fork 0 Close listenfd child closes its listening socket echo connfd child reads and echos input line Close connfd child is done with this client exit 0 child exits Close connfd parent must close connected socket class29 ppt 14 CS 213 S 01 Reaping zombie children handler reaps children as they terminate void handler int sig pid t pid int stat while pid waitpid 1 stat WNOHANG 0 return Question Why is the call to waitpid in a loop class29 ppt 15 CS 213 S 01 Issues with process based design Server should restart accept call if it is interrupted by a transfer of control to the SIGCHLD handler not necessary for systems such as Linux that support Posix signal handling required for


View Full Document

CMU CS 15213 - Concurrent Servers

Documents in this Course
lecture

lecture

14 pages

lecture

lecture

46 pages

Caches

Caches

9 pages

lecture

lecture

39 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

56 pages

lecture

lecture

11 pages

lecture

lecture

9 pages

Lecture

Lecture

36 pages

Lecture

Lecture

37 pages

Exam

Exam

16 pages

Lecture

Lecture

10 pages

Lecture

Lecture

43 pages

Lecture

Lecture

8 pages

Lecture

Lecture

8 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

37 pages

Lecture

Lecture

6 pages

Lecture

Lecture

40 pages

coding

coding

2 pages

Exam

Exam

17 pages

Exam

Exam

14 pages

Lecture

Lecture

29 pages

Lecture

Lecture

34 pages

Exam

Exam

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

lecture

lecture

46 pages

Lecture

Lecture

33 pages

Lecture

Lecture

57 pages

Lecture

Lecture

32 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

11 pages

Lecture

Lecture

6 pages

Lecture

Lecture

43 pages

Lecture

Lecture

12 pages

Lecture

Lecture

18 pages

Exam

Exam

10 pages

Lecture

Lecture

45 pages

Lecture

Lecture

37 pages

Exam

Exam

24 pages

class09

class09

21 pages

class22

class22

37 pages

class20

class20

30 pages

class27

class27

33 pages

class25

class25

21 pages

class04

class04

31 pages

Lecture

Lecture

59 pages

class01a

class01a

14 pages

class12

class12

45 pages

class29

class29

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

6 pages

class03

class03

34 pages

lecture

lecture

42 pages

Lecture

Lecture

40 pages

Lecture

Lecture

47 pages

Exam

Exam

19 pages

R06-B

R06-B

25 pages

class17

class17

37 pages

class25

class25

31 pages

Lecture

Lecture

15 pages

final-f06

final-f06

17 pages

Lecture

Lecture

9 pages

lecture

lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

22 pages

class11

class11

45 pages

lecture

lecture

50 pages

Linking

Linking

37 pages

Lecture

Lecture

64 pages

Integers

Integers

40 pages

Exam

Exam

11 pages

Lecture

Lecture

37 pages

Lecture

Lecture

44 pages

Lecture

Lecture

37 pages

Lecture

Lecture

9 pages

Lecture

Lecture

37 pages

Lecture

Lecture

45 pages

Final

Final

25 pages

lecture

lecture

9 pages

Lecture

Lecture

30 pages

Lecture

Lecture

16 pages

Final

Final

17 pages

Lecture

Lecture

8 pages

Exam

Exam

11 pages

Lecture

Lecture

47 pages

Lecture

Lecture

9 pages

lecture

lecture

39 pages

Exam

Exam

11 pages

lecture

lecture

41 pages

lecture

lecture

37 pages

Lecture

Lecture

59 pages

Lecture

Lecture

45 pages

Exam 1

Exam 1

18 pages

Lecture

Lecture

41 pages

Lecture

Lecture

32 pages

Lecture

Lecture

30 pages

Lecture

Lecture

9 pages

Lecture

Lecture

9 pages

Lecture

Lecture

15 pages

Lecture

Lecture

11 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

4 pages

Lecture

Lecture

46 pages

Lecture

Lecture

8 pages

Lecture

Lecture

65 pages

Lecture

Lecture

38 pages

Lecture

Lecture

35 pages

Lecture

Lecture

8 pages

Lecture

Lecture

34 pages

Lecture

Lecture

8 pages

Exam

Exam

13 pages

Lecture

Lecture

43 pages

Lecture

Lecture

9 pages

Lecture

Lecture

12 pages

Lecture

Lecture

9 pages

Lecture

Lecture

34 pages

Lecture

Lecture

43 pages

Lecture

Lecture

7 pages

Lecture

Lecture

45 pages

Lecture

Lecture

24 pages

Lecture

Lecture

47 pages

Lecture

Lecture

12 pages

Lecture

Lecture

20 pages

Lecture

Lecture

9 pages

Exam

Exam

11 pages

Lecture

Lecture

52 pages

Lecture

Lecture

20 pages

Exam

Exam

11 pages

Lecture

Lecture

35 pages

Lecture

Lecture

47 pages

Lecture

Lecture

18 pages

Lecture

Lecture

30 pages

Lecture

Lecture

59 pages

Lecture

Lecture

37 pages

Lecture

Lecture

22 pages

Lecture

Lecture

35 pages

Exam

Exam

23 pages

Lecture

Lecture

9 pages

Lecture

Lecture

22 pages

class12

class12

32 pages

Lecture

Lecture

8 pages

Lecture

Lecture

39 pages

Lecture

Lecture

44 pages

Lecture

Lecture

38 pages

Lecture

Lecture

69 pages

Lecture

Lecture

41 pages

Lecture

Lecture

12 pages

Lecture

Lecture

52 pages

Lecture

Lecture

59 pages

Lecture

Lecture

39 pages

Lecture

Lecture

83 pages

Lecture

Lecture

59 pages

class01b

class01b

17 pages

Exam

Exam

21 pages

class07

class07

47 pages

Lecture

Lecture

11 pages

Odyssey

Odyssey

18 pages

multicore

multicore

66 pages

Lecture

Lecture

6 pages

lecture

lecture

41 pages

lecture

lecture

55 pages

lecture

lecture

52 pages

lecture

lecture

33 pages

lecture

lecture

46 pages

lecture

lecture

55 pages

lecture

lecture

17 pages

lecture

lecture

49 pages

Exam

Exam

17 pages

lecture

lecture

56 pages

Exam 2

Exam 2

16 pages

Exam 2

Exam 2

16 pages

Notes

Notes

37 pages

Lecture

Lecture

40 pages

Lecture

Lecture

36 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Exam

Exam

13 pages

Lecture

Lecture

32 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

29 pages

Lecture

Lecture

59 pages

Lecture

Lecture

41 pages

Lecture

Lecture

50 pages

Exam

Exam

17 pages

Lecture

Lecture

29 pages

Lecture

Lecture

44 pages

Lecture

Lecture

41 pages

Lecture

Lecture

52 pages

Lecture

Lecture

40 pages

Lecture

Lecture

33 pages

lecture

lecture

10 pages

Lecture

Lecture

27 pages

Lecture

Lecture

29 pages

Lecture

Lecture

39 pages

Lecture

Lecture

9 pages

Lecture

Lecture

29 pages

Lecture

Lecture

8 pages

Lecture

Lecture

43 pages

Lecture

Lecture

43 pages

Lecture

Lecture

75 pages

Lecture

Lecture

55 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lecture

Lecture

35 pages

lecture

lecture

36 pages

Exam

Exam

33 pages

lecture

lecture

56 pages

lecture

lecture

64 pages

lecture

lecture

8 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

36 pages

lecture

lecture

56 pages

lecture

lecture

75 pages

lecture

lecture

36 pages

Lecture

Lecture

50 pages

Lecture

Lecture

45 pages

Lecture

Lecture

13 pages

Exam

Exam

23 pages

Lecture

Lecture

10 pages

Lecture

Lecture

48 pages

Lecture

Lecture

83 pages

lecture

lecture

57 pages

Lecture

Lecture

33 pages

Lecture

Lecture

39 pages

Lecture

Lecture

33 pages

lecture

lecture

54 pages

Lecture

Lecture

30 pages

Exam

Exam

13 pages

Lecture

Lecture

36 pages

Lecture

Lecture

40 pages

Exam

Exam

17 pages

Lecture

Lecture

9 pages

Exam

Exam

15 pages

Lecture

Lecture

44 pages

Lecture

Lecture

34 pages

Lecture

Lecture

24 pages

Lecture

Lecture

29 pages

class12

class12

43 pages

lecture

lecture

43 pages

class22

class22

22 pages

R06-B

R06-B

25 pages

class01b

class01b

19 pages

lecture

lecture

29 pages

lab1

lab1

8 pages

Caches

Caches

36 pages

lecture

lecture

55 pages

Lecture,

Lecture,

37 pages

Integers

Integers

40 pages

Linking

Linking

38 pages

lecture

lecture

45 pages

Lecture

Lecture

61 pages

Linking

Linking

33 pages

lecture

lecture

40 pages

lecture

lecture

40 pages

Lecture

Lecture

32 pages

lecture

lecture

48 pages

lecture

lecture

44 pages

Exam

Exam

11 pages

Lecture

Lecture

31 pages

Lecture

Lecture

46 pages

Lecture

Lecture

40 pages

Lecture

Lecture

40 pages

Exam

Exam

12 pages

Lecture

Lecture

42 pages

Lecture

Lecture

36 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

Lecture

Lecture

13 pages

Lecture

Lecture

35 pages

Lecture

Lecture

20 pages

Final

Final

19 pages

Lecture

Lecture

33 pages

Lecture

Lecture

50 pages

Lecture

Lecture

33 pages

Lecture

Lecture

27 pages

Lecture

Lecture

6 pages

Exam

Exam

15 pages

Lecture

Lecture

24 pages

Lecture

Lecture

23 pages

Lecture

Lecture

43 pages

Lecture

Lecture

32 pages

Lecture

Lecture

52 pages

Lecture

Lecture

37 pages

Lecture

Lecture

36 pages

Lecture

Lecture

34 pages

Lecture

Lecture

40 pages

Lecture

Lecture

15 pages

lecture

lecture

21 pages

Lecture

Lecture

58 pages

Lecture

Lecture

49 pages

Lecture

Lecture

36 pages

Lecture

Lecture

11 pages

Lecture

Lecture

12 pages

Lecture

Lecture

58 pages

Lecture

Lecture

33 pages

Exam

Exam

15 pages

Lecture

Lecture

35 pages

Lecture

Lecture

10 pages

Lecture

Lecture

25 pages

Lecture

Lecture

31 pages

Lecture

Lecture

24 pages

Lecture

Lecture

34 pages

Lecture

Lecture

50 pages

lecture

lecture

35 pages

Lecture

Lecture

11 pages

Lecture

Lecture

39 pages

Lecture

Lecture

45 pages

Lecture

Lecture

41 pages

exam1-f05

exam1-f05

11 pages

Lecture

Lecture

4 pages

Lecture

Lecture

17 pages

Exam

Exam

17 pages

malloc()

malloc()

12 pages

Lecture

Lecture

57 pages

Lecture

Lecture

30 pages

Lecture

Lecture

30 pages

Lecture

Lecture

47 pages

Lecture

Lecture

33 pages

Exam

Exam

12 pages

Lecture

Lecture

43 pages

Lectures

Lectures

33 pages

Lecture

Lecture

36 pages

lecture

lecture

33 pages

Exam

Exam

14 pages

Lecture

Lecture

43 pages

Lecture

Lecture

25 pages

Load more
Download Concurrent Servers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Concurrent Servers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Concurrent Servers and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?