-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLoader.c
More file actions
123 lines (102 loc) · 3.16 KB
/
FileLoader.c
File metadata and controls
123 lines (102 loc) · 3.16 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
//=======================================
// Multi-level Host Dispatcher
//
// EECE 315 - Project 4
// Group 11
//
// Siddhartha Balasubramanian
// Nabil Lathiff
// Mark Duppenthaler
// Michael Peterson
//=======================================
//=======================================
// FileLoader.c
//=======================================
// FileLoader module
//
// Loads the job lists from a specified
// file.
//
#include <stdio.h>
#include <stdlib.h>
#include "SystemDefs.h"
#include "FileLoader.h"
#include "List.h"
#include "ProcMan.h"
FILEERR FileExists(char* filename)
// Check if the file exists
{
FILE * inputfile = fopen( filename, "r" );
if(!inputfile)
return FILEDOESNOTEXIST;
fclose(inputfile);
return FILESUCCESS;
}
List* FileLoadJobs(char* filename)
// Load all the jobs from the file list
// This function also validates each job to check if the resources it requires
// can be provided by this machine
// If it cannot, that job is not admitted into the joblist
{
ProcInfo procInfo;
List* jobList;
int procNum = 1;
FILE * inputfile = fopen( filename, "r" );
if(!inputfile)
return NULL;
if(!(jobList = ListCreate(sizeof(ProcInfo))))
{
fclose(inputfile);
return NULL;
}
// Read the file here and add the processes to the job list
while( feof(inputfile) == 0 )
{
if(fscanf(inputfile, "%d, %d, %d, %d, %d, %d, %d, %d",
&(procInfo.arrTime),
&(procInfo.pri),
&(procInfo.cpuTime),
&(procInfo.memSize),
&(procInfo.numPrint),
&(procInfo.numScan),
&(procInfo.numModem),
&(procInfo.numCD)) != 8)
// If the scanf cannot scan the 8 required parameters for this job, go to the next job
// Ideally, if the input file is according to spec, this will never happen.
continue;
// Validate all the parameters to check if the process can execute on this machine
// Note, this eliminates process that can never run on this computer.
// Eg: a process requiring 3 CD drives on a machine that has only 2 will be eliminated
if(procInfo.arrTime < 0)
continue;
if(procInfo.cpuTime <= 0)
continue;
if(procInfo.pri < 0 || procInfo.pri > (SYSTEMMLFQLEVELS - 1))
continue;
if(procInfo.memSize < 0 || procInfo.memSize > (SYSTEMMEMSIZE - SYSTEMRTRESERVEMEM))
continue;
if(procInfo.numPrint < 0 || procInfo.numPrint > SYSTEMNUMPRINT)
continue;
if(procInfo.numScan < 0 || procInfo.numScan > SYSTEMNUMSCAN)
continue;
if(procInfo.numModem < 0 || procInfo.numModem > SYSTEMNUMMODEM)
continue;
if(procInfo.numCD < 0 || procInfo.numCD > SYSTEMNUMCD)
continue;
// Assign the process a number
procInfo.num = procNum++;
// Mark the process as not running
procInfo.pid = 0;
procInfo.status = PROCNOTRUNNING;
// Check if we can add this process to the job list
if(!ProcManLinkNode(ListAppend(jobList,(&procInfo))))
{
// If we cannot, close the file and clean up
fclose(inputfile);
ListDelete(jobList);
return NULL;
}
}
fclose(inputfile);
return jobList;
}