-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlift_sim_B.c
284 lines (226 loc) · 9.39 KB
/
lift_sim_B.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (c) 2020 Rohan Khayech
*/
/* Lift Sim B C File*/
/* Implementation of the main functions for Part B using processes. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include "lift_sim.h"
#include "log.h"
#include "lift_sim_B.h"
int shared_id; /* The ID of the shared memory segment */
/* The main line for Part B */
int main(int argc, char **argv)
{
Lift *l1, *l2, *l3; /* Pointer to a lift struct holding data about the lift. */
if (argc == 3 && atoi(argv[1]) >= 1 && atoi(argv[1]) <= 100 && atoi(argv[2]) >= 0) /* if command args are correct */
{
/* retrieve a segment of shared memory */
shared_id = shmget(IPC_PRIVATE, sizeof(Shared), S_IRUSR | S_IWUSR);
/* attach the shared memory to the main process */
shared = (Shared*)shmat(shared_id,NULL,0);
/* init end value to 0 */
shared->end = 0;
/* init binary semaphores (used as mutex locks) */
sem_init(&shared->bufferLock, 1, 1);
sem_init(&shared->outputLock, 1, 1);
/* read the command args */
shared->maxBuffSize = atoi(argv[1]);
shared->requestTime = atoi(argv[2]);
/* init the buffer 'pointers' */
shared->in = 0;
shared->out = 0;
/* init the counting semaphores for the buffer */
sem_init(&shared->freeSpaces,1,shared->maxBuffSize);
sem_init(&shared->fullSpaces,1,0);
/* empty the output log file */
clearLog();
/* allocate 3 lift structs */
/* these are duplicated for each child process, must be freed in each child process */
/* uses a static variable to assign a unique lift number, so must be created within the same process */
l1 = initLift();
l2 = initLift();
l3 = initLift();
/* create child processes */
if (fork() == 0) /*child process (LiftR)*/
{
/* free the unused lift structs */
free(l1);
free(l2);
free(l3);
/* run the lift request function */
request();
} else /*parent process*/
{
if (fork() == 0) /*child process (Lift1)*/
{
/* free the unused lift structs */
free(l2);
free(l3);
/* run the lift operation function */
lift(l1);
}
else /*parent process*/
{
if (fork() == 0) /*child process (Lift2)*/
{
/* free the unused lift structs */
free(l1);
free(l3);
/* run the lift operation function */
lift(l2);
}
else /*parent process*/
{
if (fork() == 0) /*child process (Lift3)*/
{
/* free the unused lift structs */
free(l1);
free(l2);
/* run the lift operation function */
lift(l3);
}
else /*parent process*/
{
/* free the original lifts */
free(l1);
free(l2);
free(l3);
/* wait for all child processes to terminate */
wait(NULL);
wait(NULL);
wait(NULL);
wait(NULL);
/* update the lift pointers to point to the returned lifts in shared memory */
l1 = &shared->liftReturn[0];
l2 = &shared->liftReturn[1];
l3 = &shared->liftReturn[2];
/* print out the total requests/movements to the log */
printTotals(l1,l2,l3);
/* destroy semaphores */
sem_destroy(&shared->bufferLock);
sem_destroy(&shared->outputLock);
sem_destroy(&shared->freeSpaces);
sem_destroy(&shared->fullSpaces);
/* detach the shared memory from the main process */
shmdt(shared);
/* close the shared memory */
shmctl(shared_id,IPC_RMID,NULL);
return 0;
}
}
}
}
return 0;
}
else
{
/* prompt user to rerun program with the correct args */
printf("Usage: lift_sim_B m t\n");
printf("\tm = buffer size (must be >= 1 and <=100)\n");
printf("\tt = request time (seconds)\n");
return 1; /* exit with error code 1 as the program couldn't run */
}
}
/* Lift Request Function */
/* Reads lift requests from the input file and places them in the shared buffer. */
void request()
{
LiftRequest *r; /* Points to a lift request from the input file. */
FILE *sim_input; /* Pointer to the input file */
/*attach the shared memory to the process */
shared = (Shared *)shmat(shared_id, NULL, 0);
/*open the input file*/
sim_input = fopen("sim_input", "rb"); /* input file only accessed by the one process, doesn't require mutual exclusion */
if (sim_input != NULL) /* if the file opened correctly */
{
while (!(shared->end)) /* while there are still requests to read */
{
/* retrieve a lift request from the input file if avaliable*/
r = readRequest(sim_input);
if (r != NULL) /* if the request is valid */
{
/* CRITICAL SECTION */
sem_wait(&shared->freeSpaces); /*wait until there is a free space in the buffer*/
sem_wait(&shared->bufferLock); /*wait until buffer is avaliable and obtain lock*/
/* place the lift request struct in the shared memory buffer */
shared->buffer[shared->in] = *r;
/* print new request to console */
printf("New Lift Request from Floor %d to Floor %d\n", r->from, r->to);
printf("Request No: %d\n", r->rNum);
/* print new request to the log file */
printRequestToLog(r);
/* free the request pointer */
free(r);
/* increase the in counter to the next free space in the buffer */
shared->in = (shared->in + 1) % shared->maxBuffSize;
sem_post(&shared->bufferLock); /* release lock on buffer */
sem_post(&shared->fullSpaces); /*increase the number of filled spaces in the buffer*/
/* END CRITICAL SECTION */
}
else /* no more requests in file or request was invalid, terminate process*/
{
shared->end = 1; /* liftR can terminate */
printf("No more requests in file (or invalid request). Ending LiftR.\n");
}
}
/*close the input file */
fclose(sim_input);
}
else
{
printf("Error opening file.\n");
shared->end = 1; /* liftR can terminate */
}
shmdt(shared);
}
/* Lift Operation Function */
/* Retrieves lift requests from the shared buffer and performs the specified operations. Takes a pointer to a Lift struct as an argument. */
void lift(Lift *lift)
{
LiftRequest r; /* Stores the lift request taken from the buffer */
int done = 0; /* Boolean indicating whether there are requests left to perform (if not the Lift thread will terminate) */
/*attach the shared memory to the process */
shared = (Shared *)shmat(shared_id, NULL, 0);
/* take and perform request if avaliable or wait until avaliable */
while (done == 0)
{
int semVal; /* Stores the value of the full semaphore (counts how many requests are in the buffer). */
sem_getvalue(&shared->fullSpaces,&semVal);
if (shared->end == 1 && semVal<=0) /*if no more requests in file or buffer*/
{
done = 1; /* lift can terminate */
}
else
{
/* wait while buffer is empty or signalled to end */
sem_wait(&shared->fullSpaces);
sem_wait(&shared->bufferLock); /*wait until buffer is avaliable and obtain lock*/
/* CRITICAL SECTION */
/* retrieve the next request from the buffer and increase the out counter to the next place */
r = shared->buffer[shared->out];
shared->out = (shared->out + 1) % shared->maxBuffSize;
/* perform the lift operation */
moveLift(lift, &r);
sem_post(&shared->bufferLock); /* release lock on buffer */
/* END CRITICAL SECTION */
sem_post(&shared->freeSpaces); /*increase the number of empty spaces in the buffer*/
/* wait t seconds before retrieving another request */
sleep(shared->requestTime);
}
}
/* return the lift struct to the main process via shared memory */
shared->liftReturn[(lift->lNum)-1] = *lift;
/* free the lift pointer */
free(lift);
shmdt(shared);
}