Lab5-sync1 - Lab 5 Synchronization Mutex and Condition...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab 5 Synchronization Mutex and Condition Variables 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Before You Start Login to Linux Boot the PC, and select the option: SLIM/SWIM Choose 5 Ubuntu 10.04 Login the system using CS account info Download lab5.tgz from course webpage to your desktop Then, in a terminal cd ~/Desktop tar xzvf lab5.tgz cd lab5 make When you see Try these command(s) in your terminal TRY!
Image of page 2
Mutex Variables Mutex is an abbreviation for “mutual exclusion” Mutexes can be used to prevent race conditions Implement thread synchronization and protect shared data to avoid data inconsistency A mutex variable acts like a "lock" protecting access to a shared data resource For example, Thread t1 locks a mutex variable mv Another thread t2 wants to lock mv, but fails and block waiting there Thread t1 unlocks mv Thread t2 is unblocked and can lock the mv now.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Process Mutex program structure Steps Define Mutex Initial Mutex …… LockMutex …… UnlockMutex …… DestroyMutex Mutex In threads Thread 1 Thread 2 in critical section
Image of page 4
Creating Mutex Mutex variables must be declared with type pthread_mutex_t and must be initialized before they can be used Two ways to initialize: Dynamically, with int pthread_mutex_init(pthread_mutex_t *mp, const pthread_mutexattr_t *mattr); this function initializes the mutex variable *mp to its default value if mattr is NULL, or to specify attributes that have already been set with pthread_mutexattr_init() Statically, by pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER ; same as pthread_mutex_init(&mymutex, NULL)
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Destroying Mutex The pthread_mutex_destroy subroutine deletes the mutex int pthread_mutex_destroy(pthread_mutex_t *mutex); After “finished using” a
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.