-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_random.c
70 lines (64 loc) · 1.77 KB
/
generate_random.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include "tuple.h"
unsigned int randval()
{
unsigned int randval;
FILE *f;
f = fopen("/dev/random", "r");
fread(&randval, sizeof(randval), 1, f);
fclose(f);
return randval;
}
int main()
{
srand(time(NULL));
FILE *queueFile = fopen("operation_queue.txt", "a");
FILE *waitingQueue = fopen("waiting_queue", "a");
if (queueFile == NULL)
{
perror("Error opening file");
return 1;
}
if (waitingQueue == NULL)
{
perror("Error opening file");
return 1;
}
for (int i = 0; i < 20; i++)
{
int studentId = randval() % 100 + 1;
int operationId = randval() % 4;
tuple temp = {};
temp.stdID = studentId;
temp.opID = operationId;
strcpy(temp.type, "random");
if (operationId == 0)
{
fwrite(&temp, 1, sizeof(tuple), waitingQueue);
fprintf(queueFile, "(random, %d, %ld)\n", operationId, time(NULL));
}
else if (operationId == 2)
{
int hostelRoom = randval() % 100 + 101;
temp.roomNo = hostelRoom;
fwrite(&temp, 1, sizeof(tuple), waitingQueue);
fprintf(queueFile, "(random, %d, %d, %d, %ld)\n", operationId, studentId, hostelRoom, time(NULL));
}
else
{
fwrite(&temp, 1, sizeof(tuple), waitingQueue);
fprintf(queueFile, "(random, %d, %d, %ld)\n", operationId, studentId, time(NULL));
}
double randomInterval = randval()%3 + 1;
fflush(queueFile);
fflush(waitingQueue);
sleep(randomInterval);
}
fclose(queueFile);
fclose(waitingQueue);
return 0;
}