Answers to the exersise

  • Here is only one possible answer of those problems. It's ok you solve those problem in a different way.

  • avoid_race.c

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NITERS 10000000

void *count (void *arg);

volatile unsigned int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int main () {

    pthread_t tid1, tid2;
    pthread_create (&tid1, NULL, count, NULL);
    pthread_create (&tid2, NULL, count, NULL);

    pthread_join (tid1, NULL);
    pthread_join (tid2, NULL);
    printf ("cnt:%d\n", cnt);
    exit (0);

}

void *count (void *arg) {

    volatile int i = 0;

    for (; i < NITERS; i++) {
        pthread_mutex_lock(&mutex);
        cnt++;
        pthread_mutex_unlock(&mutex);
    }

    return NULL;
}
  • avoid_deadlock.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

void *Allen(void *arg);
void *Bob(void *arg);

pthread_mutex_t book1;
pthread_mutex_t book2;
pthread_cond_t Allenfinish;

int main() {
    pthread_t tid1, tid2;
    pthread_create(&tid1, NULL, &Allen, NULL);
    pthread_create(&tid2, NULL, &Bob, NULL);

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    return 0;
}

void *Allen(void *arg) {
    pthread_mutex_lock(&book1);
    sleep(5);
    pthread_mutex_lock(&book2);
    printf("Allen has collected all books he need, he is going to do homework!\n");
    sleep(5);
    printf("Allen has finished his homework, he has returned all books he borrowed!\n");
    pthread_mutex_unlock(&book2);
    pthread_mutex_unlock(&book1);
    pthread_cond_signal(&Allenfinish);
}

void *Bob(void *arg) {
    pthread_mutex_lock(&book2);
    pthread_cond_wait(&Allenfinish, &book2);

    printf("Bob knows he can borrow those two books now!\n");
    pthread_mutex_lock(&book1);
    sleep(5);
    printf("Bob has finished his homework now!\n");
    pthread_mutex_unlock(&book1);
    pthread_mutex_unlock(&book2);
}
  • No answers to Starvation to problem. It's really a easy one and you can solve it!