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

11
Copyright ©: Nahrstedt, Angrave, Abdelzaher 1 Semaphore and Mutex Operations

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

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher 1

Semaphore and Mutex Operations

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher

2

CS241 Administrative

SMP2 due tomorrow by 10pm SMP3 is out today at 10pm

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher

3

Review: Basic Operations

wait (sem_t *sp)if (sp->value >0) sp->value--;else {

Add process to sp->list;<block>

}

signal (sem_t *sp)if (sp->list != NULL)

remove next process from sp->list;else sp->value++;

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher

4

Review: Implementation of Semaphores in POSIX

POSIX:SEM semaphore is variable of type sem_t Use <semaphore.h>

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);

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

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.

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher

6

Example, creating shared variable

#include <errno.h>#include <semaphore.h>

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;}

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

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); }

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

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.

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

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);

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

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 sectionunlock 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.

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher

11

Example#include <pthread.h>#include <stdio.h>#include <stdlib.h>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();}