-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader_first.cc
153 lines (117 loc) · 3.47 KB
/
reader_first.cc
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
150
151
152
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#include<semaphore.h>
#include<pthread.h>
typedef int data_item;
#define DATA_SIZE 10
data_item share_data[DATA_SIZE];
struct params {
int tid;
int wait_time;
int last_time;
};
sem_t mutex, wri;
int read_count = 0;
void toread(data_item* item) {
int index = rand()%DATA_SIZE;
*item = share_data[index];
}
void towrite(data_item item) {
int index = rand()%DATA_SIZE;
share_data[index] = item;
}
void *reader(void *data) {
data_item read_item;
struct params *current = (struct params*)data;
printf("Thread %d wait %d seconds to request a read operation.\n", current->tid, current->wait_time);
sleep(current->wait_time);
sem_wait(&mutex);
read_count++;
if (read_count == 1)
sem_wait(&wri);
sem_post(&mutex);
printf("Thread %d read operation last %d seconds.\n", current->tid, current->last_time);
sleep(current->last_time);
toread(&read_item);
printf("Thread %d get the data %d from buffer.\n", current->tid, read_item);
sem_wait(&mutex);
read_count--;
if (read_count == 0)
sem_post(&wri);
sem_post(&mutex);
free(current);
pthread_exit(0);
}
void *writer(void *data) {
data_item write_item;
struct params *current = (struct params*)data;
printf("Thread %d wait %d seconds to request a write operation.\n", current->tid, current->wait_time);
sleep(current->wait_time);
sem_wait(&wri);
printf("Thread %d write operation last %d seconds.\n", current->tid, current->last_time);
sleep(current->last_time);
write_item = rand()%100;
towrite(write_item);
printf("Thread %d Write %d to buffer.\n", current->tid, write_item);
sem_post(&wri);
free(current);
pthread_exit(0);
}
int main() {
int thread_num = 0;
char mode[100];
int wtime[100], ltime[100];
int i;
FILE *fp;
char buf[255];
fp = fopen("./text", "r");
while (fgets(buf, 255, (FILE*)fp)) {
int flag = 0;
int index = 0;
char tem[100];
for (int i = 1; buf[i-1] != '\n'; i++) {
if (buf[i] == ' '|| buf[i] == '\n') {
tem[i-index] = '\0';
if (flag == 1)
mode[thread_num] = tem[i-index-1];
else if (flag == 2) {
wtime[thread_num] = atoi(tem);
} else if (flag == 3) {
ltime[thread_num] = atoi(tem);
}
index = i+1;
flag++;
} else {
tem[i-index] = buf[i];
}
}
thread_num++;
}
fclose(fp);
for (i = 0; i < DATA_SIZE; i++)
share_data[i] = -1;
pthread_t id[thread_num];
pthread_attr_t attr;
pthread_attr_init(&attr);
srand(time(NULL));
sem_init(&mutex, 0, 1);
sem_init(&wri, 0, 1);
for (i = 0; i < thread_num; i++) {
struct params *parameter = (struct params*)malloc(sizeof(struct params));
parameter->tid = i+1;
parameter->wait_time = wtime[i];
parameter->last_time = ltime[i];
if (mode[i] == 'R')
pthread_create(&id[i], &attr, reader, (void*)parameter);
else
pthread_create(&id[i], &attr, writer, (void*)parameter);
}
for (i = 0; i < thread_num; i++)
pthread_join(id[i], NULL);
for (i = 0; i < DATA_SIZE; i++)
printf("%d ", share_data[i]);
printf("\n");
return 0;
}