DOC PREVIEW
CMU CS 15213 - Lecture

This preview shows page 1-2 out of 6 pages.

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

Unformatted text preview:

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

CMU CS 15213 - Lecture

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

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 Lecture
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 Lecture 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 Lecture 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?