Starvation problem
Bob and Allen also are borrowing books from library, but now after Allen borrows a book, he doesn't want to return it to library. So Bob can't borrow this book.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
pthread_mutex_t book=PTHREAD_MUTEX_INITIALIZER;
void *Allen(void *arg);
void *Bob(void *arg);
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) {
printf("I'm Allen, I borrow this book and I won't return it!\n");
pthread_mutex_lock(&book);
}
void *Bob(void *arg) {
pthread_mutex_lock(&book);
printf("I'm Bob, I can borrow it now\n");
}
I think you can solve this problem using pthread mutex, just try it!