Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Synchronization

6,576 views

Published on

Published in: Technology
  • Be the first to comment

Synchronization

  1. 1. © 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Synchronization
  2. 2. 2© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? Thread Synchronization Mechanisms Mutex Conditional Variables Read/Write Locks Spin Locks Barriers Semaphores Priority Inversion & its Solutions Understanding a Deadlock Inter Thread Communication
  3. 3. 3© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Recall Thread Management Creation, Termination, Joining, Cleanup Thread-specific Data What about the other data? Shared: By virtue of existence Communication: Inherent No ITC mechanisms required Where is the catch? Concurrency Issues & Race Conditions
  4. 4. 4© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Solutions to Race Conditions Concurrency issues exist within a process Do not need kernel resources to solve them Just some internal synchronization & access protection mechanisms Implemented at pthreads library level Mutex → Critical Sections Condition Variables → Atomic Checks & Actions Read/Write Locks → Readers & Writers Resources Spin Locks → Critical Sections but without yielding Barriers → Serializing Execution Sempahores → Resource Usage
  5. 5. 5© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Mutual Exclusion Type: pthread_mutex_t APIs int pthread_mutex_init(&mutex, &attr); int pthread_mutex_destroy(&mutex); int pthread_mutex_lock(&mutex); int pthread_mutex_unlock(&mutex); int pthread_mutex_trylock(&mutex); Macro PTHREAD_MUTEX_INITIALIZER
  6. 6. 6© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Priority Inversion Mutex Task H Task M Task L Priority Inversion Time
  7. 7. 7© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Priority Inheritance Mutex Priority H Priority L Time Priority M
  8. 8. 8© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Mutual Exclusion Attributes Type: pthread_mutexattr_t APIs pthread_mutexattr_init(&attr); pthread_mutexattr_destroy(&attr); pthread_mutexattr_settype(&attr, type); PTHREAD_MUTEX_RECURSIVE / NORMAL / ERRORCHECK pthread_mutexattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE pthread_mutexattr_setprotocol(&attr, protocol); (Only for RT) PTHREAD_PRIO_NONE, PTHREAD_PRIO_INHERIT, PTHREAD_PRIO_PROTECT pthread_mutexattr_setprioceiling(&attr, proioceiling); (Only for RT) Try out: thread_mutex_attr.c, thread_mutex_error.c
  9. 9. 9© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Deadlock Set of entities waiting for each other Four necessary & sufficient conditions Mutual Exclusion Hold & Wait Non-Preemptive Circular Wait Have a look @ thread_deadlock.c
  10. 10. 10© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Dealing with Deadlock The Ostrich Approach Deadlock Detection & Recovery Deadlock Algorithm: Current Resource Availability → Possible Allocation Sequences Iterative Recovery Algorithm: O(n2) Deadlock Avoidance Banker's Algorithm Deadlock Prevention Eliminate one of the four conditions
  11. 11. 11© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Condition Variables Type: pthread_cond_t APIs int pthread_cond_init(&cond, &attr); int pthread_cond_destroy(&cond); int pthread_cond_wait(&cond, &mutex); int pthread_cond_signal(&cond); int pthread_cond_broadcast(&cond); Macro PTHREAD_COND_INITIALIZER
  12. 12. 12© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Condition Variable Attributes Type: pthread_condattr_t APIs pthread_condattr_init(&attr); pthread_condattr_destroy(&attr); pthread_condattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE Try out: thread_cond.c
  13. 13. 13© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Read / Write Locks Type: pthread_rwlock_t APIs int pthread_rwlock_init(&rwlock, &attr); int pthread_rwlock_destroy(&rwlock); int pthread_rwlock_rdlock(&rwlock); int pthread_rwlock_tryrdlock(&rwlock); int pthread_rwlock_wrlock(&rwlock); int pthread_rwlock_trywrlock(&rwlock); int pthread_rwlock_unlock(&rwlock); Macro PTHREAD_RWLOCK_INITIALIZER
  14. 14. 14© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Read / Write Lock Attributes Type: pthread_rwlockattr_t APIs pthread_rwlockattr_init(&attr); pthread_rwlockattr_destroy(&attr); pthread_rwlockattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE pthread_rwlockattr_setkind_np(&attr, kind); PTHREAD_RWLOCK_PREFER_READER_NP / WRITER_NP / WRITER_NONRECURSIVE_NP Try out: thread_rwlock.c
  15. 15. 15© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Spin Locks Type: pthread_spinlock_t APIs int pthread_spin_init(&spin, pshared); int pthread_spin_destroy(&spin); int pthread_spin_lock(&spin); int pthread_spin_trylock(&spin); int pthread_spin_unlock(&spin); Try out: thread_spinlock.c
  16. 16. 16© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Barriers & its Attributes Type: pthread_barrier_t APIs int pthread_barrier_init(&barrier, &attr, cnt); int pthread_barrier_destroy(&barrier); int pthread_barrier_wait(&barrier); Attribute Type: pthread_barrierattr_t Attribute APIs pthread_barrierattr_init(&attr); pthread_barrierattr_destroy(&attr); pthread_barrierattr_setpshared(&attr, pshared); PTHREAD_PROCESS_SHARED / PRIVATE Try out: thread_barrier.c
  17. 17. 17© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Semaphores Type: sem_t APIs int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_destroy(sem_t *sem); int sem_post(sem_t *sem); int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); Try out: thread_sem.c
  18. 18. 18© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Exchange Offer Thread synchronizing mechanisms Can be used for same ancestor processes by setting there pshared attribute to non-zero Example: Semaphores for Processes Similarly, IPC mechanisms Can be used by threads as well Let's see some examples
  19. 19. 19© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Inter Thread Communication Signals in Threads Pipe for Threads Memory Map Sharing for Threads Examples thread_kill.c thread_ipc.c thread_ipc2.c
  20. 20. 20© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? Thread Synchronization Mechanisms Mutex Conditional Variables Read/Write Locks Spin Locks Barriers Semaphores Priority Inversion & its Solutions Understanding a Deadlock Inter Thread Communication
  21. 21. 21© 2010-17 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?

×