-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchopsticks.c
86 lines (78 loc) · 2.42 KB
/
chopsticks.c
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
82
83
84
85
86
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <inttypes.h>
#include <pthread.h>
#include "gtthread.h"
#include "philosopher.h"
/*
* Performs necessary initialization of mutexes.
*/
static gtthread_mutex_t chopsticks_mutex[5];
void chopsticks_init(){
int i;
for (i = 0; i < 5; ++i)
{
gtthread_mutex_init(&chopsticks_mutex[i]);
}
}
/*
* Cleans up mutex resources.
*/
void chopsticks_destroy(){
}
/*
* Uses pickup_left_chopstick and pickup_right_chopstick
* to pick up the chopsticks
*/
void pickup_chopsticks(int phil_id){
if (philosopher_chopstics_left_right_correspondance(phil_id, 0) > philosopher_chopstics_left_right_correspondance(phil_id, 1))
{
gtthread_mutex_lock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 1)]);
pickup_right_chopstick(phil_id);
gtthread_mutex_lock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 0)]);
pickup_left_chopstick(phil_id);
}
else
{
gtthread_mutex_lock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 0)]);
pickup_left_chopstick(phil_id);
gtthread_mutex_lock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 1)]);
pickup_right_chopstick(phil_id);
}
}
/*
* Uses pickup_left_chopstick and pickup_right_chopstick
* to pick up the chopsticks
*/
void putdown_chopsticks(int phil_id){
if (philosopher_chopstics_left_right_correspondance(phil_id, 1) > philosopher_chopstics_left_right_correspondance(phil_id, 0))
{
putdown_right_chopstick(phil_id);
gtthread_mutex_unlock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 1)]);
putdown_left_chopstick(phil_id);
gtthread_mutex_unlock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 0)]);
}
else
{
putdown_left_chopstick(phil_id);
gtthread_mutex_unlock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 0)]);
putdown_right_chopstick(phil_id);
gtthread_mutex_unlock(&chopsticks_mutex[philosopher_chopstics_left_right_correspondance(phil_id, 1)]);
}
}
int philosopher_chopstics_left_right_correspondance(int phil_id, int left_right_indicator){
// 0 for left, 1 for right
if (left_right_indicator == 0)
{
if (phil_id == 0)
return 4;
else
return(phil_id - 1);
}
else if (left_right_indicator == 1)
{
return phil_id;
}
return -1;
}