-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventqueue.c
More file actions
149 lines (118 loc) · 4.11 KB
/
eventqueue.c
File metadata and controls
149 lines (118 loc) · 4.11 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
#include <stdlib.h>
#include "eventqueue.h"
#include "event.h"
#include "process.h"
EventQueue* initEventQueue() {
EventQueue* newEventQueue = (EventQueue*) malloc(sizeof(EventQueue));
newEventQueue->firstEvent = NULL;
return newEventQueue;
}
void insertIntoEventQueue(EventQueue* curEventQueue, Event event, int time) {
CurrentEvent* newEvent = (CurrentEvent*) malloc(sizeof(CurrentEvent));
newEvent->event = event;
newEvent->eventTime = time;
CurrentEvent** aux = &curEventQueue->firstEvent;
while (*aux != NULL && ((*aux)->eventTime < time ||
((*aux)->eventTime == time && compareEvent((*aux)->event, event)))) {
aux = &(*aux)->nextEvent;
}
newEvent->nextEvent = *aux;
*aux = newEvent;
}
bool nextEvent(Event *event, int *time, EventQueue *queue){
if (queue->firstEvent == NULL) return false;
*event = queue->firstEvent->event;
*time = queue->firstEvent->eventTime;
return true;
}
void dropEvent(EventQueue* queue){
if (queue->firstEvent == NULL) return;
CurrentEvent *event = queue->firstEvent;
queue->firstEvent = event->nextEvent;
free(event);
}
// Reads a line of input in "buffer" and parses it, reading the process entry time
// (written to *entryTime), separated by a colon from a comma-separated list of integers
// corresponding to cpu times and IO types (PRINTER, TAPE, or DISK),
// building a chain of ProcessNodes that is written to "node".
bool parseNodes(char* buffer, int* entryTime, ProcessNode** node){
// First read the process entry time, which is separated from the rest
// of the values with a colon
int n; // Number of characters read
if (sscanf(buffer, "%d%n", entryTime, &n) != 1) return false;
buffer += n;
while (buffer[0] == ' ') buffer++; // Skip whitespace
if (buffer[0] != ':') return false;
buffer++;
while (buffer[0] == ' ') buffer++; // Skip whitespace
// Then read the list of CPU/IO actions the process
while (true){
// Skip whitespace
while (buffer[0] == ' ') buffer++;
// Check if we are done
if (buffer[0] == '\n' || buffer[0] == '\0') break;
int cpuTime;
int charsRead;
int read = sscanf(buffer, "%d%n", &cpuTime, &charsRead);
if (read != 1) cpuTime = 0;
else buffer += charsRead;
// cpuTime must be non-negative
if (cpuTime < 0) return false;
if (buffer[0] == ','){
buffer++;
// Skip whitespace
while (buffer[0] == ' ') buffer++;
}
IOType type;
char* c = strchr(buffer, ',');
if (c == NULL) c = strchr(buffer, '\n');
char* nextSpace = strchr(buffer, ' ');
if (nextSpace != NULL && nextSpace < c) c = nextSpace;
int n = c - buffer; // Distance to the comma
if (n == 0){
type = NONE;
} else if (strncmp(buffer, "PRINTER", n) == 0){
type = PRINTER;
} else if (strncmp(buffer, "DISK", n) == 0) {
type = DISK;
} else if (strncmp(buffer, "TAPE", n) == 0) {
type = TAPE;
} else {
// Could not read IO type
printf("Could not read IO type\n");
return false;
}
// Skip whitespace
while (buffer[0] == ' ') buffer++;
// Add node to chain
*node = malloc(sizeof(ProcessNode));
(*node)->CPUTime = cpuTime;
(*node)->IORequest = type;
// Focus on the next pointer
node = &(*node)->nextNode;
// Advance the buffer
buffer = c+1;
}
// End the process node chain and return
*node = NULL;
return true;
}
// Reads the processes listed in the file, in CSV format, and adds NEW_PROCESS
// events to the EventQueue accordingly. Returns true if succeeded.
bool newProcessEventsFromFile(FILE* file, EventQueue* e){
int nextPid = 1;
const int MAX_LINE_LENGTH = 10000;
char text_buffer[MAX_LINE_LENGTH];
// Loop through all lines of the file
while (true) {
// Attempt to read a line (NULL indicates EOF)
if (fgets(text_buffer, MAX_LINE_LENGTH, file) == NULL) break;
Process *p = createEmptyProcess(nextPid++);
// Parse ProcessNode chain, as well as the process entry time
int entryTime;
bool ok = parseNodes(text_buffer, &entryTime, &p->firstNode);
if (!ok) return false;
insertIntoEventQueue(e, newProcess(p), entryTime);
}
return true;
}