6_sync - 1 Synchronization& Deadlock Chapter 6& 7 2...

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 Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 1 Synchronization & Deadlock Chapter 6 & 7 2 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Shared-memory solution to bounded-buffer problem (Chapter 4) has a race condition 3 Race Condition while (1) { while (count == BUFFER_SIZE) ; // do nothing // produce an item and put in next buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } while (1) { while (count == 0) ; // do nothing next = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; // consume the item in next } 4 Race Condition count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Consider this execution: initially, count = 5 Producer: Consumer: Values: register1 = count {register1 = 5} register1 = register1 + 1 {register1 = 6} register2 = count {register2 = 5} register2 = register2 – 1 {register2 = 4} count = register1 {count = 6} count = register2 {count = 4} 5 Why can such interleaving (race) exist? Operating system scheduling is non-deterministic Context switch can happen at any arbitrary instructions In the previous example, any permutation of interleaving for the producer/consumer is possible Correct algorithm should not rely on the order to which threads of execution are scheduled 6 Example: Shared Linked List (1) n->next = p->next (2) p->next = n Order: a b 1 2 (a) m->next = p->next (b) p->next = m p n m X X 7 Example: Shared Linked List (1) n->next = p->next (2) p->next = n Order: a 1 2 b (a) m->next = p->next (b) p->next = m p n m X X 8 Critical Section What is a critical section? Each thread has a segment of code, in which the thread may be changing common variables, updating shared data structures … Code that must be executed mutually exclusively Contains a race condition What is a race condition? Outcome depends on order threads execute Different outcomes are possible Necessary conditions Concurrency Shared data Interference 9 Solution to Critical-Section Problem 1. Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the...
View Full Document

{[ snackBarMessage ]}

Page1 / 76

6_sync - 1 Synchronization& Deadlock Chapter 6& 7 2...

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