-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
155 lines (144 loc) · 5.04 KB
/
main.cc
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
// main.cc
// Bootstrap code to initialize the operating system kernel.
//
// Allows direct calls into internal operating system functions,
// to simplify debugging and testing. In practice, the
// bootstrap code would just initialize data structures,
// and start a user program to print the login prompt.
//
// Most of this file is not needed until later assignments.
//
// Usage: nachos -d <debugflags> -rs <random seed #>
// -s -x <nachos file> -c <consoleIn> <consoleOut>
// -f -cp <unix file> <nachos file>
// -p <nachos file> -r <nachos file> -l -D -t
// -n <network reliability> -m <machine id>
// -o <other machine id>
// -z
//
// -d causes certain debugging messages to be printed (cf. utility.h)
// -rs causes Yield to occur at random (but repeatable) spots
// -z prints the copyright message
//
// USER_PROGRAM
// -s causes user programs to be executed in single-step mode
// -x runs a user program
// -c tests the console
//
// FILESYS
// -f causes the physical disk to be formatted
// -cp copies a file from UNIX to Nachos
// -p prints a Nachos file to stdout
// -r removes a Nachos file from the file system
// -l lists the contents of the Nachos directory
// -D prints the contents of the entire file system
// -t tests the performance of the Nachos file system
//
// NETWORK
// -n sets the network reliability
// -m sets this machine's host id (needed for the network)
// -o runs a simple test of the Nachos network software
//
// NOTE -- flags are ignored until the relevant assignment.
// Some of the flags are interpreted here; some in system.cc.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#define MAIN
#include "copyright.h"
#undef MAIN
#include "utility.h"
#include "system.h"
// External functions used by this file
extern void ThreadTest(void), Copy(char *unixFile, char *nachosFile);
extern void Print(char *file), PerformanceTest(void);
extern void StartProcess(char *file), ConsoleTest(char *in, char *out);
extern void MailTest(int networkID);
//----------------------------------------------------------------------
// main
// Bootstrap the operating system kernel.
//
// Check command line arguments
// Initialize data structures
// (optionally) Call test procedure
//
// "argc" is the number of command line arguments (including the name
// of the command) -- ex: "nachos -d +" -> argc = 3
// "argv" is an array of strings, one for each command line argument
// ex: "nachos -d +" -> argv = {"nachos", "-d", "+"}
//----------------------------------------------------------------------
int
main(int argc, char **argv)
{
int argCount; // the number of arguments
// for a particular command
DEBUG('t', "Entering main");
(void) Initialize(argc, argv);
#ifdef THREADS
ThreadTest();
#endif
for (argc--, argv++; argc > 0; argc -= argCount, argv += argCount) {
argCount = 1;
if (!strcmp(*argv, "-z")) // print copyright
printf (copyright);
#ifdef USER_PROGRAM
if (!strcmp(*argv, "-x")) { // run a user program
ASSERT(argc > 1);
StartProcess(*(argv + 1));
argCount = 2;
} else if (!strcmp(*argv, "-c")) { // test the console
if (argc == 1)
ConsoleTest(NULL, NULL);
else {
ASSERT(argc > 2);
ConsoleTest(*(argv + 1), *(argv + 2));
argCount = 3;
}
interrupt->Halt(); // once we start the console, then
// Nachos will loop forever waiting
// for console input
}
#endif // USER_PROGRAM
#ifdef FILESYS
if (!strcmp(*argv, "-cp")) { // copy from UNIX to Nachos
ASSERT(argc > 2);
Copy(*(argv + 1), *(argv + 2));
argCount = 3;
} else if (!strcmp(*argv, "-p")) { // print a Nachos file
ASSERT(argc > 1);
Print(*(argv + 1));
argCount = 2;
} else if (!strcmp(*argv, "-r")) { // remove Nachos file
ASSERT(argc > 1);
fileSystem->Remove(*(argv + 1));
argCount = 2;
} else if (!strcmp(*argv, "-l")) { // list Nachos directory
fileSystem->List();
} else if (!strcmp(*argv, "-D")) { // print entire filesystem
fileSystem->Print();
} else if (!strcmp(*argv, "-t")) { // performance test
PerformanceTest();
}
#endif // FILESYS
#ifdef NETWORK
if (!strcmp(*argv, "-o")) {
ASSERT(argc > 1);
Delay(2); // delay for 2 seconds
// to give the user time to
// start up another nachos
MailTest(atoi(*(argv + 1)));
argCount = 2;
}
#endif // NETWORK
}
currentThread->Finish(); // NOTE: if the procedure "main"
// returns, then the program "nachos"
// will exit (as any other normal program
// would). But there may be other
// threads on the ready list. We switch
// to those threads by saying that the
// "main" thread is finished, preventing
// it from returning.
return(0); // Not reached...
}