html5-img
1 / 11

Classic Synchronization Problems

Classic Synchronization Problems. Classic Synchronization E xamples. Dining philosophers Readers and writers. The Dining Philosophers Problem. Five philosophers sit at a table A fork lies between every pair of philosophers

Télécharger la présentation

Classic Synchronization Problems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classic Synchronization Problems

  2. Classic Synchronization Examples • Dining philosophers • Readers and writers

  3. The Dining Philosophers Problem • Five philosophers sit at a table • A fork lies between every pair of philosophers • Philosophers (1) think, (2) grab one fork, (3) grab another fork, (4) eat, (5) put down one fork, (6) put the other fork

  4. Dining Philosophers: Try 1 • Assume take_fork and put_fork have locks in them to make them atomic • Is this solution correct? Each philosopher is modeled with a thread #define N 5 Philosopher() { while(TRUE) { Think(); take_fork(i); take_fork((i+1)% N); Eat(); put_fork(i); put_fork((i+1)% N); } }

  5. Dining Philosophers: Try 2 #define N 5 Philosopher() { while(TRUE) { Think(); take_fork(i); take_fork((i+1)% N); Eat(); put_fork(i); put_fork((i+1)% N); } } take_forks(i) put_forks(i)

  6. Take Forks int state[N]; lock mutex; semsem[N] = {0}; // test whether philosopher i // can take both forks // only called with mutex set! test(inti) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){ state[i] = EATING; // Signal Philosopher i V(sem[i]); } } take_forks(inti) { lock(mutex); state[i] = HUNGRY; test(i); unlock(mutex); P(sem[i]); }

  7. Put Forks int state[N]; lock mutex; semsem[N] = {0}; // test whether philosopher i // can take both forks // only called with mutex set! test(inti) { if (state[i] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING){ state[i] = EATING; // Signal Philosopher i V(sem[i]); } } put_forks(inti) { lock(mutex); state[i] = THINKING; test(LEFT); test(RIGHT); unlock(mutex); }

  8. The Readers and Writers Problem • Multiple reader and writer threads want to access some shared data • Multiple readers can read concurrently • Writers must synchronize with readers and other writers • Synchronization requirements • Only one writer can write the shared data at a time • When a writer is writing, no readers must access the data • Goals • Maximize concurrency • Prevent starvation

  9. Readers/Writers - Basics Mutex lock = UNLOCKED; Semaphore data = 1; intrc = 0; Reader () { rc= rc + 1; // Read shared data rc= rc – 1; // non-critical section } Writer () { // non-critical section // Write shared data }

  10. Readers/Writers - Mutex Mutex lock = UNLOCKED; Semaphore data = 1; intrc = 0; Reader () { lock(lock); rc= rc + 1; unlock(lock); // Read shared data lock(lock); rc= rc – 1; unlock(lock); // non-critical section } Writer () { // non-critical section // Write shared data }

  11. Readers/Writers – Synchronization • Any problems with this solution? Mutex lock = UNLOCKED; Semaphore data = 1; intrc = 0; Reader () { lock(lock); if (rc == 0) P(data); rc= rc + 1; unlock(lock); // Read shared data lock(lock); rc= rc – 1; if (rc == 0) V(data); unlock(lock); // non-critical section } Writer () { // non-critical section P(data); // Write shared data V(data); }

More Related