-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
173 lines (140 loc) · 4.11 KB
/
main.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
/*
* File: main.c
* Author: danni
*
* Created on May 9, 2010, 5:50 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "globaldefs.h"
#include "datatypes.h"
#include "ipt.h"
#include "hat.h"
#include "ui.h"
#include "disk.h"
#include "pcb.h"
#include "freelist.h"
#include "mmu.h"
#include "mm.h"
#include "readerswriters.h"
#include "aging.h"
#include "prm.h"
void readConfigFromFile(string fileName) {
const int lineLength = 100;
FILE* hFile;
hFile = fopen(fileName, "r");
int i = 0, j = 0;
char* temp = 0;
if (hFile == NULL) {
exit(-1);
} else {
char oneLine[lineLength];
fgets(oneLine, lineLength, hFile);
sscanf(oneLine, "MaxNumOfProcesses = %u", &MaxNumOfProcesses);
fgets(oneLine, lineLength, hFile);
sscanf(oneLine, "PageSize = %u", &PageSize);
fgets(oneLine, lineLength, hFile);
sscanf(oneLine, "NumOfPagesInMM = %u", &NumOfPagesInMM);
fgets(oneLine, lineLength, hFile);
sscanf(oneLine, "NumOfPagesInDisk = %u", &NumOfPagesInDisk);
fgets(oneLine, lineLength, hFile);
sscanf(oneLine, "NumOfProcessPages = %u", &NumOfProcessPages);
fgets(oneLine, lineLength, hFile);
sscanf(oneLine, "ShiftClock = %u", &ShiftClock);
fclose(hFile);
}
}
void printConfigInfo() {
fprintf(outFile, "MaxNumOfProcesses = %u\n", MaxNumOfProcesses);
fprintf(outFile, "PageSize = %u\n", PageSize);
fprintf(outFile, "NumOfPagesInMM = %u\n", NumOfPagesInMM);
fprintf(outFile, "NumOfPagesInDisk = %u\n", NumOfPagesInDisk);
fprintf(outFile, "NumOfProcessPages = %u\n", NumOfProcessPages);
fprintf(outFile, "ShiftClock = %u\n", ShiftClock);
}
void printUsage() {
fprintf(outFile, "Incorrect usage, please user:\n sim config_file_name");
}
void init() {
bool ReturnVal = FALSE;
ASSERT_PRINT("===Starting init===\n");
ASSERT_PRINT("Init FreeList...\n");
ReturnVal = FREELIST_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init Disk...\n");
ReturnVal = DISK_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Creating PCB array for %d processes..\n", MaxNumOfProcesses);
ReturnVal = PCB_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init MM...\n");
ReturnVal = MM_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init MailBox Queue...\n");
ReturnVal = QUEUES_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init IPT..\n");
ReturnVal = IPT_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init HAT..\n");
ReturnVal = HAT_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init PRM..\n");
ReturnVal = PRM_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init READERSWRITERS..\n");
ReturnVal = READERSWRITERS_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Init Aging Deamon...\n");
ReturnVal = AGING_Init();
ASSERT(ReturnVal != FALSE);
ASSERT_PRINT("Creating UI Thread...\n");
ReturnVal = UI_CreateUIThread();
ASSERT(ReturnVal != FALSE);
}
int main(int argc, char** argv) {
void* status = NULL;
inFile = stdin;
outFile = stdout;
#ifndef DEBUG
readConfigFromFile("config");
printConfigInfo();
UI_HandleBatchFile("batch");
#else
if (argc != 2) {
printUsage();
exit(-1);
}
readConfigFromFile(argv[1]);
#endif
init();
pthread_join(UI_Thread, status);
int i = 0;
for (i; i < MaxNumOfProcesses; i++) {
if(PCBArray[i].processThread)
pthread_join(PCBArray[i].processThread,NULL);
}
//closing AGING deamon
AGING_Close();
pthread_mutex_unlock(&Aging_mutex);
pthread_join(Aging, NULL);
FREELIST_DeAllocate();
//closing PRM
PRM_Close();
sem_post(&PRM_full); // decrement the full semaphore
sem_post(&PRM_mutex); // enter critical section
pthread_join(PRM, NULL);
QUEUES_DeInit();
MM_DeInit();
fclose(inFile);
fclose(outFile);
DISK_DeInit();
PCB_Free();
for (i = 0; i < NumOfPagesInMM; i++) {
//if(IPT[i]!=NULL)
free(IPT[i]);
}
free(IPT);
free(HAT);
return (EXIT_SUCCESS);
}