Lab5-sync1

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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!
Background 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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Process Mutex program structure Steps Define Mutex Initial Mutex …… LockMutex …… UnlockMutex …… DestroyMutex Mutex In threads Thread 1 Thread 2 in critical section
Background 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)
Background image of page 5

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

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

This note was uploaded on 12/01/2010 for the course CS 2342 taught by Professor Dr during the Spring '10 term at HKU.

Page1 / 20

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online