{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


Semaphores_Mutexes_Condition_Variables - forthefirsttime...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Clarifications and Corrections Response Time Time from job  submission until it  starts running  for the first time Waiting Time Total time that the job is  not running but queued Turnaround Time Time between job  submission and  completion Copyright ©:  University of Illinois CS 241 Staff Copyright ©: University of Illinois CS 241 Staff 1 Semaphores, Mutexes and  Condition Variables Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a  section of the code Mutexes Permit only one thread to execute a section of  the code Condition Variables Communicate information about the state of  shared data Copyright ©:  University of Illinois CS 241 Staff Copyright ©: University of Illinois CS 241 Staff Counting Semaphores Before entering critical  section  semWait(s) Wait until value is > 0,  then decrement After finishing critical  section semSignal(s) Increment value Implementation  requirements semSignal and  semWait must be atomic semaphore s = 1; P i { while(1) { semWait(s); <Critical Section> semSignal(s); <Other work> } } Copyright ©: University of Illinois CS 241 Staff POSIX Semaphores Data type Semaphore is a variable of type  sem_t Include  <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); use  pshared==0 Copyright ©: University of Illinois CS 241 Staff Example: bank balance Shared variable:  balance Protected by semaphore  when used in: decshared Decrements  balance incshared Increments  balance
Background image of page 1

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

View Full Document Right Arrow Icon
Copyright ©: University of Illinois CS 241 Staff Example: bank balance #include <errno.h> #include <semaphore.h> static int balance = 0; static sem_t bal_sem; int initshared(int val) { if (sem_init(&bal_sem, 0, 1) == -1) return -1; balance = val; return 0; } pshared value Copyright ©: University of Illinois CS 241 Staff Example: bank balance int decshared() { while (sem_wait(&bal_sem) == -1) if (errno != EINTR) return -1; balance--; return sem_post(&bal_sem); } int incshared() { while (sem_wait(&bal_sem) == -1) if (errno != EINTR) return -1; balance++; return sem_post(&bal_sem); } Which one is going first? Pthread Synchronization Mutex  Semaphore with maximum value 1 Simple and efficient Locked: some thread holds the mutex Unlocked: no thread holds the mutex When several threads compete  One wins The rest block Queue of blocked threads Copyright ©: University of Illinois CS 241 Staff Mutex Variables A typical sequence in the use of a mutex Create and initialize a mutex variable  Several threads attempt to lock the mutex  Only one succeeds and now owns the mutex 
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Semaphores_Mutexes_Condition_Variables - forthefirsttime...

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

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