DOC PREVIEW
CMU CS 15213 - lecture

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

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

Unformatted text preview:

Carnegie Mellon Synchroniza on Advanced 15 213 Introduc0on to Computer Systems 24th Lecture Nov 18 2010 Instructors Randy Bryant and Dave O Hallaron 1 Carnegie Mellon Today Producer consumer problem Readers writers problem Thread safety Races Deadlocks 2 Carnegie Mellon Using Semaphores to Schedule Access to Shared Resources Basic idea Thread uses a semaphore opera on to no fy another thread that some condi on has become true Use coun0ng semaphores to keep track of resource state Use binary semaphores to no0fy other threads Two classic examples The Producer Consumer Problem The Readers Writers Problem 3 Carnegie Mellon Producer Consumer Problem producer thread shared bu er consumer thread Common synchroniza on paGern Producer waits for empty slot inserts item in bu er and no0 es consumer Consumer waits for item removes it from bu er and no0 es producer Examples Mul0media processing Producer creates MPEG video frames consumer renders them Event driven graphical user interfaces Producer detects mouse clicks mouse movements and keyboard hits and inserts corresponding events in bu er Consumer retrieves events from bu er and paints the display 4 Carnegie Mellon Producer Consumer on 1 element Bu er include csapp h define NITERS 5 int main pthread t tid producer pthread t tid consumer Initialize the semaphores Sem init shared empty 0 1 Sem init shared full 0 0 void producer void arg void consumer void arg struct int buf shared var sem t full sems sem t empty shared Create threads and wait Pthread create tid producer NULL producer NULL Pthread create tid consumer NULL consumer NULL Pthread join tid producer NULL Pthread join tid consumer NULL exit 0 5 Carnegie Mellon Producer Consumer on 1 element Bu er Ini ally empty 1 full 0 Producer Thread void producer void arg int i item Consumer Thread void consumer void arg int i item for i 0 i NITERS i Read item from buf P shared full item shared buf V shared empty for i 0 i NITERS i Produce item item i printf produced d n item Consume item printf consumed d n item Write item to buf P shared empty shared buf item V shared full return NULL return NULL 6 Carnegie Mellon Producer Consumer on an n element Bu er Requires a mutex and two coun ng semaphores mutex enforces mutually exclusive access to the the bu er slots counts the available slots in the bu er items counts the available items in the bu er Implemented using a shared bu er package called sbuf 7 Carnegie Mellon sbuf Package Declara ons include csapp h typedef struct int buf int n int front int rear sem t mutex sem t slots sem t items sbuf t Buffer array Maximum number of slots buf front 1 n is first item buf rear n is last item Protects accesses to buf Counts available slots Counts available items void sbuf init sbuf t sp int n void sbuf deinit sbuf t sp void sbuf insert sbuf t sp int item int sbuf remove sbuf t sp sbuf h 8 Carnegie Mellon sbuf Package Implementa on Ini alizing and deini alizing a shared bu er Create an empty bounded shared FIFO buffer with n slots void sbuf init sbuf t sp int n sp buf Calloc n sizeof int sp n n Buffer holds max of n items sp front sp rear 0 Empty buffer iff front rear Sem init sp mutex 0 1 Binary semaphore for locking Sem init sp slots 0 n Initially buf has n empty slots Sem init sp items 0 0 Initially buf has zero items Clean up buffer sp void sbuf deinit sbuf t sp Free sp buf sbuf c 9 Carnegie Mellon sbuf Package Implementa on Inser ng an item into a shared bu er Insert item onto the rear of shared buffer sp void sbuf insert sbuf t sp int item P sp slots Wait for available slot P sp mutex Lock the buffer sp buf sp rear sp n item Insert the item V sp mutex Unlock the buffer V sp items Announce available item sbuf c 10 Carnegie Mellon sbuf Package Implementa on Removing an item from a shared bu er Remove and return the first item from buffer sp int sbuf remove sbuf t sp int item P sp items P sp mutex item sp buf sp front sp n V sp mutex V sp slots return item Wait for available item Lock the buffer Remove the item Unlock the buffer Announce available slot sbuf c 11 Carnegie Mellon Today Producer consumer problem Readers writers problem Thread safety Races Deadlocks 12 Carnegie Mellon Readers Writers Problem Generaliza on of the mutual exclusion problem Problem statement Reader threads only read the object Writer threads modify the object Writers must have exclusive access to the object Unlimited number of readers can access the object Occurs frequently in real systems e g Online airline reserva0on system Mul0threaded caching Web proxy 13 Carnegie Mellon Variants of Readers Writers First readers writers problem favors readers No reader should be kept wai0ng unless a writer has already been granted permission to use the object A reader that arrives a er a wai0ng writer gets priority over the writer Second readers writers problem favors writers Once a writer is ready to write it performs its write as soon as possible A reader that arrives a er a writer must wait even if the writer is also wai0ng Starva5on where a thread waits inde nitely is possible in both cases 14 Carnegie Mellon Solu on to First Readers Writers Problem Readers int readcnt Initially 0 sem t mutex w Both initially 1 void reader void while 1 P mutex readcnt if readcnt 1 First in P w V mutex Reading happens here Writers void writer void while 1 P w Writing here V w rw1 c P mutex readcnt if readcnt 0 Last out V w V mutex 15 Carnegie Mellon Case Study Prethreaded Concurrent Server Pool of worker threads Service client Client Master thread Bu er Remove descriptors Client Service client Accept connec5ons Insert descriptors Worker thread Worker thread 16 Carnegie Mellon Prethreaded Concurrent Server sbuf t sbuf Shared buffer of connected descriptors int main int argc char argv int i listenfd connfd port socklen t clientlen sizeof struct sockaddr in struct sockaddr in clientaddr pthread t tid port atoi argv 1 sbuf init sbuf SBUFSIZE listenfd Open listenfd port for i 0 i NTHREADS i Create worker threads Pthread create tid NULL thread NULL while 1 connfd Accept listenfd SA clientaddr clientlen sbuf insert sbuf connfd Insert connfd in buffer echoservert pre c 17 Carnegie Mellon Prethreaded Concurrent Server Worker thread rou ne void thread void vargp Pthread detach pthread self while 1 int connfd sbuf remove sbuf Remove connfd from buffer echo cnt connfd Service client Close connfd echoservert pre c 18 Carnegie Mellon Prethreaded Concurrent Server echo cnt ini aliza on rou ne static int byte cnt


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

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