|
 |
Semaphores are pretty simple, right? Atomically decrement and test an integer to see if it's locked, atomically increment it to unlock. The P() operation should do the decrementing, and the V() the incrementing. (Dijkstra, the crazy Dutch, defined these operation names to bee Pass and Release, or something like that).
The following implementations of P() and V() are given as the correct ones in my OS textbook. My classmates and I have all discovered that they are not; indeed, P() and V() as given here deadlock. Your task is to help me figure out why. The C code:
pthread_mutex_t mutex_s = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t delay_s = PTHREAD_COND_INITIALIZER;
void P(int &s) {
pthread_mutex_lock(&mutex_s);
if(--s<0) pthread_cond_wait(&delay_s, &mutex_s);
pthread_mutex_unlock(&mutex_s);
}
void V(int &s) {
pthread_mutex_lock(&mutex_s);
++s <= 0 ? pthread_cond_signal(&delay_s) : pthread_mutex_unlock(&mutex_s);
}
The code I'm using that creates the deadlock simply creates two threads which attempt to use P() and V() as a binary lock around the update of a global variable. I will post it upon request.
|
|
|