Lecture5 - Synchronization: Critical Sections &...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Synchronization: Critical Sections & Semaphores Spring 2011 Dr. Ronnie Ward 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 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 ! 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 . 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!...
View Full Document

This note was uploaded on 02/14/2012 for the course CSCE 313 taught by Professor Gu during the Spring '11 term at Texas A&M.

Page1 / 40

Lecture5 - Synchronization: Critical Sections &...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online