Home Pintos project-1-context switch
Post
Cancel

Pintos project-1-context switch

Context switch

Pintos에서 context switch가 어떻게 일어나는지 확인해 보도록 하자.

반드시 모든 과제를 수행하기 전에 이 문서를 읽는 것을 추천한다.

Process struct

Pintos에서 프로세스를 나타내는 구조체는 include/thread/thread.h에 정의되어 있다. Pintos는 process와 thread를 엄격히 구분하지 않는다. thread == process로 생각해도 좋다.

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
struct thread {
	/* Owned by thread.c. */
	tid_t tid;                          /* Thread identifier. */
	enum thread_status status;          /* Thread state. */
	char name[16];                      /* Name (for debugging purposes). */
	int priority;                       /* Priority. */

	/* Shared between thread.c and synch.c. */
	struct list_elem elem;              /* List element. */

#ifdef USERPROG
	/* Owned by userprog/process.c. */
	uint64_t *pml4;                     /* Page map level 4 */
#endif
#ifdef VM
	/* Table for whole virtual memory owned by thread. */
	struct supplemental_page_table spt;
#endif

	/* Owned by thread.c. */
	struct intr_frame tf;               /* Information for switching */
	unsigned magic;                     /* Detects stack overflow. */
};

각 변수의 설명

tid_t

thread를 구분하는 id이다. int 타입이다.

enum thread_status status

현재 thread의 상태를 나타내는 enum 변수이다. 원형은 다음과 같다.

1
2
3
4
5
6
7
/* States in a thread's life cycle. */
enum thread_status {
	THREAD_RUNNING,     /* Running thread. */
	THREAD_READY,       /* Not running but ready to run. */
	THREAD_BLOCKED,     /* Waiting for an event to trigger. */
	THREAD_DYING        /* About to be destroyed. */
};

thread의 상태 전이를 나타내는 STD (State Transition Diagram)을 참고해도 좋다.

char name[16]

Thread의 이름이다.

int priority

우선도 (priority)를 나타낸다.

struct list_elem elem;

자료구조의 list (연결 리스트)를 사용하기 위해 만들어진 항목이다.

struct intr_frame tf;

Context-switch를 할 때 필요한 인터럽트 프레임을 담고 있는 부분이다.

unsigned magic

Stack overflow (스택 오버플로우)를 감지하기 위해 설정된 값이다. 만약, 스택이 지정된 영역을 초과해 이 부분을 덮어쓰게 되면 이를 감지한다.

기타

USERPROG, VM 단계에서 조건부 컴파일되는 부분이다. 이 부분은 나중에 다루도록 한다.

This post is written by david61song