{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture5 - Synchronization:CriticalSections&Semaphores...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Synchronization: Critical Sections & Semaphores Spring 2011 Dr. Ronnie Ward
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Critical Sections and Semaphores Chapter 14 in textbook Some slides adopted and modified from Dr. Bettati.
Image of page 2
Synchronization: Critical Sections & Semaphores Why?    Examples What?   The Critical Section Problem How?   Software solutions   Hardware-supported solutions The basic synchronization mechanism: Semaphores Classical synchronization problems Reading: R&R, Ch 14
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Synchronization: Critical Sections & Semaphores Why?    Examples What?   The Critical Section Problem How?   Software solutions   Hardware-supported solutions The basic synchronization mechanism: Semaphores Classical synchronization problems
Image of page 4
The Critical Section Problem: Example 1 char in; /* shared variables */ char out; void echo () { input (in, keyboard); out := in; output (out, display); } Process 1 Process 2 Operation: Echo() Echo() Interleaved execution input(in,keyboard) out = in; output(out,display) input(in,keyboard); out = in; output(out,display); Race condition !
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Critical Section Problem: Example 2 Producer-consumer with bounded, shared-memory, buffer. Consumer: Item * remove () { while (counter == 0) no_op ; next = buffer[out]; out = (out+1) MOD n; counter = counter - 1; return next; } out in Producer: void deposit (Item * next) { while (counter == n) no_op ; buffer[in] = next; in = (in+1) MOD n; counter = counter + 1; } circular buffer  of size n int in, out; Item buffer[n]; int counter;
Image of page 6
This Implementation is not Correct! Producer counter = counter + 1 reg 1 = counter reg 1 = reg 1 + 1 counter = reg 1 reg 1 = counter reg 1 = reg 1 + 1 counter = reg 1 Consumer counter = counter - 1 reg 2 = counter reg 2 = reg 2 - 1 counter = reg 2 reg 2 = counter reg 2 = reg 2 - 1 counter = reg 2 operation: on CPU: interleaved execution: Race condition! Need to ensure that only one process can manipulate variable  counter  at a time :  synchronization .
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev prev Critical Section Problem: Example 3 Insertion of an element into a list. void insert (new, curr) { /*1*/ new.next = curr.next; /*2*/ new.prev = curr; /*3*/ curr.next = new; /*4*/ new.next.prev = new; } next next next next next next new curr next next next next next next new curr next next next next next next new curr next next next next next next new curr next next next next next next new curr 1. 2. 3. 4.
Image of page 8
Interleaved Execution causes Errors! new1.next = curr.next; new1.prev = curr; curr.next = new1; New1.next.prev = new1; Process 1 new2.next = curr.next; new2.prev = curr; curr.next = new2; new2.next.prev = new2; Process 2 prev prev prev prev prev prev next next next next next next new1 curr prev prev next next new2   Must guarantee   mutually exclusive access  to the data structure!
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Synchronization: Critical Sections & Semaphores Why?    Examples What?   The Critical Section Problem
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern