DOC PREVIEW
CMU CS 15213 - Concurrent Programming

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

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

Unformatted text preview:

15 213 The course that gives CMU its Zip Concurrent Programming November 20 2003 Topics class26 ppt Limitations of iterative servers Process based concurrent servers Event based concurrent servers Threads based concurrent servers 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 Deadlock improper resource allocation prevents forward progress Lifelock Starvation Fairness external events and or system scheduling decisions can prevent sub task progress Many aspects of concurrent programming are beyond the scope of 15 213 2 15 213 F 03 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 write call read ret read close close call accept ret accept write close 3 ret connect call read ret read close 15 213 F 03 Fundamental Flaw of Iterative Servers client 1 call connect ret connect call fgets User goes out to lunch server client 2 call accept ret accept Server blocks call read waiting for data from Client 1 Client 1 blocks waiting for user to type in data call connect Client 2 blocks waiting to complete its connection request until after lunch Solution use concurrent servers instead 4 Concurrent servers use multiple concurrent flows to serve multiple clients at the same time 15 213 F 03 Concurrent Servers Multiple Processes Concurrent servers handle multiple requests concurrently 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 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 5 call connect end read close 15 213 F 03 Three Basic Mechanisms for Creating Concurrent Flows 1 Processes Kernel automatically interleaves multiple logical flows Each flow has its own private address space 2 I O multiplexing with select User manually interleaves multiple logical flows Each flow shares the same address space Popular for high performance server designs 3 Threads 6 Kernel automatically interleaves multiple logical flows Each flow shares the same address space Hybrid of processes and I O multiplexing 15 213 F 03 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 listenfd portno 7 15 213 F 03 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 8 15 213 F 03 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 9 15 213 F 03 Implementation Issues With Process Based Designs Server should restart accept call if it is interrupted by a transfer of control to the SIGCHLD handler Not 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 children to avoid fatal memory leak Server must close its copy of connfd 10 Kernel keeps reference for each socket After fork refcnt connfd 2 Connection will not be closed until refcnt connfd 0 15 213 F 03 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 I O multiplexing provides more control with less overhead 11 15 213 F 03 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 12 15 213 F 03 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 13 15 213 F 03 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 14 Is bit fd in fdset turned on 15 213 F 03 select Example main loop wait for connection request or stdin command If connection request then echo input line and close connection If stdin command then process printf server fflush stdout while notdone select check if the user typed something to stdin or if a connection request arrived FD ZERO readfds initialize the fd set FD SET listenfd readfds add socket fd FD SET 0 readfds add stdin fd 0 Select listenfd 1 readfds NULL NULL NULL 15 15 213 F 03 select Example cont First we check for a pending event on stdin if the user has typed a command process it if FD ISSET 0 readfds fgets buf BUFSIZE stdin switch buf 0 case c print the connection count


View Full Document

CMU CS 15213 - Concurrent Programming

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