



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















CS 241 Spring 2007System Programming
CS 241 Spring 2007System Programming
^
Mutex Synchronization Primitive ^
Operations on Mutex ^
Examples
Review: Implementation of Semaphoresin POSIX^ POSIX:SEM semaphore is
variable of type sem_t 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); Use <semaphore.h>
#include <errno.h> #include <semaphore.h> static int shared = 0; static sem_t sharedsem; int initshared(int val)
if (
sem_init(&sharedsem, 0, 1)
return -1; shared = val; return 0; }
*int getshared(int sval)
while (
sem_wait(&sharedsem)
if (errno != EINTR)
return -1; *sval = shared; return
sem_post(&sharedsem);
} int incshared()
while (
sem_wait(&sharedsem)
if (errno != EINTR)
return -1; shared++; return
sem_post(&sharedsem);
Thread with Critical Section
(Example
RR:500) -^ #include <errno.h>^ #include <pthread.h>^ #include <semaphore.h>^ #include <stdio.h>^ #include <unistd.h>^ #define TEN_MILLION 10000000L^ #define BUFSIZE 1024^ **void threadout(void args)
char buffer[BUFSIZE]; char *c;^ *sem_t semlockp; struct timespec sleeptime;
Thread with Critical Section /******************
entry section
*************************/
while (
sem_wait(semlockp)
== -1)
/* Entry section */
if(errno != EINTR) {
fprintf(stderr, "Thread failed to lock semaphore\n"); return NULL; } /******************
start of critical section
***********/
while (c != '\0') {*
fputc(c, stderr); c++; nanosleep(&sleeptime, NULL); }*^ /******************
exit section
******************/
if (
sem_post(semlockp)
== -1)
/* Exit section */
fprintf(stderr, "Thread failed to unlock semaphore\n"); /******************
remainder section
****************/
return NULL; }
fprintf (stderr, "Usage: %s numthreads\n", argv[0]); return 1; } n = atoi(argv[1]); *tids = (pthread_t )calloc(n, sizeof(pthread_t)); if (tids == NULL) {
perror("Failed to allocate memory for thread IDs"); return 1; } if (
sem_init(&semlock, 0, 1)
== -1) {
perror("Failed to initialize semaphore"); return 1; }^
What happens if we replace sem_init with sem_init(&semlock,0,0)?
for (i = 0; i < n; i++)
if (error =
pthread_create(tids + i, NULL,
threadout,^ &semlock))
fprintf(stderr, "Failed to create thread:%s\n",
strerror(error));
return 1; } for (i = 0; i < n; i++)
if (error =
pthread_join(tids[i], NULL))
fprintf(stderr, "Failed to join thread:%s\n",
strerror(error));
return 1; } return 0; }
pthread_mutex_init
(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
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);
variable.
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.
mutex
has type
pthread_mutex_t
Since a
mutex
is meant to be used by multiple threads, it is
usually declared to have
static storage class
It can be defined inside a function using the
static
qualifier
if it will only be used by that function or it can
be defined at the top level. A^
mutex
must be initialized before it is used.
This can be done when the
mutex
is defined, as in: