[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1#ifndef THREADS_THREAD_H
2#define THREADS_THREAD_H
3
4#include <debug.h>
5#include <list.h>
6#include <stdint.h>
7#include "threads/interrupt.h"
8#include "threads/synch.h"
9#ifdef VM
10#include "vm/vm.h"
11#endif
12#define USERPROG // syscall.c 파일에서 에러 뜨는 부분 방지
13#define VM // vm.c 파일에서 에러 뜨는 부분 방지
14
15/* States in a thread's life cycle. */
17 THREAD_RUNNING, /* Running thread. */
18 THREAD_READY, /* Not running but ready to run. */
19 THREAD_BLOCKED, /* Waiting for an event to trigger. */
20 THREAD_DYING /* About to be destroyed. */
21};
22
23/* Thread identifier type.
24 You can redefine this to whatever type you like. */
25typedef int tid_t;
26#define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */
27
28/* Thread priorities. */
29#define PRI_MIN 0 /* Lowest priority. */
30#define PRI_DEFAULT 31 /* Default priority. */
31#define PRI_MAX 63 /* Highest priority. */
32
33/*------------------------- [P2] System Call --------------------------*/
34#define FDT_PAGES 3 // fdt 할당시 필요한 페이지 개수
35#define FDCOUNT_LIMIT FDT_PAGES *(1<<9) // 3(테이블 개수) * 512(한 테이블 당 전체 엔트리 개수)
36// ↳ [comment] FDT_PAGES < 3 으로 하면 "multi-oom"테스트에서 터진다.
37/*
38 - 한 페이지 당 엔트리 개수가 512인 이유?
39 - 기본으로 설정된 페이지 하나의 사이즈 : PGSIZE(1 << 12) _ Ref. "threads/vaddr.h"
40 - PGSIZE / sizeof(struct file**)[= 8byte] = 512(개의 엔트리)
41*/
42
43/* A kernel thread or user process.
44 *
45 * Each thread structure is stored in its own 4 kB page. The
46 * thread structure itself sits at the very bottom of the page
47 * (at offset 0). The rest of the page is reserved for the
48 * thread's kernel stack, which grows downward from the top of
49 * the page (at offset 4 kB). Here's an illustration:
50 *
51 * 4 kB +---------------------------------+
52 * | kernel stack |
53 * | | |
54 * | | |
55 * | V |
56 * | grows downward |
57 * | |
58 * | |
59 * | |
60 * | |
61 * | |
62 * | |
63 * | |
64 * | |
65 * +---------------------------------+
66 * | magic |
67 * | intr_frame |
68 * | : |
69 * | : |
70 * | name |
71 * | status |
72 * 0 kB +---------------------------------+
73 *
74 * The upshot of this is twofold:
75 *
76 * 1. First, `struct thread' must not be allowed to grow too
77 * big. If it does, then there will not be enough room for
78 * the kernel stack. Our base `struct thread' is only a
79 * few bytes in size. It probably should stay well under 1
80 * kB.
81 *
82 * 2. Second, kernel stacks must not be allowed to grow too
83 * large. If a stack overflows, it will corrupt the thread
84 * state. Thus, kernel functions should not allocate large
85 * structures or arrays as non-static local variables. Use
86 * dynamic allocation with malloc() or palloc_get_page()
87 * instead.
88 *
89 * The first symptom of either of these problems will probably be
90 * an assertion failure in thread_current(), which checks that
91 * the `magic' member of the running thread's `struct thread' is
92 * set to THREAD_MAGIC. Stack overflow will normally change this
93 * value, triggering the assertion. */
94/* The `elem' member has a dual purpose. It can be an element in
95 * the run queue (thread.c), or it can be an element in a
96 * semaphore wait list (synch.c). It can be used these two ways
97 * only because they are mutually exclusive: only a thread in the
98 * ready state is on the run queue, whereas only a thread in the
99 * blocked state is on a semaphore wait list. */
100struct thread {
101 /* Owned by thread.c. */
102 tid_t tid; /* Thread identifier. */
103 enum thread_status status; /* Thread state. */
104 char name[16]; /* Name (for debugging purposes). */
105 int priority; /* Priority. */
106 /*------------------------- [P1] Alarm Clock & Priority Scheduling --------------------------*/
107 int priority_base; // donation 이후 우선순위 초기화를 위한 초기 우선 순위 값
108 struct lock *wait_on_lock; // 해당 스레드가 대기하고 있는 lock의 주소
109 int64_t wakeup_tick; // 해당 스레드를 깨워야하는 시간(local ticks)
110
111 /* Shared between thread.c and synch.c. */
112 struct list_elem elem; /* List element. */
113
114 struct list donations; // 해당 스레드에 priority donation 해준 스레드 리스트
115 struct list_elem d_elem; // donations 를 위한 elem
116
117
118/*------------------------- [P2] System Call --------------------------*/
119#ifdef USERPROG
120 /* Owned by userprog/process.c. */
121 uint64_t *pml4; /* Page map level 4 */
122 struct file **fdt; // 파일 디스크립터 테이블(프로세스당 개별적으로 존재)
123 int next_fd; // 다음 fd 인덱스
124
125 // Ref_92p. Hanyang Univ
126 struct intr_frame parent_if; // 부모 프로세스의 인터럽트 프레임
127 struct list child_list; // 자식 프로세스 리스트
128 struct list_elem child_elem; // 자식 프로세스 리스트의 element
129
130 struct file *running; // 현재 실행 중인 파일
131 int exit_status; // 프로세스의 종료 유무 확인
132
133 struct semaphore fork_sema; // fork가 완료될 때 sema_up 수행
134 struct semaphore free_sema; // 자식 프로세스가 종료될 때까지 부모 프로세스는 대기
135 struct semaphore wait_sema; // 자식 프로세스가 종료될 때까지 대기. 종료 상태 저장
136
137
138#endif
139#ifdef VM
140 /* Table for whole virtual memory owned by thread. */
141 /*-------------------------[P3]Anonoymous page---------------------------------*/
142 /* KAIST 15p. hash vm */
146 /*-------------------------[P3]Anonoymous page---------------------------------*/
147#endif
148
149 /* Owned by thread.c. */
150 struct intr_frame tf; /* Information for switching */
151 unsigned magic; /* Detects stack overflow. */
152};
153
154/* If false (default), use round-robin scheduler.
155 If true, use multi-level feedback queue scheduler.
156 Controlled by kernel command-line option "-o mlfqs". */
157extern bool thread_mlfqs;
158
159void thread_init (void);
160void thread_start (void);
161
162void thread_tick (void);
163void thread_print_stats (void);
164
165typedef void thread_func (void *aux);
166tid_t thread_create (const char *name, int priority, thread_func *, void *);
167
168void thread_block (void);
169void thread_unblock (struct thread *);
170
171struct thread *thread_current (void);
172tid_t thread_tid (void);
173const char *thread_name (void);
174
175void thread_exit (void) NO_RETURN;
176void thread_yield (void);
177
178int thread_get_priority (void);
179void thread_set_priority (int);
180
181int thread_get_nice (void);
183int thread_get_recent_cpu (void);
184int thread_get_load_avg (void);
185
186void do_iret (struct intr_frame *tf);
187
188/*------------------------- [P1] Alarm Clock & Priority Scheduling --------------------------*/
193bool cmp_priority (const struct list_elem *a, const struct list_elem *b, void *aux UNUSED);
194
195#endif /* threads/thread.h */
#define NO_RETURN
Definition: debug.h:8
#define UNUSED
Definition: debug.h:7
signed long long int int64_t
Definition: stdint.h:16
unsigned long long int uint64_t
Definition: stdint.h:29
Definition: file.c:7
Definition: interrupt.h:37
Definition: list.h:87
Definition: list.h:93
Definition: synch.h:20
Definition: synch.h:8
Definition: vm.h:115
Definition: thread.h:100
struct list_elem child_elem
Definition: thread.h:128
int priority
Definition: thread.h:105
struct lock * wait_on_lock
Definition: thread.h:108
int next_fd
Definition: thread.h:123
void * stack_bottom
Definition: thread.h:144
uint64_t * pml4
Definition: thread.h:121
struct list_elem d_elem
Definition: thread.h:115
int64_t wakeup_tick
Definition: thread.h:109
unsigned magic
Definition: thread.h:151
struct list_elem elem
Definition: thread.h:112
char name[16]
Definition: thread.h:104
struct supplemental_page_table spt
Definition: thread.h:143
tid_t tid
Definition: thread.h:102
struct list donations
Definition: thread.h:114
void * rsp_stack
Definition: thread.h:145
int priority_base
Definition: thread.h:107
struct intr_frame parent_if
Definition: thread.h:126
enum thread_status status
Definition: thread.h:103
int exit_status
Definition: thread.h:131
struct file * running
Definition: thread.h:130
struct semaphore free_sema
Definition: thread.h:134
struct intr_frame tf
Definition: thread.h:150
struct file ** fdt
Definition: thread.h:122
struct list child_list
Definition: thread.h:127
struct semaphore fork_sema
Definition: thread.h:133
struct semaphore wait_sema
Definition: thread.h:135
struct thread * thread_current(void)
Definition: thread.c:301
void thread_start(void)
Definition: thread.c:140
void thread_sleep(int64_t ticks)
Definition: thread.c:365
void thread_awake(int64_t ticks)
Definition: thread.c:405
int thread_get_nice(void)
Definition: thread.c:463
bool thread_mlfqs
Definition: thread.c:62
thread_status
Definition: thread.h:16
@ THREAD_BLOCKED
Definition: thread.h:19
@ THREAD_DYING
Definition: thread.h:20
@ THREAD_RUNNING
Definition: thread.h:17
@ THREAD_READY
Definition: thread.h:18
void thread_init(void)
Definition: thread.c:110
void thread_unblock(struct thread *)
Definition: thread.c:275
int thread_get_priority(void)
Definition: thread.c:441
bool cmp_priority(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED)
Definition: thread.c:449
int tid_t
Definition: thread.h:25
const char * thread_name(void)
Definition: thread.c:293
void do_iret(struct intr_frame *tf)
Definition: thread.c:577
int thread_get_recent_cpu(void)
Definition: thread.c:477
void thread_tick(void)
Definition: thread.c:156
void thread_yield(void)
Definition: thread.c:343
void thread_set_nice(int)
tid_t thread_create(const char *name, int priority, thread_func *, void *)
Definition: thread.c:197
int64_t get_global_ticks(void)
Definition: thread.c:390
tid_t thread_tid(void)
Definition: thread.c:317
void thread_set_priority(int)
Definition: thread.c:426
void thread_block(void)
Definition: thread.c:258
void thread_func(void *aux)
Definition: thread.h:165
void set_global_ticks(int64_t ticks)
Definition: thread.c:397
int thread_get_load_avg(void)
Definition: thread.c:470
void thread_print_stats(void)
Definition: thread.c:176
void thread_exit(void) NO_RETURN
Definition: thread.c:325
static int64_t ticks
Definition: timer.c:22