티스토리 뷰

반응형


  • 개인공부 후 자료를 남기기 위한 목적으로 포스팅합니다. 내용 상에 오류가 있을 수 있습니다.


0x07. main() 함수 분석


1. ThreadedKernel, UserProgKernel 두 class는 어떤 관계인가?

// kernel.h


#ifndef KERNEL_H

#define KERNEL_H

#include "copyright.h"

#include "debug.h"

#include "utility.h"

#include "thread.h"

#include "scheduler.h“

#include "interrupt.h"

#include "stats.h"

#include "alarm.h"

class ThreadedKernel {

 public:

    ThreadedKernel(int argc, char **argv);  // Interpret command line arguments

    ~ThreadedKernel();         // deallocate the kernel

    void Initialize();                // initialize the kernel -- separated

                                // from constructor because

                                // refers to "kernel" as a global

    void Run();                 // do kernel stuff

    void SelfTest();          // test whether kernel is working

    

    Thread *currentThread;   // the thread holding the CPU

    Scheduler *scheduler;     // the ready list

    Interrupt *interrupt;     // interrupt status

    Statistics *stats;         // performance metrics

    Alarm *alarm;             // the software alarm clock   

  private:

    bool randomSlice;         // enable pseudo-random time slicing

};

#endif 

// userkernel.h


#ifndef USERKERNEL_H 

#define USERKERNEL_H

#include "kernel.h"

#include "filesys.h"

#include "machine.h"

class UserProgKernel : public ThreadedKernel {

  public:

    UserProgKernel(int argc, char **argv);

                                // Interpret command line arguments

    ~UserProgKernel();         // deallocate the kernel

    void Initialize();         // initialize the kernel

    void Run();                 // do kernel stuff

    void SelfTest();          // test whether kernel is working

                                // These are public for notational convenience.

    Machine *machine;

    FileSystem *fileSystem;

  private:

    bool debugUserProg;               // single step user program

};

#endif

   두 클래스는 상속 관계에 있다. ThreadedKernel이 UserProgKernel 클래스의 부모 클래스이며 이들은 사용자 프로그램의 실행을 지원해 준다. 이 관계는 객체지향의 상속 관계에서 보았을 때 is a 관계라고 할 수 있다.




2. thread/nachos, userprog/nachos 두 실행 파일을 실행할 때의 main 함수가 수행하는 작업의 차이점은 무엇인가? (main.h, main.cc)

// main.h


#ifndef MAIN_H

        #define MAIN_H

        #include "copyright.h"

        #include "debug.h"

        #ifdef NETWORK  // THREADS && USER_PROGRAM && NETWORK

                #include "netkernel.h"

                #define KernelType NetKernel

                #else 

                #ifdef USER_PROGRAM  // THREADS && USER_PROGRAM

                // USER_PROGRAM이 정의되어 있으면 아래의 코드 실행

                        #include "userkernel.h"

                        #define KernelType UserProgKernel

                #else

                // USER_PROGRAM이 정의되어 있지 않으면 아래의 코드 실행

                        #include "kernel.h" // THREADS

                        #define KernelType ThreadedKernel

                #endif

        #endif

        extern KernelType *kernel;

        extern Debug *debug;

#endif


//main.cc


#define MAIN

#include "copyright.h"

#undef MAIN

#include "main.h"

 

// global variables

KernelType *kernel;

Debug *debug;

 

static void Cleanup(int x)

{    

    cerr << "\nCleaning up after signal " << x << "\n";

    delete kernel;

}

 

int main(int argc, char **argv)

{

    int i;

    char *debugArg = "";

 

    // before anything else, initialize the debugging system

    for (i = 1; i < argc; i++) {

        if (strcmp(argv[i], "-d") == 0) {

            ASSERT(i + 1 < argc);   // next argument is debug string

            debugArg = argv[i + 1];

            i++;

        } else if (strcmp(argv[i], "-u") == 0) {

            cout << "Partial usage: nachos [-z -d debugFlags]\n";

        } else if (strcmp(argv[i], "-z") == 0) {

            cout << copyright;

        }

    }

    debug = new Debug(debugArg);

   

    DEBUG(dbgThread, "Entering main");

 

    kernel = new KernelType(argc, argv); //이곳에서 KernelType을 호출하는 곳이 달라짐

 

    kernel->Initialize();

    CallOnUserAbort(Cleanup);          // if user hits ctl-C

    kernel->SelfTest();

    kernel->Run();

    return 0;

}

   main.h에서 보면 #ifdef User_PROGRAM 부분에서 해당 매크로가 선언되어 있는지 본 뒤 USER_PROGRAM (make 실행시 선언되는 매크로)이 선언되어 있는 경우, userkernel.h를 include하고 선언되어있지 않은 경우 Kernel.h를 include한다. 즉, thread 디렉토리에서 nachos를 실행하면 ThreadKernel 클래스의 함수가 호출되고 userprog 디렉토리의 nachos를 실행할 경우 UsesrProgKernel의 클래스의 함수가 호출된다.




3. 위의 두 실행 파일에서 사용할 수 있는 option들은 무엇인가?

//상단부 생략...

int main(int argc, char **argv)

{

    int i;

    char *debugArg = "";

 

    // before anything else, initialize the debugging system

    for (i = 1; i < argc; i++)

    {

        if (strcmp(argv[i], "-d") == 0) {       //“-d" 옵션은 디버그 모드

            ASSERT(i + 1 < argc);   // next argument is debug string

            debugArg = argv[i + 1];

            i++;

        } else if (strcmp(argv[i], "-u") == 0) { //“-u" 옵션은 디버그 모드

            cout << "Partial usage: nachos [-z -d debugFlags]\n";

        } else if (strcmp(argv[i], "-z") == 0) { //“-z" 옵션은 디버그 모드

            cout << copyright;

        }

    }

    debug = new Debug(debugArg);

   

    DEBUG(dbgThread, "Entering main");

    kernel = new KernelType(argc, argv);

    kernel->Initialize();

    CallOnUserAbort(Cleanup);         // if user hits ctl-C

 

    kernel->SelfTest();

    kernel->Run();

   

    return 0;

}




4. nachos의 실행 옵션은 3가지

    ❶ -d: 디버그 모드를 나타낸다. 아래의 옵션은 디버그 모드에서 사용 가능한 옵션이다.

옵션 명

설    명

+

모든 디버그

t

스레드 관련

s

Locks, Semaphore, Condition vars 관련

i

인터럽트 Emulation

m

머신 Emulation

d

디스크 Emulation

f

파일 시스템

a

주소 공간

n

네트워크 Emulation

    ❷ -u: 사용법을 출력해준다.

    ❸ -z: 저작권에 대한 정보를 출력해 준다.




반응형
프로필사진

Yowu (Yu Yongwoo)

흔한 Node.js/Java 백엔드 개발자입니다
Ubuntu와 MacOS 데스크탑 개발 환경을 선호합니다
최근에는 vscode와 IntelliJ를 사용하고 있습니다
vscode에는 neovim, IntelliJ는 ideaVim
개발용 키보드는 역시 HHKB Pro 2 무각입니다
락 밴드에서 드럼을 쳤습니다

최근에 올라온 글
최근에 달린 댓글
«   2024/03   »
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
글 보관함
Total
Today
Yesterday