120 likes | 347 Views
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.
E N D
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.
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?
Critical sections The part of a program where a shared resource is accessed is called a critical section.
Critical sections do { entry section critical section exit section remainder section } while (true);
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.
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
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
Mutexes Entry section do { acquire lock critical section release lock remainder section } while (true); Exit section acquire() { while (!available); available = false; } release() { available = true; }
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
Monitors ● The highest level synchronization mechanism. ● Mutual exclusion: Only one thread at a time can execute an operation.
Pthreadmutexes pthread_mutex_tmutex; pthread_mutex_init(&mutex, NULL); pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex);
Pthread condition variables pthread_cond_t empty; pthread_cond_init(&empty, NULL); pthread_cond_wait(&empty, &mutex); pthread_cond_signal(&empty);