-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.project.semaphores.cpp
More file actions
270 lines (253 loc) · 8.98 KB
/
final.project.semaphores.cpp
File metadata and controls
270 lines (253 loc) · 8.98 KB
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
//
// final.project.semaphores.cpp
// Operating Systems Projects
//
// Created by Xavier Martinez on 11/15/16.
// Copyright © 2016 Xavier Martinez. All rights reserved.
//
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "semaphore.h"
using namespace std;
enum {SAVINGS, CHECKING, MONEY_MARKET, IRA, CARD, AUTO, LOC, MORTGAGE, PRINT};
void child_process(SEMAPHORE &, double *, int);
void account_status(double *);
void account_status(SEMAPHORE &, double *, int);
void parentCleanup(SEMAPHORE &, int);
int main(){
srand(time(NULL));
cout.precision(2);
// Establish shared memory
int shm_id;
shm_id = shmget(IPC_PRIVATE, 8 * sizeof(double), PERMS);
// Establish accounts
double * accounts = (double *) shmat(shm_id, NULL, 0);
accounts[SAVINGS] = 500000;
accounts[CHECKING] = 100000;
accounts[MONEY_MARKET] = 5000;
accounts[IRA] = 1000000;
accounts[CARD] = 10000;
accounts[AUTO] = 30000;
accounts[LOC] = 5000;
accounts[MORTGAGE] = 300000;
// Initialize semaphores
SEMAPHORE sem(9);
sem.V(SAVINGS);
sem.V(CHECKING);
sem.V(MONEY_MARKET);
sem.V(IRA);
sem.V(CARD);
sem.V(AUTO);
sem.V(LOC);
sem.V(MORTGAGE);
sem.V(PRINT);
account_status(accounts);
int status;
// Create child processes by forking
for (int i = 0; i < 5; i++){
pid_t process = fork();
if (process == 0) {
child_process(sem, accounts, i+1);
exit(0);
}
}
// Wait for all child processes to finish
for (int i = 0; i < 5; i ++) {
wait(NULL);
}
parentCleanup(sem, shm_id);
exit(0);
}
// Child process operations
void child_process(SEMAPHORE & sem, double * accounts, int process_identifier){
int randnum = rand();
bool printed = false;
int transactions = 0;
while (transactions < 500) {
if (randnum % 37003 == 0) {
int randnum2 = (rand() % 3) + 1;
switch (randnum2) {
// Operation 1 - deposit 1% of fund or interest in savings
case 1:
sem.P(SAVINGS);
*(accounts + SAVINGS) += (accounts[SAVINGS] / 100.00);
sem.V(SAVINGS);
break;
// Operation 2 - separately deposit 3% of fund or interest in IRA
case 2:
sem.P(IRA);
*(accounts + IRA) += (accounts[IRA] * (3.00/100.00));
sem.V(IRA);
break;
// Operation 3 - separately deposit (or wired) of $1500 into checking
case 3:
sem.P(CHECKING);
*(accounts + CHECKING) += 1500.00;
sem.V(CHECKING);
break;
}
transactions++;
printed = false;
}
// Ooperation 4 - transfer $500 from equity line of credit into checking
else if (randnum % 100003 == 0){
sem.P(LOC);
if ((*(accounts + LOC) -= 500) < 0){
*(accounts + LOC) = 0;
};
sem.V(LOC);
sem.P(CHECKING);
*(accounts + IRA) += 500;
sem.V(CHECKING);
transactions++;
printed = false;
}
else if(randnum % 6301 == 0){
int randnum2 = (rand() % 2) + 1;
// Operation 5 - withdraw $100 from checking or clear $100 check
switch (randnum2) {
case 1:
sem.P(CHECKING);
if ((*(accounts + CHECKING) -= 100) < 0){
*(accounts + CHECKING) = 0;
}
sem.V(CHECKING);
break;
case 2:
// Clear check
if (process_identifier > 2) {
cout << "Process ID = " << process_identifier << ". Clearing $100 check." << endl;
}
break;
}
transactions++;
printed = false;
}
// Operation 6 - deposit a small amount to IRA from a random account
else if(randnum % 126247697 == 0){
int randnum2 = (rand() % 3) + 1;
switch(randnum2){
case 1:
sem.P(IRA);
*(accounts + IRA) += 50;
sem.V(IRA);
sem.P(SAVINGS);
if ((*(accounts + SAVINGS) -= 50) < 0){
*(accounts + SAVINGS) = 0;
}
sem.V(SAVINGS);
break;
case 2:
sem.P(CHECKING);
if ((*(accounts + CHECKING) -= 50) < 0){
*(accounts + CHECKING) = 0;
}
sem.V(CHECKING);
sem.P(IRA);
*(accounts + IRA) += 50;
sem.V(IRA);
break;
case 3:
sem.P(IRA);
// sem.P(MONEY_MARKET);
if ((*(accounts + MONEY_MARKET) -= 50) < 0){
*(accounts + MONEY_MARKET) = 0;
}
*(accounts + IRA) += 50;
sem.V(IRA);
// sem.V(MONEY_MARKET);
break;
}
transactions++;
printed = false;
}
else if(randnum % 7001 == 0){
int randnum2 = (rand() % 2) + 1;
switch (randnum2){
// Loan Operation 1 - transfer $200 from checking to auto loan
case 1:
sem.P(CHECKING);
// sem.P(AUTO);
if ((*(accounts + CHECKING) -= 200) < 0){
*(accounts + CHECKING) = 0;
}
*(accounts + AUTO) += 200;
sem.V(CHECKING);
// sem.V(AUTO);
break;
// Loan Operation 2 - transfer $150 from checking to equity line of credit
case 2:
sem.P(CHECKING);
if ((*(accounts + CHECKING) -= 150) < 0){
*(accounts + CHECKING) = 0;
}
sem.V(CHECKING);
sem.P(LOC);
*(accounts + LOC) += 150;
sem.V(LOC);
break;
}
transactions++;
printed = false;
}
// Loan Operation 3 - transfer $300 from checking to home mortgage
else if (randnum % 3001 == 0){
sem.P(CHECKING);
// sem.P(MORTGAGE);
if ((*(accounts + CHECKING) -= 300) < 0){
*(accounts + CHECKING) = 0;
}
*(accounts + MORTGAGE) += 300;
sem.V(CHECKING);
// sem.V(MORTGAGE);
transactions++;
printed = false;
}
// Print the status of the accounts every 100 transactions
if ((transactions % 100) == 0 and !printed){
account_status(sem, accounts, process_identifier);
printed = true;
}
randnum = rand();
}
// cout << "Process " << process_identifier << " finished!" << endl;
}
// Deallocate shared memory and semaphores
void parentCleanup(SEMAPHORE &sem, int shmid) {
shmctl(shmid, IPC_RMID, NULL);
sem.remove();
}
// Print the status of the accounts with locking capability
void account_status(SEMAPHORE & sem, double * accounts, int pid){
sem.P(PRINT);
cout << endl << "####################" << endl << "Account status printed by process: " << pid << "." << endl;
cout << fixed << "Savings: $" << accounts[SAVINGS] << endl;
cout << fixed <<"Checking: $" << accounts[CHECKING] << endl;
cout << fixed << "Money Market: $" << accounts[MONEY_MARKET] <<endl;
cout << fixed << "IRA: $" << accounts[IRA] <<endl;
cout << fixed << "Credit Card: $" << accounts[CARD] <<endl;
cout << fixed << "Auto Loan: $" << accounts[AUTO] <<endl;
cout << fixed << "Line of Credit: $" << accounts[LOC] << endl;
cout << fixed << "Mortgage: $" << accounts[MORTGAGE] <<endl;
cout << "####################" << endl;
sem.V(PRINT);
}
// Print the status of the accounts
void account_status(double * accounts){
cout << fixed << endl << "Savings: $" << accounts[SAVINGS] << endl;
cout << fixed <<"Checking: $" << accounts[CHECKING] << endl;
cout << fixed << "Money Market: $" << accounts[MONEY_MARKET] <<endl;
cout << fixed << "IRA: $" << accounts[IRA] <<endl;
cout << fixed << "Credit Card: $" << accounts[CARD] <<endl;
cout << fixed << "Auto Loan: $" << accounts[AUTO] <<endl;
cout << fixed << "Line of Credit: $" << accounts[LOC] << endl;
cout << fixed << "Mortgage: $" << accounts[MORTGAGE] <<endl;
}