-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp.c
79 lines (66 loc) · 1.63 KB
/
dp.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
/*
* Reader and Writer's Problem for disk scheduler
* CSE 511 Test Program
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "dp_serial.h"
#define DELAY 100000
#define EatTime 1.0
#define ThinkTime 1.0
#define NUM_PHILOSOPHERS 5
void *model_eat(int id)
{
printf("Philosopher #%d is eating.\n", id);
double eat_time = ( rand() % (NUM_PHILOSOPHERS * 2) ) * EatTime - (NUM_PHILOSOPHERS - 1) * ThinkTime;
sleep( (eat_time > 0) ? eat_time : EatTime);
return NULL;
}
void *model_think(int id)
{
printf("Philosopher #%d is thinking.\n", id);
double think_time = ( rand() % (NUM_PHILOSOPHERS * 2) ) * ThinkTime - (NUM_PHILOSOPHERS - 1) * EatTime;
sleep( (think_time > 0) ? think_time : ThinkTime);
return NULL;
}
void *action(void* id)
{
int tid = (int)id;
while(1)
{
printf("Philosopher #%d is going to eat.\n", tid);
Eat(tid, model_eat);
printf("Philosopher #%d 's eating is done.\n", tid);
Think(tid, model_think);
}
return NULL;
}
int main(int argc, char *argv[])
{
// Initialize vars
long i;
int rc;
pthread_t philosopher[NUM_PHILOSOPHERS];
// Initialize the system
Init_dp(NUM_PHILOSOPHERS);
// Create the r/w requests (as threads) to the disk
for (i = 0; i < NUM_PHILOSOPHERS; i++)
{
rc = pthread_create(&philosopher[i], NULL, action, (void *)i);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
printf("Philosophers created successfully!\n");
// Wait until all threads are done
for (i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_join (philosopher[i], NULL);
printf("No more food!\n");
// Finish
return 0;
}