/
worker.c
231 lines (186 loc) · 4.81 KB
/
worker.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Matt Dumford - mdumford
* mdumfo2@uic.edu
*
* WORKER
*
* Reads and writes from shared memory and reports errors to master.
* Locks down shared memory with semaphores if lock is turned on.
*/
#include "message.h"
#include <errno.h>
#include <sys/msg.h>
#include <string.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define BOOL int
#define TRUE 1
#define FALSE 0
float strtof(const char *str, char **endptr); //only need this declaration on bert...?
int str2int(char *str); //convert a string to int, wrapper for strtol
float str2float(char *str); //convert a string to float, wrapper for strtof
void wait(int sem, int semID, int nBuffers); //reserves resources for a semaphore
void signal(int sem, int semID, int nBuffers); //frees resources for a semaphore
int main(int argc, char **argv){
if(argc < 6 || argc > 7){
printf("Wrong number of arguments!");
exit(-1);
}
int workerID = str2int(argv[1]);
int nBuffers = str2int(argv[2]);
float sleepTime = str2float(argv[3]);
int msgID = str2int(argv[4]);
int shmID = str2int(argv[5]);
int semID;
BOOL lock = FALSE;
if(argc == 7){ //if a semID is given
semID = str2int(argv[6]);
lock = TRUE;
}
//connect to message queue
int msgQ = msgID;
if(msgQ == -1){
perror("Error connecting to message queue");
exit(-1);
}
//send startup message
struct message msg;
msg.mtype = 1;
msg.workerID = workerID;
msg.sleepTime = sleepTime;
if(msgsnd(msgQ, &msg, sizeof(struct message) - sizeof(long), 0) == -1){
perror("Error sending message");
exit(-1);
}
//attatch to shared memory
int *shm = shmat(shmID, (void *)0, 0);
if(shm == (int *) -1){
perror("Error attatching to shared memory");
exit(-1);
}
//===================== PART 5 ==============================
//use semaphores
if(lock){
int i;
int currentBuffer = workerID;
for(i=0; i<nBuffers; i++){
int j;
for(j=0; j<2; j++){ //read twice
wait(currentBuffer, semID, nBuffers);
int read = shm[currentBuffer];
if(usleep(sleepTime*1000000) == -1){
perror("Error sleeping");
exit(-1);
}
//check if changed
if(shm[currentBuffer] != read){
msg.mtype = 3;
msg.workerID = workerID;
msg.changedBuffer = currentBuffer;
msg.initVal = read;
msg.newVal = shm[currentBuffer];
if(msgsnd(msgQ, &msg, sizeof(struct message), 0) == -1){
perror("Error sending message");
exit(-1);
}
}
signal(currentBuffer, semID, nBuffers);
currentBuffer += workerID;
if(currentBuffer >= nBuffers)
currentBuffer -= nBuffers;
}
//write
wait(currentBuffer, semID, nBuffers);
int read = shm[currentBuffer];
if(usleep(sleepTime*1000000) == -1){
perror("Error sleeping");
exit(-1);
}
shm[currentBuffer] = read | (1<<(workerID -1));
signal(currentBuffer, semID, nBuffers);
currentBuffer += workerID;
if(currentBuffer >= nBuffers)
currentBuffer -= nBuffers;
}
}
//===================== PART 3/4 ============================
//dont use semaphores
else{
int i;
int currentBuffer = workerID;
for(i=0; i<nBuffers; i++){
int j;
for(j=0; j<2; j++){ //read twice
int read = shm[currentBuffer];
if(usleep(sleepTime*1000000) == -1){
perror("Error sleeping");
exit(-1);
}
//check if changed
if(shm[currentBuffer] != read){
msg.mtype = 3;
msg.workerID = workerID;
msg.changedBuffer = currentBuffer;
msg.initVal = read;
msg.newVal = shm[currentBuffer];
if(msgsnd(msgQ, &msg, sizeof(struct message), 0) == -1){
perror("Error sending message");
exit(-1);
}
}
currentBuffer += workerID;
if(currentBuffer >= nBuffers)
currentBuffer -= nBuffers;
}
//write
int read = shm[currentBuffer];
if(usleep(sleepTime*1000000) == -1){
perror("Error sleeping");
exit(-1);
}
shm[currentBuffer] = read | (1<<(workerID -1));
currentBuffer += workerID;
if(currentBuffer >= nBuffers)
currentBuffer -= nBuffers;
}
}
//cleanup message
msg.mtype = 2;
msg.workerID = workerID;
if(msgsnd(msgQ, &msg, sizeof(struct message), 0) == -1){
perror("Error sending message");
exit(-1);
}
exit(0);
}
void wait(int sem, int semID, int nBuffers){
struct sembuf sembuffer;
sembuffer.sem_num = sem;
sembuffer.sem_op = -1;
sembuffer.sem_flg = SEM_UNDO;
if(semop(semID, &sembuffer, 1) == -1){
perror("Error in semop");
exit(-1);
}
}
void signal(int sem, int semID, int nBuffers){
struct sembuf sembuffer;
sembuffer.sem_num = sem;
sembuffer.sem_op = 1;
sembuffer.sem_flg = SEM_UNDO;
if(semop(semID, &sembuffer, 1) == -1){
perror("Error in semop");
exit(-1);
}
}
int str2int(char *str){
return strtol(str, 0, 10);
}
float str2float(char *str){
char *end;
return strtof(str, &end);
}