-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperatingSystemBase.c
343 lines (288 loc) · 11.3 KB
/
OperatingSystemBase.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
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
#include "OperatingSystemBase.h"
#include "OperatingSystem.h"
#include "Processor.h"
#include "Buses.h"
#include "Clock.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
// Code that students should NOT touch
// Functions prototypes
int OperatingSystem_ObtainPositiveNumberOfFile(FILE *);
void OperatingSystem_PrepareDaemons(int);
// int OperatingSystem_lineBeginsWithANumber(char *);
void OperatingSystem_PrintSleepingProcessQueue();
void OperatingSystem_PrintExecutingProcessInformation();
void OperatingSystem_PrintProcessTableAssociation();
extern int initialPID;
extern int baseDaemonsInProgramList;
extern int executingProcessID;
#ifdef SLEEPINGQUEUE
extern char * queueNames [];
#endif
// Search for a free entry in the process table. The index of the selected entry
// will be used as the process identifier
int OperatingSystem_ObtainAnEntryInTheProcessTable() {
int orig=initialPID%PROCESSTABLEMAXSIZE;
int index=0;
int entry;
while (index<PROCESSTABLEMAXSIZE) {
entry = (orig+index)%PROCESSTABLEMAXSIZE;
if (processTable[entry].busy==0) {
return entry;
}
else {
if (++index==PROCESSTABLEMAXSIZE) { // if no entries free, remove zombie proceses
int i;
for (i=0;i<PROCESSTABLEMAXSIZE;i++)
if (processTable[i].busy && (processTable[i].state==EXIT)) {
OperatingSystem_ShowTime(SYSPROC);
ComputerSystem_DebugMessage(79,SYSPROC
,i,programList[processTable[i].programListIndex]->executableName
,processTable[i].processSize
,processTable[i].initialPhysicalAddress);
processTable[i].busy=0;
index=0; // New search after liberation of PCB
}
}
}
}
return NOFREEENTRY;
}
// Returns the size of the program, stored in the program file
int OperatingSystem_ObtainProgramSize(FILE *programFile) {
int programSize;
programSize = OperatingSystem_ObtainPositiveNumberOfFile(programFile);
return programSize;
}
// Returns the priority of the program, stored in the program file
int OperatingSystem_ObtainPriority(FILE *programFile) {
int processPriority;
processPriority = OperatingSystem_ObtainPositiveNumberOfFile(programFile);
return processPriority;
}
// Function that processes the contents of the file named by the first argument
// in order to load it in main memory from the address given as the second
// argument
// IT IS NOT NECESSARY TO COMPLETELY UNDERSTAND THIS FUNCTION
int OperatingSystem_LoadProgram(FILE *programFile, int initialAddress, int size) {
char lineRead[MAXLINELENGTH];
char *token0, *token1, *token2;
BUSDATACELL data;
int nbInstructions = 0;
int opCode;
int op1, op2;
Processor_SetMAR(initialAddress);
while (fgets(lineRead, MAXLINELENGTH, programFile) != NULL) {
// REMARK: if lineRead is greater than MAXLINELENGTH in number of characters, the program
// loading does not work
opCode=op1=op2=0;
token0=strtok(lineRead," \n\r\t");
if (token0!=NULL && token0[0]!='/' && token0[0]!='\n' && token0[0]!='\r') {
// I have an instruction with, at least, an operation code
opCode=Processor_ToInstruction(token0);
token1=strtok(NULL," ");
if (token1!=NULL && token1[0]!='/') {
// I have an instruction with, at least, an operand
op1=atoi(token1);
token2=strtok(NULL," ");
if (token2!=NULL && token2[0]!='/') {
// The read line is similar to 'ADD 2 3 //coment'
// I have an instruction with two operands
op2=atoi(token2);
}
}
// More instructions than size...
if (++nbInstructions > size){
return TOOBIGPROCESS;
}
data.cell=Processor_Encode(opCode,op1,op2);
Processor_SetMBR(&data);
// Send data to main memory using the system buses
Buses_write_DataBus_From_To(CPU, MAINMEMORY);
Buses_write_AddressBus_From_To(CPU, MAINMEMORY);
// Tell the main memory controller to write
Processor_SetCTRL(CTRLWRITE);
Buses_write_ControlBus_From_To(CPU,MAINMEMORY);
Processor_SetMAR(Processor_GetMAR()+1);
}
}
return SUCCESS;
}
// Auxiliar for check that line begins with positive number
int OperatingSystem_lineBeginsWithAPositiveNumber(char * line) {
int i;
for (i=0; i<strlen(line) && line[i]==' '; i++); // Don't consider blank spaces
// If is there a digit number...
if (i<strlen(line) && isdigit(line[i]))
// It's a positive number
return 1;
else
return 0;
}
int OperatingSystem_ObtainPositiveNumberOfFile(FILE *programFile) {
char lineRead[MAXLINELENGTH];
int isComment=1;
int value=PROGRAMNOTVALID;
// Read the first number as the size of the program. Skip all comments.
while (isComment==1) {
if (fgets(lineRead, MAXLINELENGTH, programFile) == NULL) {
return PROGRAMNOTVALID;
}
else
if (lineRead[0]!='/' && lineRead[0]!='\n' && lineRead[0]!='\r') { // Line IS NOT a comment and IS NOT empty
isComment=0;
if (OperatingSystem_lineBeginsWithAPositiveNumber(lineRead)) {
value=atoi(strtok(lineRead," "));
}
else {
return PROGRAMNOTVALID;
}
}
}
// Only sizes above 0 are allowed
if (value<=0){
return PROGRAMNOTVALID;
}
return value;
}
// Daemon processes are system processes, that is, they work together with the OS.
// The System Idle Process uses the CPU whenever a user process is able to use it
void OperatingSystem_PrepareDaemons(int programListDaemonsBase) {
// Include a entry for SystemIdleProcess at 0 position
programList[0]=(PROGRAMS_DATA *) malloc(sizeof(PROGRAMS_DATA));
programList[0]->executableName="SystemIdleProcess";
programList[0]->arrivalTime=0;
programList[0]->type=DAEMONPROGRAM; // daemon program
sipID=initialPID%PROCESSTABLEMAXSIZE; // first PID for sipID
// Preparing aditionals daemons here
// index for aditionals daemons program in programList
baseDaemonsInProgramList=OperatingSystem_PrepareTeachersDaemons(programListDaemonsBase);
baseDaemonsInProgramList=OperatingSystem_PrepareStudentsDaemons(baseDaemonsInProgramList);
}
int OperatingSystem_PrepareTeachersDaemons(int programListDaemonsBase){
FILE *daemonsFile;
char lineRead[MAXLINELENGTH];
PROGRAMS_DATA *progData;
char *name, *arrivalTime;
int time;
daemonsFile= fopen("teachersDaemons", "r");
// Check if programFile exists
if (daemonsFile==NULL)
return programListDaemonsBase;
while (fgets(lineRead, MAXLINELENGTH, daemonsFile) != NULL
&& programListDaemonsBase<PROGRAMSMAXNUMBER) {
name=strtok(lineRead,",");
if (name==NULL){
continue;
}
arrivalTime=strtok(NULL,",");
if (arrivalTime==NULL
|| sscanf(arrivalTime,"%d",&time)==0)
time=0;
progData=(PROGRAMS_DATA *) malloc(sizeof(PROGRAMS_DATA));
progData->executableName = (char *) malloc((strlen(name)+1)*sizeof(char));
strcpy(progData->executableName,name);
progData->arrivalTime=time;
progData->type=DAEMONPROGRAM;
programList[programListDaemonsBase++]=progData;
}
return programListDaemonsBase;
}
void OperatingSystem_ReadyToShutdown(){
int sipIdPCtoShutdown=processTable[sipID].initialPhysicalAddress+processTable[sipID].processSize-1;
// Simulation must finish (done by modifying the PC of the System Idle Process so it points to its 'TRAP 3' instruction,
// located at the last memory position used by that process, and dispatching sipId (next ShortTermSheduled)
if (executingProcessID==sipID)
Processor_CopyInSystemStack(MAINMEMORYSIZE-1, sipIdPCtoShutdown);
else
processTable[sipID].copyOfPCRegister=sipIdPCtoShutdown;
}
void OperatingSystem_TerminatingSIP() {
processTable[sipID].copyOfPCRegister=OS_address_base+1;
Processor_CopyInSystemStack(MAINMEMORYSIZE-1,processTable[sipID].copyOfPCRegister);
processTable[sipID].copyOfPSWRegister|= ((unsigned int) 1) << INTERRUPT_MASKED_BIT;
Processor_CopyInSystemStack(MAINMEMORYSIZE-2,processTable[sipID].copyOfPSWRegister);
executingProcessID=NOPROCESS;
}
// Show time messages
void OperatingSystem_ShowTime(char section) {
ComputerSystem_DebugMessage(100,section,Processor_PSW_BitState(EXECUTION_MODE_BIT)?"\t":"");
ComputerSystem_DebugMessage(Processor_PSW_BitState(EXECUTION_MODE_BIT)?95:94,section,Clock_GetTime());
}
// Show general status
void OperatingSystem_PrintStatus(){
OperatingSystem_PrintExecutingProcessInformation(); // Show executing process information
OperatingSystem_PrintReadyToRunQueue(); // Show Ready to run queues implemented for students
OperatingSystem_PrintSleepingProcessQueue(); // Show Sleeping process queue
OperatingSystem_PrintProcessTableAssociation(); // Show PID-Program's name association
ComputerSystem_PrintArrivalTimeQueue(); // Show arrival queue of programs
}
// Show Executing process information
void OperatingSystem_PrintExecutingProcessInformation(){
#ifdef SLEEPINGQUEUE
OperatingSystem_ShowTime(SHORTTERMSCHEDULE);
if (executingProcessID>=0)
// Show message "Running Process Information:\n\t\t[PID: executingProcessID, Priority: priority, WakeUp: whenToWakeUp, Queue: queueID]\n"
ComputerSystem_DebugMessage(74,SHORTTERMSCHEDULE,
executingProcessID,processTable[executingProcessID].priority,processTable[executingProcessID].whenToWakeUp
,queueNames[processTable[executingProcessID].queueID]);
else
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,"Running Process Information:\n\t\t[--- No running process ---]\n");
#endif
}
// Show SleepingProcessQueue
void OperatingSystem_PrintSleepingProcessQueue(){
#ifdef SLEEPINGQUEUE
int i;
OperatingSystem_ShowTime(SHORTTERMSCHEDULE);
// Show message "SLEEPING Queue:\n\t\t");
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,"SLEEPING Queue:\n\t\t");
if (numberOfSleepingProcesses>0)
for (i=0; i< numberOfSleepingProcesses; i++) {
// Show message [PID, priority, whenToWakeUp]
ComputerSystem_DebugMessage(75,SHORTTERMSCHEDULE
, sleepingProcessesQueue[i].info
, processTable[sleepingProcessesQueue[i].info].priority
, processTable[sleepingProcessesQueue[i].info].whenToWakeUp);
if (i<numberOfSleepingProcesses-1)
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,", ");
}
else
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,"[--- empty queue ---]");
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,"\n");
#endif
}
void OperatingSystem_PrintProcessTableAssociation() {
int i;
OperatingSystem_ShowTime(SHORTTERMSCHEDULE);
// Show message "Process table association with program's name:");
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,"PID association with program's name:\n");
for (i=0; i< PROCESSTABLEMAXSIZE; i++) {
if (processTable[i].busy) {
// Show message PID -> program's name\n
ComputerSystem_DebugMessage(76,SHORTTERMSCHEDULE,i,programList[processTable[i].programListIndex]->executableName);
}
}
}
// This function returns:
// EMPTYQUEUE (-1) if no programs in arrivalTimeQueue
// YES (1) if any program arrivalTime is now
// NO (0) else
// considered by the LTS to create processes at the current time
int OperatingSystem_IsThereANewProgram() {
#ifdef ARRIVALQUEUE
int currentTime;
int programArrivalTime;
int indexInProgramList = Heap_getFirst(arrivalTimeQueue,numberOfProgramsInArrivalTimeQueue);
if (indexInProgramList < 0)
return EMPTYQUEUE; // No new programs in command line list of programs
// Get the current simulation time
currentTime = Clock_GetTime();
// Get arrivalTime of next program
programArrivalTime = programList[indexInProgramList]->arrivalTime;
if (programArrivalTime <= currentTime)
return YES; // There'is new program to start
#endif
return NO; // No program in current time
}