天天看點

Nachos源代碼分析(一)

// main.cc

//   初始化作業系統核心

//

//   這部分代碼一般是初始化各個資料結構,并且而啟動一個使用者程式來輸出登陸提示資訊

//   (具體指令行參數用法省略)

#define MAIN

#include "copyright.h"

#undef MAIN

#include "utility.h"

#include "system.h"

//引入檔案中使用的外部函數

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

//   功能:啟動作業系統核心

//  

//   1,檢查指令行參數

//   2,初始化資料結構

//   3,(可選) 調用測試方法

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

          Print(*(argv + 1));

          argCount = 2;

      } else if (!strcmp(*argv, "-r")) {  // remove Nachos file

          fileSystem->Remove(*(argv + 1));

      } 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")) {

            Delay(2);                       // delay for 2 seconds

                                  // to give the user time to

                                  // start up another nachos

            MailTest(atoi(*(argv + 1)));

        }

#endif // NETWORK

    }

currentThread->Finish(); //main快結束了,切換到其他生成的線程,進而讓main函數無法終止

    return(0);                // Not reached...

}

本文轉自Phinecos(洞庭散人)部落格園部落格,原文連結:http://www.cnblogs.com/phinecos/archive/2007/10/31/944918.html,如需轉載請自行聯系原作者