Main Page | Alphabetical List | Class List | File List | Class Members | File Members

system.cc

Go to the documentation of this file.
00001 // system.cc 00002 // Nachos initialization and cleanup routines. 00003 // 00004 // Copyright (c) 1992-1993 The Regents of the University of California. 00005 // All rights reserved. See copyright.h for copyright notice and limitation 00006 // of liability and disclaimer of warranty provisions. 00007 00008 #include "copyright.h" 00009 #include "system.h" 00010 #include <sys/time.h> 00011 #include <stdio.h> 00012 #include <signal.h> 00013 #include <pthread.h> 00014 #include <unistd.h> 00015 00016 // This defines *all* of the global data structures used by Nachos. 00017 // These are all initialized and de-allocated by this file. 00018 00019 Thread *currentThread; // the thread we are running now 00020 Thread *threadToBeDestroyed; // the thread that just finished 00021 Scheduler *scheduler; // the ready list 00022 Interrupt *interrupt; // interrupt status 00023 Statistics *stats; // performance metrics 00024 Timer *timer; // the hardware timer device, 00025 // for invoking context switches 00026 00027 00028 long timerCount = 0; 00029 long secCount = 0; 00030 00031 00032 00033 #ifdef FILESYS_NEEDED 00034 FileSystem *fileSystem; 00035 #endif 00036 00037 #ifdef FILESYS 00038 SynchDisk *synchDisk; 00039 #endif 00040 00041 #ifdef USER_PROGRAM // requires either FILESYS or FILESYS_STUB 00042 Machine *machine; // user program memory and registers 00043 #endif 00044 00045 #ifdef NETWORK 00046 PostOffice *postOffice; 00047 #endif 00048 00049 00050 // External definition, to allow us to take a pointer to this function 00051 extern void Cleanup(); 00052 00053 00054 //---------------------------------------------------------------------- 00055 // TimerInterruptHandler 00056 // Interrupt handler for the timer device. The timer device is 00057 // set up to interrupt the CPU periodically (once every TimerTicks). 00058 // This routine is called each time there is a timer interrupt, 00059 // with interrupts disabled. 00060 // 00061 // Note that instead of calling Yield() directly (which would 00062 // suspend the interrupt handler, not the interrupted thread 00063 // which is what we wanted to context switch), we set a flag 00064 // so that once the interrupt handler is done, it will appear as 00065 // if the interrupted thread called Yield at the point it is 00066 // was interrupted. 00067 // 00068 // "dummy" is because every interrupt handler takes one argument, 00069 // whether it needs it or not. 00070 //---------------------------------------------------------------------- 00071 static void 00072 TimerInterruptHandler(int dummy) 00073 { 00074 if (interrupt->getStatus() != IdleMode) 00075 interrupt->YieldOnReturn(); 00076 } 00077 00078 00079 00080 //---------------------------------------------------------------------- 00081 // Initialize 00082 // Initialize Nachos global data structures. Interpret command 00083 // line arguments in order to determine flags for the initialization. 00084 // 00085 // "argc" is the number of command line arguments (including the name 00086 // of the command) -- ex: "nachos -d +" -> argc = 3 00087 // "argv" is an array of strings, one for each command line argument 00088 // ex: "nachos -d +" -> argv = {"nachos", "-d", "+"} 00089 //---------------------------------------------------------------------- 00090 void Initialize(int argc, char **argv) 00091 { 00092 int argCount; 00093 char *debugArgs = ""; 00094 bool randomYield = FALSE; 00095 00096 00097 #ifdef USER_PROGRAM 00098 bool debugUserProg = FALSE; // single step user program 00099 #endif 00100 #ifdef FILESYS_NEEDED 00101 bool format = FALSE; // format disk 00102 #endif 00103 #ifdef NETWORK 00104 double rely = 1; // network reliability 00105 int netname = 0; // UNIX socket name 00106 #endif 00107 00108 for (argc--, argv++; argc > 0; argc -= argCount, argv += argCount) { 00109 argCount = 1; 00110 if (!strcmp(*argv, "-d")) { 00111 if (argc == 1) 00112 debugArgs = "+"; // turn on all debug flags 00113 else { 00114 debugArgs = *(argv + 1); 00115 argCount = 2; 00116 } 00117 } else if (!strcmp(*argv, "-rs")) { 00118 ASSERT(argc > 1); 00119 RandomInit(atoi(*(argv + 1))); // initialize pseudo-random 00120 // number generator 00121 randomYield = TRUE; 00122 argCount = 2; 00123 } 00124 #ifdef USER_PROGRAM 00125 if (!strcmp(*argv, "-s")) 00126 debugUserProg = TRUE; 00127 #endif 00128 #ifdef FILESYS_NEEDED 00129 if (!strcmp(*argv, "-f")) 00130 format = TRUE; 00131 #endif 00132 #ifdef NETWORK 00133 if (!strcmp(*argv, "-l")) { 00134 ASSERT(argc > 1); 00135 rely = atof(*(argv + 1)); 00136 argCount = 2; 00137 } else if (!strcmp(*argv, "-m")) { 00138 ASSERT(argc > 1); 00139 netname = atoi(*(argv + 1)); 00140 argCount = 2; 00141 } 00142 #endif 00143 } 00144 00145 DebugInit(debugArgs); // initialize DEBUG messages 00146 stats = new Statistics(); // collect statistics 00147 interrupt = new Interrupt; // start up interrupt handling 00148 scheduler = new Scheduler(); // initialize the ready queue 00149 //if (randomYield) // start the timer (if needed) 00150 //timer = new Timer(TimerInterruptHandler, 0, randomYield); 00151 timer = 0; 00152 00153 00154 threadToBeDestroyed = NULL; 00155 00156 // We didn't explicitly allocate the current thread we are running in. 00157 // But if it ever tries to give up the CPU, we better have a Thread 00158 // object to save its state. 00159 //currentThread = new Thread("main"); 00160 //currentThread->setStatus(RUNNING); 00161 00162 interrupt->Enable(); 00163 CallOnUserAbort(Cleanup); // if user hits ctl-C 00164 00165 #ifdef USER_PROGRAM 00166 machine = new Machine(debugUserProg); // this must come first 00167 #endif 00168 00169 #ifdef FILESYS 00170 synchDisk = new SynchDisk("DISK"); 00171 #endif 00172 00173 #ifdef FILESYS_NEEDED 00174 fileSystem = new FileSystem(format); 00175 #endif 00176 00177 #ifdef NETWORK 00178 postOffice = new PostOffice(netname, rely, 10); 00179 #endif 00180 } 00181 00182 extern int tready; 00183 //---------------------------------------------------------------------- 00184 // Cleanup 00185 // Nachos is halting. De-allocate global data structures. 00186 //---------------------------------------------------------------------- 00187 void Cleanup() 00188 { 00189 extern timer_t timer_id; 00190 //ualarm(0, 0); // switch off the timer 00191 timer_delete(timer_id); 00192 printf("\nCleaning up...\n"); 00193 #ifdef NETWORK 00194 delete postOffice; 00195 #endif 00196 00197 #ifdef USER_PROGRAM 00198 delete machine; 00199 #endif 00200 00201 #ifdef FILESYS_NEEDED 00202 delete fileSystem; 00203 #endif 00204 00205 #ifdef FILESYS 00206 delete synchDisk; 00207 #endif 00208 00209 delete timer; 00210 delete scheduler; 00211 delete interrupt; 00212 00213 Exit(0); 00214 } 00215

Generated on Thu Sep 16 12:33:45 2004 for NachOS by doxygen 1.3.8