Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.

Similar presentations

Presentation on theme: "Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations."— Presentation transcript:

1 Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations

2 Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 CS241 Administrative SMP2 due tomorrow by 10pm SMP3 is out today at 10pm

3 Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Review: Basic Operations wait (sem_t *sp) if (sp->value >0) sp->value--; else { Add process to sp->list; } signal (sem_t *sp) if (sp->list != NULL) remove next process from sp->list; else sp->value++;

4 Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Review: Implementation of Semaphores in POSIX POSIX:SEM semaphore is variable of type sem_t Use Atomic Operations: int sem_init(sem_t *sem, int pshared, unsigned value); int sem_destroy(sem_t *sem); int sem_post(sem_t *sem); int sem_trywait(sem_t *sem); int sem_wait(sem_t *sem);

5 Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Example 1 on Semaphore We want a shared variable shared (critical section) to be protected by semaphore to allow for two functions decshared – is a function that decrements the current value of the shared variable shared incshared – is a function that increments the shared variable.

6 Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Example, creating shared variable #include static int shared = 0; static sem_t sharedsem; int initshared(int val) { if (sem_init(&sharedsem, 0, 1) == -1) return -1; shared = val; return 0; }

7 Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Example – shared variable int decshared() { while (sem_wait(&sharedsem) == -1) if (errno != EINTR) return -1; shared--; return sem_post(&sharedsem); } int incshared() { while (sem_wait(&sharedsem) == -1) if (errno != EINTR) return -1; shared++; return sem_post(&sharedsem); }

8 Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Mutex Simplest and most efficient thread synchronization mechanism A special variable that can be either in locked state: a distinguished thread that holds or owns the mutex; or unlocked state: no thread holds the mutex When several threads compete for a mutex, the losers block at that call The mutex also has a queue of threads that are waiting to hold the mutex. POSIX does not require that this queue be accessed FIFO.

9 Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 POSIX Mutex-related Functions int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); Also see PTHREAD_MUTEX_INITIALIZER int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex);

10 Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Mutex and Shared Variables Mutex locks are usually used to protect access to a shared variable. The idea: lock the mutex critical section unlock the mutex Unlike a semaphore, a mutex does not have a value, it has states (locked and unlocked). Only the owner of the mutex should unlock the mutex. Do not lock a mutex that is already locked. Do not unlock a mutex that is not already locked.

11 Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Example #include static pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER; void *mythread(void *ptr) { long int i,j; while (1) { pthread_mutex_lock (&my_lock); for (i=0; i<10; i++) { printf ("Thread %d\n", (int) ptr); for (j=0; j<50000000; j++); } pthread_mutex_unlock (&my_lock); for (j=0; j<50000000; j++); } int main (int argc, char *argv[]) { pthread_t thread[2]; pthread_create(&thread[0], NULL, mythread, (void *)0); pthread_create(&thread[1], NULL, mythread, (void *)1); getchar(); }

Download ppt "Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations."

Similar presentations

Ads by Google