1 / 13

Background on the need for Synchronization

Background on the need for Synchronization. Threads may need to wait for other threads to finish an operation (i.e. Waitforallthreads () from lab) Additionally concurrent access to shared data with threads may result in data inconsistency (i.e., incorrect values)

agalia
Download Presentation

Background on the need for 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. Background on the need for Synchronization • Threads may need to wait for other threads to finish an operation (i.e. Waitforallthreads() from lab) • Additionally concurrent access to shared data with threads may result in data inconsistency (i.e., incorrect values) • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes (or threads)

  2. Example Problem • Suppose two threads share a common buffer array. The producer put items in the buffer and the consumer removes them. • A solution to a two thread consumer-producer problem that fills all the buffer space has an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.

  3. Producer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }

  4. Consumer while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed }

  5. Critical Section • The code segments that read and write global shared data between threads or processes is called a “critical section” • Possible race condition on global variable values – example will follow • OS Synchronization API used to solve this • Must be careful and use OS synchronization primitives to control access to a critical section or hidden bugs will appear in code

  6. Race Condition on Count • count++ could be implemented asregister1 = count register1 = register1 + 1 count = register1 • count-- could be implemented asregister2 = count register2 = register2 - 1 count = register2 • Consider this execution interleaving with “count = 5” initially: S0: producer execute register1 = count {register1 = 5}S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}

  7. Need an Atomic Operation • Count++ and Count-- code runs to end before switching to other thread • Atomic operation here means a basic operation which cannot be stopped or interrupted in the middle to switch to another thread • Race conditions will occur faster on systems with multiple processors since threads are running in parallel

  8. Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi 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 selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted • Assume that each process executes at a nonzero speed • No assumption concerning relative speed of the N processes

  9. Solution to Critical-section Problem Using Mutex Locks do { acquire lock critical section release lock remainder section } while (TRUE);

  10. Semaphore Synchronization tool that does not require busy waiting (i.e., no while loops using processor time and power) Semaphore S – integer variable Two standard operations modify S: wait() and signal() Originally called P() andV() Less complicated Can only be accessed via two indivisible (atomic) operations • wait (S) { while S <= 0 ; // no-op S--; } • signal (S) { S++; }

  11. Deadlock and Starvation • Deadlock – two or more processes or threads are waiting indefinitely for an event that can be caused by only one of the waiting processes • Let S and Q be two semaphores initialized to 1 (i.e. a mutual exclusion lock) P0P1 wait (S); wait (Q); wait (Q); wait (S); . . . . . . signal (S); signal (Q); signal (Q); signal (S); • Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended • Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process. Might need to run lower –priority process first to continue. – messes up priority on processes

  12. Barriers for Thread Synchronization Barriers allow defining synchronization points used to coordinate the execution of a team of threads. When a thread reaches a synchronization point, its execution is stopped until all other threads in the team reach the synchronization point. Basic Barrier A simple barrier is implemented using an atomic shared counter. The counter is incremented by each thread after entering the barrier. Threads wait at the barrier until the counter becomes equal to the number of threads. This kind of barrier cannot be reused, because the counter is never reset safely. Reusing the barrier, thought resetting the counter, results in possible starvation, because storing 0 into the counter will mask the old value. If a thread is suspended during the resetting phase, it will never leave the barrier.

  13. Sense Reversing Barrier Adding a sense flag allows reuse of a barrier many times. The barrier counter is used to keep track of how many threads have reached the barrier, but the waiting phase is performed by spinning on a sense flag. Threads wait until the barrier sense flag matches the thread-private sense flag. The last thread reaching the barrier resets both the counter and the barrier sense flag, while each thread must reset its local sense flag before exiting the barrier. The sense flag allows the discrimination between odd and even barrier phases. Resetting the counter is not an unsafe operation because it does not interfere with the barrier waiting variable, represented by the sense flag.

More Related