80 lines
1.3 KiB
C
80 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
|
|
#include "common.h"
|
|
#include "common_threads.h"
|
|
|
|
typedef struct {
|
|
int num_loops;
|
|
int thread_id;
|
|
} arg_t;
|
|
|
|
sem_t forks[5];
|
|
|
|
int left(int p) {
|
|
return p;
|
|
}
|
|
|
|
int right(int p) {
|
|
return (p + 1) % 5;
|
|
}
|
|
|
|
void get_forks(int p) {
|
|
Sem_wait(&forks[left(p)]);
|
|
Sem_wait(&forks[right(p)]);
|
|
}
|
|
|
|
void put_forks(int p) {
|
|
Sem_post(&forks[left(p)]);
|
|
Sem_post(&forks[right(p)]);
|
|
}
|
|
|
|
void think() {
|
|
return;
|
|
}
|
|
|
|
void eat() {
|
|
return;
|
|
}
|
|
|
|
void *philosopher(void *arg) {
|
|
arg_t *args = (arg_t *) arg;
|
|
int p = args->thread_id;
|
|
|
|
int i;
|
|
for (i = 0; i < args->num_loops; i++) {
|
|
think();
|
|
get_forks(p);
|
|
eat();
|
|
put_forks(p);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc != 2) {
|
|
fprintf(stderr, "usage: dining_philosophers_deadlock <num_loops>\n");
|
|
exit(1);
|
|
}
|
|
printf("dining: started\n");
|
|
|
|
int i;
|
|
for (i = 0; i < 5; i++)
|
|
Sem_init(&forks[i], 1);
|
|
|
|
pthread_t p[5];
|
|
arg_t a[5];
|
|
for (i = 0; i < 5; i++) {
|
|
a[i].num_loops = atoi(argv[1]);
|
|
a[i].thread_id = i;
|
|
Pthread_create(&p[i], NULL, philosopher, &a[i]);
|
|
}
|
|
|
|
for (i = 0; i < 5; i++)
|
|
Pthread_join(p[i], NULL);
|
|
|
|
printf("dining: finished\n");
|
|
return 0;
|
|
}
|