-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferCacheFinal.cpp
417 lines (350 loc) · 9.56 KB
/
BufferCacheFinal.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define BUFFER_SIZE 1024
#define HASHVAL 3
struct BUFFER
{
int iStatus; // 0 : free / 1 : locked / 2 : delayed write
int iBlockNumber;
char cData[BUFFER_SIZE];
struct BUFFER *nextHashQueue;
struct BUFFER *prevHashQueue;
struct BUFFER *nextFreeList;
struct BUFFER *prevFreeList;
};
typedef struct TreadParam
{
int db;
int processNum;
struct BUFFER ** hashHead;
struct BUFFER ** freeHead;
}THREAD_PARAM,*THPARAM;
#pragma region INITIAL_STAGE
struct BUFFER * insert(struct BUFFER * insertHead, int BlockNumber, struct BUFFER ** freeHead)
{
// initialization
struct BUFFER *st = (struct BUFFER *)malloc(sizeof(struct BUFFER));
// assigning values
st->iStatus = 0;
st->iBlockNumber = BlockNumber;
st->nextHashQueue = NULL;
st->prevHashQueue = NULL;
st->nextFreeList = st;
st->prevFreeList = st;
// for hashlist
if (insertHead == NULL)
{
insertHead = st;
}
else
{
struct BUFFER *traverse;
traverse = insertHead;
while (traverse->nextHashQueue != NULL)
{
traverse = traverse->nextHashQueue;
}
traverse->nextHashQueue = st;
st->prevHashQueue = traverse;
}
// for freelist
if (*freeHead == NULL)
{
*freeHead = st;
}
else
{
struct BUFFER *traverse;
traverse = *freeHead;
st->nextFreeList = traverse->nextFreeList->prevFreeList;
st->prevFreeList = traverse->prevFreeList;
traverse->prevFreeList->nextFreeList = st;
traverse->prevFreeList = st;
}
return(insertHead);
}
void displayHash(struct BUFFER *head)
{
struct BUFFER *traverse;
traverse = head;
while (traverse != NULL)
{
printf("\t\t BLK_NO = %d | STATUS = %d ---> ", traverse->iBlockNumber,traverse->iStatus);
traverse = traverse->nextHashQueue;
}
printf(" NULL \n");
}
void displayFree(struct BUFFER *head)
{
struct BUFFER *traverse;
traverse = head;
if (head == NULL)
{
printf("No data\n");
}
else
{
printf("\t\t BLK_NO = %d ---> ", traverse->iBlockNumber);
traverse = traverse->nextFreeList;
while (traverse != head)
{
printf(" BLK_NO = %d ---> ", traverse->iBlockNumber);
traverse = traverse->nextFreeList;
}
}
}
#pragma endregion
void GetBlk(int db, struct BUFFER ** hashHead, struct BUFFER ** freeHead)
{
printf("\nBlock Number is : %d \n", db);
int modVal = db % HASHVAL;
printf("Hash Value of %d is : %d \n",db, modVal);
struct BUFFER *traverse;
traverse = hashHead[modVal];
while (hashHead[modVal] != NULL && traverse->iBlockNumber != db && traverse->nextHashQueue != NULL)
{
traverse = traverse->nextHashQueue;
}
if (hashHead[modVal] != NULL && traverse->iBlockNumber == db)
{
printf("\n\n GOT THE BUFFER IN BUFFER POOL \n\n");
// if buffer on free list, remove it from free list
if (traverse->nextFreeList != NULL && traverse->prevFreeList != NULL)
{
traverse->prevFreeList->nextFreeList = traverse->nextFreeList;
traverse->nextFreeList->prevFreeList = traverse->prevFreeList;
*freeHead = traverse->nextFreeList;
traverse->prevFreeList = NULL;
traverse->nextFreeList = NULL;
traverse->iStatus = 1;
}
}
else
{
printf("Buffer Not Present In Pool !!\n");
printf("Getting buffer from Free List\n");
struct BUFFER *t;
t = *freeHead;
if (t == NULL)
{
printf("No buffer in free list");
printf("exiting");
exit(0); // free and then exit
}
printf("Picking up 1st buffer from free list");
t->prevFreeList->nextFreeList = t->nextFreeList;
t->nextFreeList->prevFreeList = t->prevFreeList;
*freeHead = t->nextFreeList;
t->prevFreeList = NULL;
t->nextFreeList = NULL;
t->iStatus = 1;
// remove it from old hash queue and put it on new
// we have traverse at end of hashqueue[modbval] so use it
if (t->prevHashQueue == NULL)
{
printf("\n *** 1st free buffer in hashqueue ");
// this is 1st free buffer on hash queue
int val = (t->iBlockNumber) % HASHVAL;
printf("\nval : %d", val);
hashHead[val] = t->nextHashQueue;
if (t->nextHashQueue != NULL)
{
t->nextHashQueue->prevHashQueue = NULL;
}
}
else
{
t->prevHashQueue->nextHashQueue = t->nextHashQueue;
t->nextHashQueue->prevHashQueue = t->prevHashQueue;
}
t->prevHashQueue = traverse;
t->nextHashQueue = NULL;
if (traverse != NULL)
traverse->nextHashQueue = t;
else
hashHead[modVal] = t;
t->iBlockNumber = db; // so that we can use prev info of buffer in between
}
}
DWORD MyThreadProc(LPVOID param)
{
// initialization of variables
THREAD_PARAM *th = (THPARAM)param;
struct BUFFER ** hashHead = th->hashHead;
int db = th->db;
struct BUFFER ** freeHead = th->freeHead;
int procNum = th->processNum;
struct BUFFER *UsedBuff; // required for future in (free list element add method)
// code
printf("\nFor Process : %d , Block Number is : %d \n",procNum, db);
int modVal = db % 3;
printf("\nFor Process : %d , Hash Value of %d is : %d \n",procNum, db, modVal);
struct BUFFER *traverse;
traverse = hashHead[modVal];
while (hashHead[modVal] != NULL && traverse->iBlockNumber != db && traverse->nextHashQueue != NULL)
{
traverse = traverse->nextHashQueue;
}
if (hashHead[modVal] != NULL && traverse->iBlockNumber == db)
{
printf("\n\n Process %d , GOT THE BUFFER \n\n",procNum);
// if buffer on free list, remove it from free list
if (traverse->nextFreeList != NULL && traverse->prevFreeList != NULL)
{
traverse->prevFreeList->nextFreeList = traverse->nextFreeList;
traverse->nextFreeList->prevFreeList = traverse->prevFreeList;
*freeHead = traverse->nextFreeList;
traverse->prevFreeList = NULL;
traverse->nextFreeList = NULL;
traverse->iStatus = 1;
}
}
else
{
printf("For Process %d, Buffer Not Present In Pool !!\n",procNum);
printf("For Process %d, Getting buffer from Free List\n",procNum);
struct BUFFER *t;
t = *freeHead;
if (t == NULL)
{
printf("For Process %d, No buffer in free list",procNum);
printf("For Process %d, sleeping for some time....",procNum);
_sleep(rand()%10000);
MyThreadProc(param);
return(0);
//exit(0); // free and then exit
}
printf("For Process %d, Picking up 1st buffer from free list",procNum);
t->prevFreeList->nextFreeList = t->nextFreeList;
t->nextFreeList->prevFreeList = t->prevFreeList;
*freeHead = t->nextFreeList;
t->prevFreeList = NULL;
t->nextFreeList = NULL;
t->iStatus = 1;
//t->iBlockNumber = db;
// remove it from old hash queue and put it on new
// we have traverse at end of hashqueue[modbval] so use it
if (t->prevHashQueue == NULL)
{
printf("\n *** 1st free buffer in hashqueue ");
// this is 1st free buffer on hash queue
int val = (t->iBlockNumber) % HASHVAL;
printf("\nval : %d", val);
hashHead[val] = t->nextHashQueue;
if (t->nextHashQueue != NULL)
{
t->nextHashQueue->prevHashQueue = NULL;
}
}
else
{
t->prevHashQueue->nextHashQueue = t->nextHashQueue;
t->nextHashQueue->prevHashQueue = t->prevHashQueue;
}
t->prevHashQueue = traverse;
t->nextHashQueue = NULL;
if (traverse != NULL)
traverse->nextHashQueue = t;
else
hashHead[modVal] = t;
t->iBlockNumber = db; // so that we can use prev info of buffer in between
//UsedBuff = t;
}
printf("For Process %d, process is working on block ....",procNum);
_sleep(rand() % 1000);
printf("For Process %d, putting buffer on freelist !!",procNum);
//struct BUFFER *UsedBuff;
UsedBuff = hashHead[modVal];
while (UsedBuff->iBlockNumber != db && UsedBuff->nextHashQueue != NULL)
{
UsedBuff = UsedBuff->nextHashQueue;
}
if (UsedBuff->iBlockNumber == db)
{
if (*freeHead == NULL)
{
*freeHead = UsedBuff;
}
else
{
struct BUFFER *forFree;
forFree = *freeHead;
UsedBuff->nextFreeList = forFree->nextFreeList->prevFreeList;
UsedBuff->prevFreeList = forFree->prevFreeList;
forFree->prevFreeList->nextFreeList = UsedBuff;
forFree->prevFreeList = UsedBuff;
}
UsedBuff->iStatus = 0;
}
else
{
printf("For Process %d, some thing is wrong !!!",procNum);
}
return (0);
}
int main()
{
// initialization
struct BUFFER **HashListHead = (struct BUFFER **)malloc(3 * sizeof(struct BUFFER *));
struct BUFFER *FreeListHead = NULL;
int iDataBlock;
int cnt = 1;
// code
#pragma region INITIAL_CALL
// creating buffer pool
printf("\n \t INITIAL STAGE \n \t ");
for (int i = 0; i < HASHVAL; i++)
{
HashListHead[i] = NULL;
for (int j = 0; j < HASHVAL; j++)
{
HashListHead[i] = insert(HashListHead[i], i + (HASHVAL * j), &FreeListHead);
}
}
// printing buffer pool
printf("\n \t PRINTING INITIAL BUFFER POOL STATUS ...\n\n");
for (int i = 0; i < HASHVAL; i++)
{
printf("\nHashList %d :\n", i);
displayHash(HashListHead[i]);
}
printf("FREE LIST:\n");
displayFree(FreeListHead);
#pragma endregion
printf("\n\n\n.........................................\n\n\n");
// playing around
while (cnt != 10)
{
int iDataBlock = rand();
THREAD_PARAM thread_param;
thread_param.db = iDataBlock;
thread_param.freeHead = &FreeListHead;
thread_param.hashHead = HashListHead;
thread_param.processNum = cnt;
//GetBlk(iDataBlock, HashListHead, &FreeListHead); // for single threaded
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThreadProc, (LPVOID)&thread_param, 0, NULL);
printf("\n FOR PROCESS %d ... \n",cnt);
for (int i = 0; i < HASHVAL; i++)
{
printf("HashList %d :\n", i);
displayHash(HashListHead[i]);
}
printf("FREE LIST:\n");
displayFree(FreeListHead);
printf("\n\n");
_sleep(1000);
cnt++;
}
_sleep(30000);
printf("FINAL BUFFER POOL STRUCTURE ....\n\n");
for (int i = 0; i < 3; i++)
{
printf("HashList %d :\n", i);
displayHash(HashListHead[i]);
}
printf("FREE LIST:\n");
displayFree(FreeListHead);
printf("\n\n");
return 0;
}