This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPh.cpp
81 lines (68 loc) · 1.74 KB
/
DPh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
typedef struct {
int position;
int count;
sem_t *chopsticks;
sem_t *lock;
} params_t;
void initialize_semaphores(sem_t *lock, sem_t *chopsticks, int num_chopsticks);
void run_all_threads(pthread_t *threads, sem_t *chopsticks, sem_t *lock, int num_philosophers);
void *philosopher(void *params);
void think(int position);
void eat(int position);
int askForNumber(){
int i;
printf( "Enter a number:");
scanf("%d", &i);
return i;
}
int main()
{
int num_philosophers = askForNumber();
sem_t lock;
sem_t chopsticks[num_philosophers];
pthread_t philosophers[num_philosophers];
int i;
for(i = 0; i < num_philosophers; i++) {
sem_init(&chopsticks[i], 0, 1);
}
sem_init(&lock, 0, num_philosophers - 1);
//run_all_threads
for(i = 0; i < num_philosophers; i++) {
params_t *arg = (params_t*)malloc(sizeof(params_t));
arg->position = i;
arg->count = num_philosophers;
arg->lock = &lock;
arg->chopsticks = chopsticks;
pthread_create(&philosophers[i], NULL, philosopher, (void *)arg);
}
pthread_exit(NULL);
}
void *philosopher(void *params)
{
int i;
params_t me = *(params_t *)params;
for(i=0;i<100;i++){
think(me.position);
sem_wait(me.lock);
sem_wait(&me.chopsticks[me.position]);
sem_wait(&me.chopsticks[(me.position + 1) % me.count]);
eat(me.position);
sem_post(&me.chopsticks[me.position]);
sem_post(&me.chopsticks[(me.position + 1) % me.count]);
sem_post(me.lock);
}
think(me.position);
pthread_exit(NULL);
}
void think(int position)
{
printf("I am philosopher %d thinking.\n", position+1);
}
void eat(int position)
{
printf("I am philosopher %d eating.\n", position+1);
}