PROCESS SYNCHRONIZATION USING SEMAPHORES
(Producer Consumer Problem)
PTHREAD MUTEX FUNCTIONS:
Mutex is a data type used in Pthreads to provide functions that will block a thread if another thread is accessing data that it is using. Mutex has two basic operations, lock and unlock. If a mutex is unlocked and a thread calls lock, the mutex locks and the thread continues. If however the mutex is locked, the thread blocks until the thread ‘holding’ the lock calls unlock.
There are 5 basic functions for dealing with mutex.
-
Initialize a mutex with specified attributes
-
int pthread_mutex_init (pthread_mutex_t *mut, const pthread_mutexattr_t *attr);
-
-
Lock the Mutex
-
int pthread_mutex_lock (pthread_mutex_t *mut);
-
-
Unlock the Mutex.
-
int pthread_mutex_unlock (pthread_mutex_t *mut);
-
-
Acquires the lock on the Mutex.
-
int pthread_mutex_trylock (pthread_mutex_t *mut);
-
-
Deallocate any memory or other resources associated with the Mutex.
-
int pthread_mutex_destroy (pthread_mutex_t *mut);
-