-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.cpp
708 lines (540 loc) · 15 KB
/
server2.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/** This program illustrates the server end of the message queue **/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include <vector>
#include <list>
#include <string>
#include <fstream>
#include <iostream>
#include "msg.h"
#include <time.h>
using namespace std;
struct record
{
/* The record id */
int id;
/* The first name */
string firstName;
/* The first name */
string lastName;
/**
* Prints the record
* @param fp - the stream to print to
*/
void print(FILE* fp)
{
fprintf(stderr, "%d--%s--%s", id, firstName.c_str(), lastName.c_str());
}
};
/**
* The structure of a hashtable cell
*/
class hashTableCell
{
/* Public members */
public:
pthread_mutex_t sMutex;
/**
* Initialize the mutex
*/
hashTableCell()
{
/* TODO: Initialize the mutex associated with this cell */
sMutex = PTHREAD_MUTEX_INITIALIZER;
}
/**
* Initialize the mutex
*/
~hashTableCell()
{
if(pthread_mutex_destroy(&sMutex)!=0){
perror("deallocate mutex");
exit (-1);
}
}
/**
* Locks the cell mutex
*/
void lockCell()
{
if(pthread_mutex_lock(&sMutex)!=0){
perror("lock");
exit (-1);
}
}
/**
* Unlocks the cell mutex
*/
void unlockCell()
{
if(pthread_mutex_unlock(&sMutex)!=0){
perror("unlock");
exit (-1);
}
}
/* The linked list of records */
list<record> recordList;
/**
* TODO: Declare the cell mutex
*/
};
/* The number of cells in the hash table */
#define NUMBER_OF_HASH_CELLS 100
/* The number of inserter threads */
#define NUM_INSERTERS 5
/* The hash table */
vector<hashTableCell> hashTable(NUMBER_OF_HASH_CELLS);
/* The number of fetcher threads */
vector<pthread_t> fetcherThreadIds;
/* The thread ids */
vector<pthread_t> threadsIds(NUM_INSERTERS);
/* The number of threads */
int numThreads = 0;
/* The message queue id */
int msqid;
/* A flag indicating that it's time to exit */
bool timeToExit = false;
/* The number of threads that have exited */
int numExitedThreads = 0;
/* The ids that yet to be looked up */
list<int> idsToLookUpList;
/* The mutex protecting idsToLookUpList */
pthread_mutex_t idsToLookUpListMutex = PTHREAD_MUTEX_INITIALIZER;
void ctrlCSignal(int signal)
{
cout << "pressed ctrl c\n\n\n";
/* Free system V resources */
exit(0);
}
/**
* Prototype for createInserterThreads
*/
void createInserterThreads();
/**
* A prototype for adding new records.
*/
void* addNewRecords(void* arg);
void* addNewRecords2(void* arg){
while(true){
signal(SIGINT, ctrlCSignal);
addNewRecords(NULL);
}
}
/**
* Deallocated the message queue
* @param sig - the signal
*/
void cleanUp(int sig)
{
/* Finally, deallocate the queue */
if(msgctl(msqid, IPC_RMID, NULL) < 0)
{
perror("msgctl");
}
/* It's time to exit */
timeToExit = true;
}
/**
* Sends the message over the message queue
* @param msqid - the message queue id
* @param rec - the record to send
*/
void sendRecord(const int& msqid, const record& rec)
{
/**
* Convert the record to message
*/
/* The message to send */
message msg;
/* Copy fields from the record into the message queue */
msg.messageType = SERVER_TO_CLIENT_MSG;
msg.id = rec.id;
strncpy(msg.firstName, rec.firstName.c_str(), MAX_NAME_LEN);
strncpy(msg.lastName, rec.lastName.c_str(), MAX_NAME_LEN);
/* Send the message */
sendMessage(msqid, msg);
}
/**
* Prints the hash table
*/
void printHashTable()
{
/* Go through the hash table */
for(vector<hashTableCell>::const_iterator hashIt = hashTable.begin();
hashIt != hashTable.end(); ++hashIt)
{
/* Go through the list at each hash location */
for(list<record>::const_iterator lIt = hashIt->recordList.begin();
lIt != hashIt->recordList.end(); ++lIt)
{
fprintf(stderr, "%d-%s-%s-%d\n", lIt->id,
lIt->firstName.c_str(),
lIt->lastName.c_str(),
lIt->id % NUMBER_OF_HASH_CELLS
);
}
}
}
/**
* Adds a record to hashtable
* @param rec - the record to add
*/
void addToHashTable(const record& rec)
{
/**
* TODO: Lock the mutex associated with the cell
*/
int modNum = rec.id%NUMBER_OF_HASH_CELLS;
hashTable.at(modNum).lockCell();
/* TODO: Add the record to the cell */
hashTable.at(modNum).recordList.push_back(rec);
/**
* TODO: release mutex associated with the cell
*/
hashTable.at(modNum).unlockCell();
}
/**
* Adds a record to hashtable
* @param id the id of the record to retrieve
* @return - the record from hashtable if exists;
* otherwise returns a record with id field set to -1
*/
record getHashTableRecord(const int& id)
{
/* The data structure to hold the record */
record rec;
/* TODO: Find the cell containing the record with the specificied ID. Search
* the linklist of records in that cell and see if its ID matches. If so, return that
* record. If not, return a record with ID of -1 indicating that no such record is present.
* PLEASE REMEMBER: YOU NEED TO AVOID RACE CONDITIONS ON THE RECORDS IN THE SAME CELL.
* You will need to lock the appropriate cell mutex.
*/
int mod = id%NUMBER_OF_HASH_CELLS;
hashTable.at(mod).lockCell();
list<record>::const_iterator lIt = hashTable.at(mod).recordList.begin();
while(lIt!=hashTable.at(mod).recordList.end()){
if(lIt->id==id){
rec.firstName = lIt->firstName;
rec.id = lIt->id;
rec.lastName = lIt->lastName;
hashTable.at(mod).unlockCell();
return rec;
}
++lIt;
}
//cant find record return -1
rec.id = -1;
hashTable.at(mod).unlockCell();
return rec;
}
/**
* Loads the database into the hashtable
* @param fileName - the file name
* @return - the number of records left.
*/
int populateHashTable(const string& fileName)
{
/* The record */
record rec;
/* Open the file */
ifstream dbFile(fileName.c_str());
/* Is the file open */
if(!dbFile.is_open())
{
fprintf(stderr, "Could not open file %s\n", fileName.c_str());
exit(-1);
}
/* Read the entire file */
while(!dbFile.eof())
{
/* Read the id */
dbFile >> rec.id;
/* Make sure we did not hit the EOF */
if(!dbFile.eof())
{
/* Read the first name and last name */
dbFile >> rec.firstName >> rec.lastName;
/* Add to hash table */
addToHashTable(rec);
}
}
/* Close the file */
dbFile.close();
}
/**
* Gets ids to process from work list
* @return - the id of record to look up, or
* -1 if there is no work
*/
int getIdsToLookUp()
{
/* The id of the looked up record */
int id = -1;
/* TODO: Check if idsToLookUpList (see global declaration) contains any
* requests (i.e., ids of requested records). If so, then remove that ID and
* return it. Otherwise, return -1 indicating that the queue is empty. Please
* remember that multiple threads can access this queue. Please make sure that
* there are no race conditions. You can use the globally declared mutex
* idsToLookUpListMutex. If you use a different mutex, please make sure you
* use the same mutex in addIdsToLookUp (or face race conditions).
*/
if (pthread_mutex_lock(&idsToLookUpListMutex) != 0){
perror("lock");
exit(-1);
}
if (!idsToLookUpList.empty()){
id = idsToLookUpList.front();
idsToLookUpList.pop_front();
}
if (pthread_mutex_unlock(&idsToLookUpListMutex) != 0){
perror("unlock");
exit(-1);
}
return id;
}
/**
* Add id of record to look up
* @param id - the id to process
*/
void addIdsToLookUp(const int& id)
{
/* TODO: Add the id of the requested record to the idsToLookUpList (globally declared).
* Please make sure there are no race conditions. Protect all accesses using the approach
* described in getIdsToLookUp().
*/
//check if locked using idsToLookUpListMutex
if (pthread_mutex_lock(&idsToLookUpListMutex) !=0){
perror("lock");
exit(-1);
}
idsToLookUpList.push_back(id);
if (pthread_mutex_unlock(&idsToLookUpListMutex) !=0){
perror("unlock");
exit(-1);
}
}
/**
* The thread function where the thread
* services requests
* @param thread argument
*/
void* fetcherThreadFunction(void* arg)
{
/* TODO: The thread should stay in a loop until the user presses
* Ctrl-c. The thread should check if there are any requests on the
* idsToLookUpList (you can use getIdsToLookUp() and if there are look up the
* record in the hashtable (you can use getHashTableRecord()) and send it back
* to the client (you can use sendRecord())
*/
record tempRec;
int tempID;
while (true){
tempID = getIdsToLookUp();
signal(SIGINT, ctrlCSignal);
if (tempID == -1){
}
else {
tempRec = getHashTableRecord(tempID);
cout << tempRec.id << "\t" << tempRec.firstName <<"\t" << tempRec.lastName << endl;
// sendRecord(msqid, tempRec);
}
}
}
/**
* Creates threads that update the database
* with randomly generated records
*/
void createInserterThreads()
{
/* TODO: Create NUM_INSERTERS number of threads that
* will be constantly inserting records to the hash table.
* Threads can use addNewRecords() for that.
*/
for (int i=0; i< NUM_INSERTERS; i++){
if(pthread_create(&threadsIds.at(i), NULL, addNewRecords2, NULL) != 0)
{
perror("pthread_create");
exit(-1);
}
}
for (int i=0; i< NUM_INSERTERS; i++){
if(pthread_join(threadsIds.at(i), NULL) != 0)
{
perror("pthread_create");
exit(-1);
}
}
}
/**
* Creates the specified number of fetcher threads
* @param numFetchers - the number of fetcher threads
*/
void createFetcherThreads(const int& numFetchers)
{
/* TODO: Create numFetchers threads that call fetecherThreadFunction().
* These threads will be responsible for responding to client requests
* with records looked up in the hashtable.
*/
for (int i=0; i< numFetchers; i++){
if(pthread_create(&fetcherThreadIds.at(i), NULL, fetcherThreadFunction, NULL) != 0)
{
perror("thread create");
exit(-1);
}
}
for (int i=0; i< numFetchers; i++){
if(pthread_join(fetcherThreadIds.at(i), NULL) != 0)
{
perror("pthread_create");
exit(-1);
}
}
}
/**
* Called by parent thread to process incoming messages
*/
void processIncomingMessages()
{
/* TODO: This function will be called by the parent thread.
* It will wait to recieve requests from the client on the
* message queue (you can use recvMessage() and message queue was
* already created for you (msqid is the id; see main()).
*/
}
/**
* Generates a random record
* @return - a random record
*/
record generateRandomRecord()
{
/* A record */
record rec;
/* Generate a random id */
rec.id = rand() % NUMBER_OF_HASH_CELLS;
/* Add the fake first name and last name */
rec.firstName = "Random";
rec.lastName = "Record";
return rec;
}
/**
* Threads inserting new records to the database
* @param arg - some argument (unused)
*/
void* addNewRecords(void* arg)
{
/* TODO: This function will be called by the threads responsible for
* performing random generation and insertion of new records into the
* hashtable.
*/
int num = 1000;
int id = rand()%num + 1;
record rec;
rec.id = id;
rec.firstName = "?";
rec.lastName = "?";
int mod = id%NUMBER_OF_HASH_CELLS;
//lock cell
hashTable.at(mod).lockCell();
//get size of recordList
int size = hashTable.at(mod).recordList.size();
//init walker
list<record>::const_iterator lIt = hashTable.at(mod).recordList.begin();
//iterate through recordList
while(lIt != hashTable.at(mod).recordList.end()){
//if duplicate dont insert and then unlock
if (lIt->id == id){
hashTable.at(mod).unlockCell();
return 0;
}
++lIt;
}
hashTable.at(mod).recordList.push_back(rec);
hashTable.at(mod).unlockCell();
}
/**
* Tells all threads to exit and waits until they exit
* @param numThreads - the number of threads that retrieve
* records from the hash table.
*/
void waitForAllThreadsToExit()
{
/* TODO: Wait for all inserter threads and fetcher threads to join */
}
int main(int argc, char** argv)
{
srand(time(NULL));
/**
* Check the command line
*/
if(argc < 2)
{
fprintf(stderr, "USAGE: %s <DATABASE FILE NAME>\n", argv[0]);
exit(-1);
}
/* Install a signal handler */
if(signal(SIGINT, cleanUp) == SIG_ERR)
{
perror("signal");
exit(-1);
}
/* Populate the hash table */
populateHashTable(argv[1]);
createInserterThreads();
printHashTable();
return 0;
//testing add
cout << "testing add 500\n\n\n";
// for (int i=0; i< 500; i++){
// int num = 1000;
// int* ptr = #
// addNewRecords(ptr);
// }
// printHashTable();
cout << "testing retrieve\n\n\n\n\n";
for (int i=0; i< 500; i++){
int num = rand()%1000 +1;
record rec1;
rec1 = getHashTableRecord(num);
if(rec1.id!=-1){
cout << rec1.id << "\t" << rec1.firstName << "\t" << rec1.lastName << endl;
}
else {
cout << "record not found\n";
}
}
createInserterThreads();
printHashTable();
return 0;
/* Get the number of threads */
numThreads = atoi(argv[2]);
/* Use a random file and a random character to generate
* a unique key. Same parameters to this function will
* always generate the same value. This is how multiple
* processes can connect to the same queue.
*/
key_t key = ftok("/bin/ls", 'O');
/* Connect to the message queue */
msqid = createMessageQueue(key);
/* Instantiate a message buffer */
message msg;
/* Create the threads */
createFetcherThreads(numThreads);
/* Create the inserter threads */
createInserterThreads();
/* Process incoming requests */
processIncomingMessages();
fprintf(stderr, "Here");
/* Terminate all threads */
waitForAllThreadsToExit();
/* Free the mutex used for protecting the work queue */
if(pthread_mutex_destroy(&idsToLookUpListMutex) != 0)
perror("pthread_mutex_destroy");
return 0;
}