Skip to content

Commit 15d1c16

Browse files
committed
Archivos bien de la V3 y ej 0
1 parent b8c5316 commit 15d1c16

12 files changed

+146
-41
lines changed

V3/ComputerSystem.c

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ PROGRAMS_DATA *programList[PROGRAMSMAXNUMBER];
1616

1717
// Students messages
1818
char STUDENT_MESSAGES_FILE[MAXIMUMLENGTH]="messagesSTD.txt";
19+
20+
// V3 Ej 0a
21+
heapItem arrivalTimeQueue[PROGRAMSMAXNUMBER];
22+
int numberOfProgramsInArrivalTimeQueue=0;
1923

2024
// Powers on of the Computer System.
2125
void ComputerSystem_PowerOn(int argc, char *argv[], int paramIndex) {

V3/ComputerSystem.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef COMPUTERSYSTEM_H
22
#define COMPUTERSYSTEM_H
33

4+
#define ARRIVALQUEUE // V3 Ej0b
5+
46
#include "Simulator.h"
57
#include "ComputerSystemBase.h"
68

@@ -27,6 +29,8 @@ void ComputerSystem_PrintProgramList();
2729
#define ERROR 'e' // Error messages
2830
#define CLOCK 'c' // Clock time messages
2931

32+
33+
3034
// Basic data to collect about every program to be created
3135
// User programs specified in the command line: name of the file, the time of its arrival time
3236
// to the system (0, by default), and type USERPROGRAM

V3/ComputerSystemBase.c

+39
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ int intervalBetweenInterrupts = DEFAULT_INTERVAL_BETWEEN_INTERRUPTS; // Default
2929
// Only one colour messages. Set to 1 for more colours checking uppercase in debugLevel
3030
int COLOURED = 0 ;
3131

32+
#ifdef ARRIVALQUEUE
33+
char * typeProgramNames []={"USER","DAEMONS"};
34+
extern heapItem arrivalTimeQueue[];
35+
extern int numberOfProgramsInArrivalTimeQueue;
36+
#endif
37+
3238
// Fill in the array named userProgramsList with the information given
3339
// by the user in the command line
3440
// IT IS NOT NECESSARY TO COMPLETELY UNDERSTAND THIS FUNCTION
@@ -205,3 +211,36 @@ void ComputerSystem_DebugMessage(int msgNo, char section, ...) {
205211
void ComputerSystem_ShowTime(char section) {
206212
ComputerSystem_DebugMessage(Processor_PSW_BitState(EXECUTION_MODE_BIT)?95:94,section,Clock_GetTime());
207213
}
214+
215+
// Fill ArrivalTimeQueue heap with user program from parameters and daemons
216+
void ComputerSystem_FillInArrivalTimeQueue() {
217+
#ifdef ARRIVALQUEUE
218+
219+
int arrivalIndex = 0;
220+
221+
while (programList[arrivalIndex]!=NULL && arrivalIndex<PROGRAMSMAXNUMBER) {
222+
Heap_add(arrivalIndex,arrivalTimeQueue,QUEUE_ARRIVAL,&arrivalIndex,PROGRAMSMAXNUMBER);
223+
}
224+
numberOfProgramsInArrivalTimeQueue=arrivalIndex;
225+
#endif
226+
}
227+
228+
// Print arrivalTiemQueue program information
229+
void ComputerSystem_PrintArrivalTimeQueue(){
230+
#ifdef ARRIVALQUEUE
231+
int i;
232+
233+
if (numberOfProgramsInArrivalTimeQueue>0) {
234+
OperatingSystem_ShowTime(LONGTERMSCHEDULE);
235+
// Show message "Arrival Time Queue: "
236+
ComputerSystem_DebugMessage(100,LONGTERMSCHEDULE,"Arrival Time Queue:\n");
237+
for (i=0; i< numberOfProgramsInArrivalTimeQueue; i++) {
238+
// Show message [executableName,arrivalTime]
239+
ComputerSystem_DebugMessage(78,LONGTERMSCHEDULE,programList[arrivalTimeQueue[i].info]->executableName,
240+
programList[arrivalTimeQueue[i].info]->arrivalTime,
241+
typeProgramNames[programList[arrivalTimeQueue[i].info]->type]);
242+
}
243+
}
244+
#endif
245+
}
246+

V3/ComputerSystemBase.h

+10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22
#define COMPUTERSYSTEMBASE_H
33

44
#include "ComputerSystem.h"
5+
#include "Heap.h"
56

67
// Functions prototypes
78
int ComputerSystem_ObtainProgramList(int , char *[], int);
89
void ComputerSystem_DebugMessage(int, char , ...);
910
void ComputerSystem_ShowTime(char);
11+
void ComputerSystem_FillInArrivalTimeQueue();
12+
void ComputerSystem_PrintArrivalTimeQueue();
1013

1114
// This "extern" declarations enables other source code files to gain access to the variables
1215
extern char defaultDebugLevel[];
1316
extern int intervalBetweenInterrupts;
1417

18+
extern int endSimulationTime; // For end simulation forced by time
19+
20+
#ifdef ARRIVALQUEUE
21+
extern int numberOfProgramsInArrivalTimeQueue;
22+
extern heapItem arrivalTimeQueue[];
23+
#endif
24+
1525
#define DEFAULT_INTERVAL_BETWEEN_INTERRUPTS 5
1626

1727
#endif

V3/Heap.c

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ int Heap_compare_wakeup(int value1, int value2) {
109109
#endif
110110
}
111111

112+
// Auxiliary for arrival-time comparations
113+
int Heap_compare_arrival(int value1, int value2) {
114+
return programList[value2]->arrivalTime - programList[value1]->arrivalTime;
115+
}
116+
112117
// Auxiliary for assert-time comparations
113118
int Heap_compare_assertsTime(int value1, int value2) {
114119
return asserts[value2].time - asserts[value1].time;
@@ -124,6 +129,9 @@ int Heap_compare(heapItem value1, heapItem value2, int queueType) {
124129
case QUEUE_PRIORITY:
125130
primaryKey= Heap_compare_priority(value1.info, value2.info);
126131
break;
132+
case QUEUE_ARRIVAL:
133+
primaryKey= Heap_compare_arrival(value1.info, value2.info);
134+
break;
127135
case QUEUE_ASSERTS:
128136
primaryKey= Heap_compare_assertsTime(value1.info, value2.info);
129137
break;

V3/Heap.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#define QUEUE_WAKEUP 0
55
#define QUEUE_PRIORITY 1
6+
#define QUEUE_ARRIVAL 2
67
#define QUEUE_ASSERTS 3
78

89
typedef struct {
@@ -14,7 +15,7 @@ typedef struct {
1415
// Implements the extraction operation (the element with the highest priority).
1516
// Parameters are:
1617
// heap: the corresponding queue: readyToRun, asserts, UserProgramList or sleepingQueue
17-
// queueType: if sleeping queue, QUEUE_WAKEUP; if ready to run queue, QUEUE_PRIORITY; if asserts QUEUE_ASSERTS;
18+
// queueType: if sleeping queue, QUEUE_WAKEUP; if ready to run queue, QUEUE_PRIORITY; if asserts QUEUE_ASSERTS; if userProgramList, QUEUE_ARRIVAL
1819
// numElem: number of current elements inside the queue, if successful is decremented by one
1920
// Returns: the item with the highest priority in the queue, if everything went ok
2021
int Heap_poll(heapItem[], int, int*);
@@ -23,7 +24,7 @@ int Heap_poll(heapItem[], int, int*);
2324
// Parameters are:
2425
// info: item to be inserted
2526
// heap: the corresponding queue: readyToRun, asserts, UserProgramList or sleepingQueue
26-
// queueType: if sleeping queue, QUEUE_WAKEUP; if ready to run queue, QUEUE_PRIORITY; if asserts QUEUE_ASSERTS;
27+
// queueType: if sleeping queue, QUEUE_WAKEUP; if ready to run queue, QUEUE_PRIORITY; if asserts QUEUE_ASSERTS; if userProgramList, QUEUE_ARRIVAL
2728
// numElem: number of current elements inside the queue, if successful is increased by one
2829
// limit: maximum capacity of the queue
2930
// return 0/-1 ok/fail
@@ -33,7 +34,7 @@ int Heap_add(int, heapItem[], int , int*, int);
3334
// Parameters are:
3435
// Position one
3536
// Position two
36-
// queueType: if sleeping queue, QUEUE_WAKEUP; if ready to run queue, QUEUE_PRIORITY; if asserts QUEUE_ASSERTS;
37+
// queueType: if sleeping queue, QUEUE_WAKEUP; if ready to run queue, QUEUE_PRIORITY; if asserts QUEUE_ASSERTS; if userProgramList, QUEUE_ARRIVAL
3738
int Heap_compare(heapItem, heapItem, int);
3839

3940
// Return top value of heap

V3/OperatingSystem.c

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ void OperatingSystem_Initialize(int daemonsIndex) {
9797
// Include in program list all system daemon processes
9898
OperatingSystem_PrepareDaemons(daemonsIndex);
9999

100+
ComputerSystem_FillInArrivalTimeQueue(); //V3 Ej 0c
101+
102+
OperatingSystem_PrintStatus(); //V3 Ej 0d
103+
100104
// Create all user processes from the information given in the command line
101105
int createdProcesses = OperatingSystem_LongTermScheduler();
102106
//V1 Ej 15

V3/OperatingSystemBase.c

+57-8
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,24 @@ int OperatingSystem_ObtainAnEntryInTheProcessTable() {
3535

3636
while (index<PROCESSTABLEMAXSIZE) {
3737
entry = (orig+index)%PROCESSTABLEMAXSIZE;
38-
if (processTable[entry].busy==0)
38+
if (processTable[entry].busy==0) {
3939
return entry;
40-
else
41-
index++;
40+
}
41+
else {
42+
if (++index==PROCESSTABLEMAXSIZE) { // if no entries free, remove zombie proceses
43+
int i;
44+
for (i=0;i<PROCESSTABLEMAXSIZE;i++)
45+
if (processTable[i].busy && (processTable[i].state==EXIT)) {
46+
OperatingSystem_ShowTime(SYSPROC);
47+
ComputerSystem_DebugMessage(79,SYSPROC
48+
,i,programList[processTable[i].programListIndex]->executableName
49+
,processTable[i].processSize
50+
,processTable[i].initialPhysicalAddress);
51+
processTable[i].busy=0;
52+
index=0; // New search after liberation of PCB
53+
}
54+
}
55+
}
4256
}
4357
return NOFREEENTRY;
4458
}
@@ -215,13 +229,20 @@ int OperatingSystem_PrepareTeachersDaemons(int programListDaemonsBase){
215229

216230

217231
void OperatingSystem_ReadyToShutdown(){
232+
int sipIdPCtoShutdown=processTable[sipID].initialPhysicalAddress+processTable[sipID].processSize-1;
218233
// Simulation must finish (done by modifying the PC of the System Idle Process so it points to its 'TRAP 3' instruction,
219234
// located at the last memory position used by that process, and dispatching sipId (next ShortTermSheduled)
220-
processTable[sipID].copyOfPCRegister=processTable[sipID].initialPhysicalAddress+processTable[sipID].processSize-1;
235+
if (executingProcessID==sipID)
236+
Processor_CopyInSystemStack(MAINMEMORYSIZE-1, sipIdPCtoShutdown);
237+
else
238+
processTable[sipID].copyOfPCRegister=sipIdPCtoShutdown;
221239
}
222240

223241
void OperatingSystem_TerminatingSIP() {
224-
Processor_CopyInSystemStack(MAINMEMORYSIZE-1,OS_address_base+1);
242+
processTable[sipID].copyOfPCRegister=OS_address_base+1;
243+
Processor_CopyInSystemStack(MAINMEMORYSIZE-1,processTable[sipID].copyOfPCRegister);
244+
processTable[sipID].copyOfPSWRegister|= ((unsigned int) 1) << INTERRUPT_MASKED_BIT;
245+
Processor_CopyInSystemStack(MAINMEMORYSIZE-2,processTable[sipID].copyOfPSWRegister);
225246
executingProcessID=NOPROCESS;
226247
}
227248

@@ -237,7 +258,7 @@ void OperatingSystem_PrintStatus(){
237258
OperatingSystem_PrintReadyToRunQueue(); // Show Ready to run queues implemented for students
238259
OperatingSystem_PrintSleepingProcessQueue(); // Show Sleeping process queue
239260
OperatingSystem_PrintProcessTableAssociation(); // Show PID-Program's name association
240-
261+
ComputerSystem_PrintArrivalTimeQueue(); // Show arrival queue of programs
241262
}
242263

243264
// Show Executing process information
@@ -267,8 +288,10 @@ void OperatingSystem_PrintSleepingProcessQueue(){
267288
if (numberOfSleepingProcesses>0)
268289
for (i=0; i< numberOfSleepingProcesses; i++) {
269290
// Show message [PID, priority, whenToWakeUp]
270-
ComputerSystem_DebugMessage(75,SHORTTERMSCHEDULE,
271-
sleepingProcessesQueue[i].info,processTable[sleepingProcessesQueue[i].info].priority,processTable[sleepingProcessesQueue[i].info].whenToWakeUp);
291+
ComputerSystem_DebugMessage(75,SHORTTERMSCHEDULE
292+
, sleepingProcessesQueue[i].info
293+
, processTable[sleepingProcessesQueue[i].info].priority
294+
, processTable[sleepingProcessesQueue[i].info].whenToWakeUp);
272295
if (i<numberOfSleepingProcesses-1)
273296
ComputerSystem_DebugMessage(100,SHORTTERMSCHEDULE,", ");
274297
}
@@ -292,3 +315,29 @@ void OperatingSystem_PrintProcessTableAssociation() {
292315
}
293316
}
294317

318+
// This function returns:
319+
// EMPTYQUEUE (-1) if no programs in arrivalTimeQueue
320+
// YES (1) if any program arrivalTime is now
321+
// NO (0) else
322+
// considered by the LTS to create processes at the current time
323+
int OperatingSystem_IsThereANewProgram() {
324+
#ifdef ARRIVALQUEUE
325+
int currentTime;
326+
int programArrivalTime;
327+
int indexInProgramList = Heap_getFirst(arrivalTimeQueue,numberOfProgramsInArrivalTimeQueue);
328+
329+
if (indexInProgramList < 0)
330+
return EMPTYQUEUE; // No new programs in command line list of programs
331+
332+
// Get the current simulation time
333+
currentTime = Clock_GetTime();
334+
335+
// Get arrivalTime of next program
336+
programArrivalTime = programList[indexInProgramList]->arrivalTime;
337+
338+
if (programArrivalTime <= currentTime)
339+
return YES; // There'is new program to start
340+
#endif
341+
return NO; // No program in current time
342+
}
343+

V3/OperatingSystemBase.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ int OperatingSystem_PrepareTeachersDaemons(int);
1818
void OperatingSystem_ShowTime(char);
1919
void OperatingSystem_PrintStatus();
2020
void OperatingSystem_PrintReadyToRunQueue();
21-
void OperatingSystem_PrepareDaemons(int);
22-
int OperatingSystem_PrepareTeachersDaemons(int);
21+
int OperatingSystem_IsThereANewProgram();
22+
23+
#define EMPTYQUEUE -1
24+
#define NO 0
25+
#define YES 1
2326

2427
#ifdef SLEEPINGQUEUE
2528
extern heapItem sleepingProcessesQueue[];
2629
extern int numberOfSleepingProcesses;
2730
#endif
2831

29-
// Begin indes for daemons in programList
30-
// extern int baseDaemonsInProgramList;
32+
#ifdef ARRIVALQUEUE
33+
extern int numberOfProgramsInArrivalTimeQueue;
34+
extern heapItem arrivalTimeQueue[];
35+
#endif
3136

3237
#endif

V3/V1-2-Simulator.UO271563

-109 KB
Binary file not shown.

V3/messagesTCH.txt

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
75,[@G%d@@, %d, @R%d@@]
2121
76,\t\tPID: @G%d@@ -> %s\n
2222

23+
// 77,PID association with program's name:\n
24+
78,\t\t[@G%s@@, @R%d@@, @G%s@@]\n
25+
79,Zombie process [@R%d@@ -> @R%s@@], with size [@R%d@@] and initial address [@R%d@@] is removed from system\n
26+
27+
2328
// Assert system messages
2429
81,Using asserts file: %s\nMaximum number of asserts: %d\n
2530
82,%d Asserts Loaded\n

V3/readme.txt

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,2 @@
1-
Ej 1:
2-
Se han añadido las llamadas a los metodos que muestran los tics de reloj antes de cada mensaje
3-
Ej 2:
4-
a: añadido el CLOCKINT_BIT=9 al interruptLines_CPU del Processor.h. Además tambien hubo que inicializar el vector de interrupciones
5-
b: Creada la funcion de manejo de interrupciones de reloj
6-
c: En OperatingSystem_InterruptLogic se ha añadido otro case para CLOCKINT_BIT que llame a OperatingSystem_HandleClockInterrupt
7-
d: En Clock_Update se ha añadido un if para controlar si han pasado intervalBetweenInterrupts unidades de tiempo. Por alguna razon, si llamo a Processor_RaiseInterruption, entra en bucle infinito, por lo que de momento he llamado a OperatingSystem_InterruptLogic
8-
Ej 3:
9-
a: Añadido INTERRUPT_MASKED_BIT=15 a PSW_BITS en Processor.h
10-
b: Añadidas las lineas que se ordenan en Processor_ShowPSW
11-
c/e: En Processor_ManageInterrupts se ha hecho la llamada a Processor_ActivatePSW_Bit(INTERRUPT_MASKED_BIT) para que active tambien el bit de enmascarado
12-
Ej 4:
13-
Añadidas las llamadas a las funciones debugMessage y showTime en OperatingSystem_HandleClockInterrupt()
14-
Ej 5:
15-
Introducida la nueva llamada al sistema de procesos bloqueados
16-
Añadidas las variables necesarias en el PCB y en OperatingSystem.c y OperatingSystem.h
17-
Añadida la llamada SYSCALL_SLEEP y en su case correspondiente, llamada a la funcion OperatingSystem_MoveToTheBLOCKEDState, donde se calcula whenToWakeUp y se hacen otras operaciones necesarias
18-
, ademas de llamar a PintStatus al final del metodo
19-
Ej 6:
20-
Añadido un bucle en la funcion OperatingSystem_HandleClockInterrupt para comprobar qué procesos tienen que ser despertados. Si tienen que despertarse se llama a una funcion que lo hace
21-
Tras mostrar el estado del simulador si ha habido algun cambio, se comprueba si el proceso actual es el que mas prioridad tiene. Si no es asi, se echa y se ejecuta el mas prioritario mostrando el mensaje pertinente
22-
Si ha habido algun cambio de proceso, se vuelve a imprimir el estado del sistema
23-
Ej 7:
24-
Se llama a PrintStatus en los puntos indicados: Al final del tratamiento de SYSCALL_YIELD si ha habido cambio de proceso; al final del tratamiento de SYSCALL_END; al final del manejador de excepciones y al final de la ejecucion del PLP si se ha creado algun proceso
25-
Ej 8:
26-
Se ha comentado la llamada a la funcion que imprime la cola de listos dentro de la funcion OperatingSystem_MoveToTheREADYState
1+
Ej 0:
2+

0 commit comments

Comments
 (0)