Page principale | Liste des namespaces | Hiérarchie des classes | Liste des classes | Répertoires | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

main.cc

Aller à la documentation de ce fichier.
00001 // main.cc 
00002 //      Driver code to initialize, selftest, and run the 
00003 //      operating system kernel.  
00004 //
00005 // Usage: nachos -d <debugflags> -rs <random seed #>
00006 //              -s -x <nachos file> -ci <consoleIn> -co <consoleOut>
00007 //              -f -cp <unix file> <nachos file>
00008 //              -p <nachos file> -r <nachos file> -l -D
00009 //              -n <network reliability> -m <machine id>
00010 //              -z -K -C -N
00011 //
00012 //    -d causes certain debugging messages to be printed (see debug.h)
00013 //    -rs causes Yield to occur at random (but repeatable) spots
00014 //    -z prints the copyright message
00015 //    -s causes user programs to be executed in single-step mode
00016 //    -x runs a user program
00017 //    -ci specify file for console input (stdin is the default)
00018 //    -co specify file for console output (stdout is the default)
00019 //    -n sets the network reliability
00020 //    -m sets this machine's host id (needed for the network)
00021 //    -K run a simple self test of kernel threads and synchronization
00022 //    -C run an interactive console test
00023 //    -N run a two-machine network test (see Kernel::NetworkTest)
00024 //
00025 //    Filesystem-related flags:
00026 //    -f forces the Nachos disk to be formatted
00027 //    -cp copies a file from UNIX to Nachos
00028 //    -p prints a Nachos file to stdout
00029 //    -r removes a Nachos file from the file system
00030 //    -l lists the contents of the Nachos directory
00031 //    -D prints the contents of the entire file system 
00032 //
00033 //  Note: the file system flags are not used if the stub filesystem
00034 //        is being used
00035 //
00036 // Copyright (c) 1992-1996 The Regents of the University of California.
00037 // All rights reserved.  See copyright.h for copyright notice and limitation 
00038 // of liability and disclaimer of warranty provisions.
00039 
00040 #define MAIN
00041 #include "copyright.h"
00042 #undef MAIN
00043 
00044 #include "main.h"
00045 #include "filesys.h"
00046 #include "openfile.h"
00047 #include "sysdep.h"
00048 
00049 #ifdef TUT
00050 
00051 #include "tut.h"
00052 //implements callbacks for unit test
00053 #include "tut_reporter.h"
00054 namespace tut
00055 {
00056     test_runner_singleton runner;
00057 }
00058 
00059 #endif TUT
00060 
00061 // global variables
00062 Kernel *kernel;
00063 Debug *debug;
00064 
00065 
00066 //----------------------------------------------------------------------
00067 // Cleanup
00068 //      Delete kernel data structures; called when user hits "ctl-C".
00069 //----------------------------------------------------------------------
00070 
00071 static void 
00072 Cleanup(int x) 
00073 {     
00074     cerr << "\nCleaning up after signal " << x << "\n";
00075     delete kernel; 
00076 }
00077 
00078 //-------------------------------------------------------------------
00079 // Constant used by "Copy" and "Print"
00080 //   It is the number of bytes read from the Unix file (for Copy)
00081 //   or the Nachos file (for Print) by each read operation
00082 //-------------------------------------------------------------------
00083 static const int TransferSize = 128;
00084 
00085 
00086 #ifndef FILESYS_STUB
00087 //----------------------------------------------------------------------
00088 // Copy
00089 //      Copy the contents of the UNIX file "from" to the Nachos file "to"
00090 //----------------------------------------------------------------------
00091 
00092 static void
00093 Copy(char *from, char *to)
00094 {
00095     int fd;
00096     OpenFile* openFile;
00097     int amountRead, fileLength;
00098     char *buffer;
00099 
00100 // Open UNIX file
00101     if ((fd = OpenForReadWrite(from,FALSE)) < 0) {       
00102         cerr <<"Copy: couldn't open input file"<< from << "\n";
00103         return;
00104     }
00105 
00106 // Figure out length of UNIX file
00107     Lseek(fd, 0, 2);            
00108     fileLength = Tell(fd);
00109     Lseek(fd, 0, 0);
00110 
00111 // Create a Nachos file of the same length
00112     DEBUG('f', "Copying file " << from << " of size " << fileLength <<  " to file " << to);
00113     if (!kernel->fileSystem->Create(to, fileLength)) {   // Create Nachos file
00114         cerr<< "Copy: couldn't create output file"<< to << "\n";
00115         Close(fd);
00116         return;
00117     }
00118     
00119     openFile = kernel->fileSystem->Open(to);
00120     ASSERT(openFile != NULL);
00121     
00122 // Copy the data in TransferSize chunks
00123     buffer = new char[TransferSize];
00124     while ((amountRead=ReadPartial(fd, buffer, sizeof(char)*TransferSize)) > 0)
00125         openFile->Write(buffer, amountRead);    
00126     delete [] buffer;
00127 
00128 // Close the UNIX and the Nachos files
00129     delete openFile;
00130     Close(fd);
00131 }
00132 
00133 #endif // FILESYS_STUB
00134 
00135 //----------------------------------------------------------------------
00136 // Print
00137 //      Print the contents of the Nachos file "name".
00138 //----------------------------------------------------------------------
00139 
00140 void
00141 Print(char *name)
00142 {
00143     OpenFile *openFile;    
00144     int i, amountRead;
00145     char *buffer;
00146 
00147     if ((openFile = kernel->fileSystem->Open(name)) == NULL) {
00148         cerr << "Print: unable to open file"<< name<< "\n";
00149         return;
00150     }
00151     
00152     buffer = new char[TransferSize];
00153     while ((amountRead = openFile->Read(buffer, TransferSize)) > 0)
00154         for (i = 0; i < amountRead; i++)
00155             cerr <<  buffer[i];
00156     cerr <<"\n";
00157     delete [] buffer;
00158 
00159     delete openFile;            // close the Nachos file
00160     return;
00161 }
00162 
00163 
00164 
00165 //----------------------------------------------------------------------
00166 // main
00167 //      Bootstrap the operating system kernel.  
00168 //      
00169 //      Initialize kernel data structures
00170 //      Call some test routines
00171 //      Call "Run" to start an initial user program running
00172 //
00173 //      "argc" is the number of command line arguments (including the name
00174 //              of the command) -- ex: "nachos -d +" -> argc = 3 
00175 //      "argv" is an array of strings, one for each command line argument
00176 //              ex: "nachos -d +" -> argv = {"nachos", "-d", "+"}
00177 //----------------------------------------------------------------------
00178 
00179 int
00180 main(int argc, char **argv)
00181 {
00182     int i;
00183     char *debugArg = "";
00184     char *userProgName = NULL;        // default is not to execute a user prog
00185     bool threadTestFlag = false;
00186     bool consoleTestFlag = false;
00187     bool networkTestFlag = false;
00188 #ifndef FILESYS_STUB
00189     char *copyUnixFileName = NULL;    // UNIX file to be copied into Nachos
00190     char *copyNachosFileName = NULL;  // name of copied file in Nachos
00191     char *printFileName = NULL; 
00192     char *removeFileName = NULL;
00193     bool dirListFlag = false;
00194     bool dumpFlag = false;
00195 #endif //FILESYS_STUB
00196 
00197     // some command line arguments are handled here.
00198     // those that set kernel parameters are handled in
00199     // the Kernel constructor
00200     for (i = 1; i < argc; i++) {
00201         if (strcmp(argv[i], "-d") == 0) {
00202             ASSERT(i + 1 < argc);   // next argument is debug string
00203             debugArg = argv[i + 1];
00204             i++;
00205         }
00206         else if (strcmp(argv[i], "-z") == 0) {
00207             cerr << copyright << "\n";
00208         }
00209         else if (strcmp(argv[i], "-x") == 0) {
00210             ASSERT(i + 1 < argc);
00211             userProgName = argv[i + 1];
00212             i++;
00213         }
00214         else if (strcmp(argv[i], "-K") == 0) {
00215             threadTestFlag = TRUE;
00216         }
00217         else if (strcmp(argv[i], "-C") == 0) {
00218             consoleTestFlag = TRUE;
00219         }
00220         else if (strcmp(argv[i], "-N") == 0) {
00221             networkTestFlag = TRUE;
00222         }
00223 #ifndef FILESYS_STUB
00224         else if (strcmp(argv[i], "-cp") == 0) {
00225             ASSERT(i + 2 < argc);
00226             copyUnixFileName = argv[i + 1];
00227             copyNachosFileName = argv[i + 2];
00228             i += 2;
00229         }
00230         else if (strcmp(argv[i], "-p") == 0) {
00231             ASSERT(i + 1 < argc);
00232             printFileName = argv[i + 1];
00233             i++;
00234         }
00235         else if (strcmp(argv[i], "-r") == 0) {
00236             ASSERT(i + 1 < argc);
00237             removeFileName = argv[i + 1];
00238             i++;
00239         }
00240         else if (strcmp(argv[i], "-l") == 0) {
00241             dirListFlag = true;
00242         }
00243         else if (strcmp(argv[i], "-D") == 0) {
00244             dumpFlag = true;
00245         }
00246 #endif //FILESYS_STUB
00247         else if (strcmp(argv[i], "-u") == 0) {
00248             cerr << "Partial usage: nachos [-z -d debugFlags]\n";
00249             cerr<< "Partial usage: nachos [-x programName]\n";
00250             cerr << "Partial usage: nachos [-K] [-C] [-N]\n";
00251 #ifndef FILESYS_STUB
00252             cerr << "Partial usage: nachos [-cp UnixFile NachosFile]\n";
00253             cerr << "Partial usage: nachos [-p fileName] [-r fileName]\n";
00254             cerr << "Partial usage: nachos [-l] [-D]\n";
00255 #endif //FILESYS_STUB
00256         }
00257 
00258     }
00259     debug = new Debug(debugArg);
00260     
00261     DEBUG(dbgThread, "Entering main");
00262 
00263 #ifdef TUT
00264     ::tut::callback * clbk = new tut::reporter(cerr);
00265     ::tut::runner.get().set_callback(clbk);  
00266     ::tut::runner.get().run_tests(); //run all unit tests
00267 #endif 
00268     
00269     kernel = new Kernel(argc, argv);
00270 
00271     kernel->Initialize();
00272 
00273     CallOnUserAbort(Cleanup);           // if user hits ctl-C
00274 
00275     // at this point, the kernel is ready to do something
00276     // run some tests, if requested
00277     if (threadTestFlag) {
00278       kernel->ThreadSelfTest();  // test threads and synchronization
00279     }
00280     if (consoleTestFlag) {
00281       kernel->ConsoleTest();   // interactive test of the synchronized console
00282     }
00283     if (networkTestFlag) {
00284       kernel->NetworkTest();   // two-machine test of the network
00285     }
00286 
00287 #ifndef FILESYS_STUB
00288     if (removeFileName != NULL) {
00289       kernel->fileSystem->Remove(removeFileName);
00290     }
00291     if (copyUnixFileName != NULL && copyNachosFileName != NULL) {
00292       Copy(copyUnixFileName,copyNachosFileName);
00293     }
00294     if (dumpFlag) {
00295       kernel->fileSystem->Print();
00296     }
00297     if (dirListFlag) {
00298       kernel->fileSystem->List();
00299     }
00300     if (printFileName != NULL) {
00301       Print(printFileName);
00302     }
00303 #endif // FILESYS_STUB
00304 
00305     // finally, run an initial user program if requested to do so
00306     if (userProgName != NULL) {
00307       AddrSpace *space = new AddrSpace;
00308       ASSERT(space != (AddrSpace *)NULL);
00309       if (space->Load(userProgName)) {  // load the program into the space
00310         kernel->currentThread->space = space;
00311         space->Execute();              // run the program
00312         ASSERTNOTREACHED();            // Execute never returns
00313       }
00314     }
00315 
00316     // If we don't run a user program, we may get here.
00317     // Calling "return" would terminate the program.
00318     // Instead, call Halt, which will first clean up, then
00319     //  terminate.
00320     kernel->interrupt->Halt();
00321     
00322     ASSERTNOTREACHED();
00323 }
00324 

Généré le Sun Jan 15 00:45:45 2006 pour Système NachOS : par  doxygen 1.4.4