{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture5

# Lecture5 - Synchronization:CriticalSections&Semaphores...

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

Synchronization: Critical Sections & Semaphores Spring 2011 Dr. Ronnie Ward

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

View Full Document
Critical Sections and Semaphores Chapter 14 in textbook Some slides adopted and modified from Dr. Bettati.
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

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

View Full Document
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
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 !

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

View Full Document
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;
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 .

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

View Full Document
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.
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!

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

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

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern