1 / 12

Synchronization

Synchronization. Some of these slides were originally made by Dr. Roger deBry . They include text, figures, and information from this class’s textbook, Operating Systems Concepts, by Silberschatz , Galvin, and Gagne. They also include material from the slides that accompany that book.

meli
Télécharger la présentation

Synchronization

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. Synchronization Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating Systems Concepts, by Silberschatz, Galvin, and Gagne. They also include material from the slides that accompany that book.

  2. Race Condition balance starts at 1000 When the result of executing a program depends on which thread runs precisely when. Thread 1 Thread 2 b = getBalance(“1234”); b -= 100; setBalance(“1234”, b); b = getBalance(“1234”); b += 500; setBalance(“1234”, b); What should the ending balance be? What will it be? Will getting rid of the local variable b solve the problem? How can we solve this problem?

  3. Critical sections The part of a program where a shared resource is accessed is called a critical section.

  4. Critical sections do { entry section critical section exit section remainder section } while (true);

  5. Avoiding race conditions • The following four conditions must hold to avoid a race condition: • Mutual exclusion: No two processes may be simultaneously inside their critical sections • No assumptions may be made about speeds, scheduling, or the number of CPUs • No process running outside of its critical section may block another process • No process should have to wait forever to enter its critical section.

  6. Peterson’s solution Entry section do { flag[i] = true; turn = j; while (flag[j] && turn == j); critical section flag[i] = false; remainder section } while (true); Busy waiting Exit section

  7. Synchronization hardware:Test and Set Lock (TSL) instruction Stores current value of lock in r1 and a non-zero value in the lock as an atomic instruction. enter_section: tsl r1, lock cmp r1, #0 jneenter_section ret If the original value of the lock was not zero, jump back to beginning of the loop. If the original value of the lock was zero, enter critical section leave_section: move lock, #0 ret

  8. Mutexes Entry section do { acquire lock critical section release lock remainder section } while (true); Exit section acquire() { while (!available); available = false; } release() { available = true; }

  9. Semaphores Integer variable accessed only through two atomic operations: wait and signal do { wait critical section signal remainder section } while (true); wait(S) { while (S <= 0); S--; } signal(S) { S++; } Binary and counting semaphores

  10. Monitors ● The highest level synchronization mechanism. ● Mutual exclusion: Only one thread at a time can execute an operation.

  11. Pthreadmutexes pthread_mutex_tmutex; pthread_mutex_init(&mutex, NULL); pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex);

  12. Pthread condition variables pthread_cond_t empty; pthread_cond_init(&empty, NULL); pthread_cond_wait(&empty, &mutex); pthread_cond_signal(&empty);

More Related