sync - Dante Cannarozzi Washington University...

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: Dante Cannarozzi Washington University djc2@cse.wustl.edu www.cse.wustl.edu/~djc2 Synchronization (CSE 422S) 2-Ken Wong, Oct 2008 The Shared Data Problem (1) Machine Code has 20 = 6!/(3!3!) executions and 2 possible values of x (=2 and =3) Consider the following shared memory code: Shared (Global) int x = 1; CPU 0 CPU 1 x = x + 1; x = x + 1; Shared (Global) int x = 1; CPU 0 CPU 1 1) Load R1,x 1) Load R1,x 2) Inc R1 2) Inc R1 3) Store R1,x 3) Store R1,x Each CPU has its own registers 3-Ken Wong, Oct 2008 The Shared Data Problem (2) SP PC Registers CPU 1 SP PC Registers CPU 0 Main Memory x = 1 Bus 4-Ken Wong, Oct 2008 The Shared Data Problem (3) The problem exists even if there is only 1 CPU! Shared (Global) int x = 1; CPU 0 CPU 1 1) Load R1,x <<<<< Interrupt; Context Switch >>>>> 1) Load R1,x 2) Inc R1 3) Store R1,x <<<<< Interrupt; Context Switch >>>>> 2) Inc R1 3) Store R1,x 5-Ken Wong, Oct 2008 Synchronization/Mutual Exclusion There is a race condition between two threads Because the outcome depends on the relative execution times of each thread Synchronize threads to provide mutual exclusion Coordinate execution schedules of threads one thread should finish updating shared variables before another thread updates same shared variables Critical Section A code segment requiring synchronization Need Entry/Exit Sections [ Entry Section ] . . . Critical Section . . . [ Exit Section ] These guard the critical section 6-Ken Wong, Oct 2008 Shared Ring Buffer Problem Shared Data Get Put M Consumers M Producers M = M = 1 out in N-1 int buffer[N]; // N integer buffers int in, out; in = 0; // index to next input buffer out = 0; // index to next output buffer count = 0; // number of buffers in use 7-Ken Wong, Oct 2008 Ring Buffer Producer/Consumer do { // Producer ... compute newItem ... while (count == N) { ... do nothing ... } buffer[in] = newItem; in = mod (in+1, N); count = count + 1; } do { // Consumer while (count == 0) { ... do nothing ... } outItem = buffer[out]; out = mod (out+1, N); count = count - 1; ... use outItem ... } Is this program correct? Only if M=1 and arithmetic operations on 'count' are atomic 8-Ken Wong, Oct 2008 Requirements For Mutual Exclusion Enforcement Enforce mutual exclusion for critical sections sharing same objects Isolation A process that halts outside all critical sections should not interfere with other processes Bounded Waiting No deadlock or starvation Progress When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay Delay Insensitive Make no assumptions about relative process speeds Finite Blocking A process remains in its critical section for only a finite time 9-Ken Wong, Oct 2008 Software Approaches 1 and 2 Can provide mutual exclusion?...
View Full Document

Page1 / 29

sync - Dante Cannarozzi Washington University...

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