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

Generated on Mon Feb 10 09:54:47 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees