Semaphores_Mutexes_Condition_Variables

Semaphores_Mutexes_Condition_Variables - Clarifications and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 Il inois CS 241 Staff Copyright : University of Il inois 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 Il inois CS 241 Staff Copyright : University of Il inois 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 Il inois 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 Il inois CS 241 Staff Example: bank balance Shared variable: balance Protected by semaphore when used in: decshared Decrements balance incshared Increments balance Copyright : University of Il inois 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 Il inois 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 Il inois 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...
View Full Document

This note was uploaded on 07/10/2011 for the course CS 241 taught by Professor Campbell during the Spring '08 term at University of Illinois, Urbana Champaign.

Page1 / 6

Semaphores_Mutexes_Condition_Variables - Clarifications and...

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

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