/
Dining-Philosophers.c
149 lines (106 loc) · 3.64 KB
/
Dining-Philosophers.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Solution for Dinig Philosophers problem implemented for VxWorks for ARM */
/*************************************************************************/
/* HelloSemaPhore.c */
/* */
/*************************************************************************/
/* includes */
#include "vxWorks.h"
#include "stdio.h"
#include "stdlib.h"
#include "semLib.h"
#include "taskLib.h"
/* defines */
#define DELAY_TICKS 60
#define STACK_SIZE 20000
#define MAXSECONDS 100
#define MAXPHILO 20
int i;
int *p ;
/* Semaphore IDs */
SEM_ID *Sema_fork;
int *tPhil;
/* task IDs */
int X;
/* function declarations */
void Philosopher (int,int);
/*************************************************************************/
/* main task */
/* */
/*************************************************************************/
int main (void) {
int nseconds = 0;
int Philos = 0;
X=0;
/* get the simulation time */
while ((nseconds<1) || (nseconds > MAXSECONDS)) {
printf ("Enter overall simulation time [1-%d s]: ", MAXSECONDS);
scanf ("%d", &nseconds);
};
while ((Philos<1) || (Philos > MAXSECONDS)) {
printf ("Enter Number of Philosophers [3-%d]: ", MAXPHILO);
scanf ("%d", &Philos);
};
while ((X<1) || (X > MAXSECONDS)) {
printf ("Enter waiting time [3-%d ticks]: ", MAXSECONDS);
scanf ("%d", &X);
};
printf("\nSimulating %d philosophers with waiting time %d ticks for %d seconds...\n\n", Philos, X, nseconds);
p = (int*)malloc( Philos * sizeof(int) );
for (i=0 ; i<Philos ; i++){
p[i]=0;
}
Sema_fork = (SEM_ID*)malloc( Philos * sizeof(SEM_ID) );
/* create binary semaphores */
for (i=0 ; i<Philos ; i++){
Sema_fork[i] = semBCreate (SEM_Q_FIFO, SEM_FULL);
}
/* spawn (create and start) tasks */
tPhil = (int*)malloc( Philos * sizeof(int) );
for (i=0 ; i<Philos ; i++){
tPhil[i] = taskSpawn ("tPhil", 200, 0, STACK_SIZE,
(FUNCPTR) Philosopher, i,Philos,0,0,0,0,0,0,0,0);
}
/* run for the given simulation time */
taskDelay (nseconds*60);
/* delete tasks and semaphores */
for (i=0 ; i<Philos ; i++){
taskDelete (tPhil[i]);
semDelete (Sema_fork[i]);
printf("\n\n\n");
printf("Philosopher %d ate %d times\n" , i+1 , p[i]);
}
printf("\n\n All philosophers stopped.\n");
return(0);
}
/*************************************************************************/
/* task "professor 1" */
/* */
/*************************************************************************/
void Philosopher (int i, int Philos) {
while (1) {
if (i==Philos-1){
taskDelay (DELAY_TICKS);
printf("Philosopher %d - start thinking\n", i+1);
semTake(Sema_fork[0], WAIT_FOREVER);
taskDelay(X);
semTake (Sema_fork[Philos-1], WAIT_FOREVER);
printf("Philosopher %d - start eating\n", i+1);
taskDelay(10);
semGive(Sema_fork[Philos-1]);
semGive(Sema_fork[0]);
p[i]++;
}
else {
taskDelay (DELAY_TICKS);
printf("Philosopher %d - start thinking\n", i+1);
semTake(Sema_fork[i], WAIT_FOREVER);
taskDelay(X);
semTake (Sema_fork[i+1], WAIT_FOREVER);
printf("Philosopher %d - start eating\n", i+1);
taskDelay(10);
semGive(Sema_fork[i]);
semGive(Sema_fork[i+1]);
p[i]++;
}
};
}