반응형
/* The main system call interface */
void
syscall_handler (struct intr_frame *f UNUSED) {
// TODO: Your implementation goes here.
uint64_t syscall_no = f->R.rax; // 콜 넘버
// uint64_t a1 = f->R.rdi; // 파일 네임
// uint64_t a2 = f->R.rsi; // v(데이터)
// uint64_t a3 = f->R.rdx; // 사이즈
// uint64_t a4 = f->R.r10;
// uint64_t a5 = f->R.r8;
// uint64_t a6 = f->R.r9;
switch (f->R.rax) { // rax is the system call number
char *fn_copy;
// 핀토드 종료 시스템 콜
case SYS_HALT :
power_off();
break;
//프로세스 종료 시스템 콜
case SYS_EXIT :
exit_syscall (f->R.rdi);
break;
case SYS_FORK :
f->R.rax = fork_syscall(f->R.rdi, f);
break;
//프로세스 생성
case SYS_EXEC :
f->R.rax = exec_syscall(f->R.rdi);
break;
case SYS_WAIT :
f->R.rax = wait_syscall(f->R.rdi);
break;
// 파일 이름과 파일 사이즈를 인자 값으로 받아 파일을 생성하는 함수.
case SYS_CREATE :
f->R.rax = create_syscall(f->R.rdi, f->R.rsi);
break;
case SYS_REMOVE :
f->R.rax = remove_syscall(f->R.rdi);
break;
case SYS_OPEN :
f->R.rax = open_syscall(f->R.rdi);
break;
case SYS_FILESIZE :
f->R.rax = filesize_syscall(f->R.rdi);
break;
case SYS_READ :
f->R.rax = read_syscall (f->R.rdi, f->R.rsi, f->R.rdx);
break;
case SYS_WRITE :
f->R.rax = write_syscall(f->R.rdi, f->R.rsi, f->R.rdx);
break;
case SYS_SEEK :
// hong_dump_frame (f);
seek_syscall (f->R.rdi, f->R.rsi);
break;
case SYS_TELL :
f->R.rax = tell_syscall (f->R.rdi);
break;
case SYS_CLOSE :
close_syscall(f->R.rdi);
break;
}
}
static void
__do_fork (void *aux) {
struct intr_frame if_;
struct thread *parent = (struct thread *) aux;
struct thread *current = thread_current (); //자식 프로세스임
/* TODO: somehow pass the parent_if. (i.e. process_fork()'s if_) */
struct intr_frame *parent_if;
parent_if = &parent->parent_if;
bool succ = true;
/* 1. Read the cpu context to local stack. */
memcpy (&if_, parent_if, sizeof (struct intr_frame));
if_.R.rax = 0;
/* 2. Duplicate PT */
current->pml4 = pml4_create();
if (current->pml4 == NULL)
goto error;
process_activate (current);
#ifdef VM
supplemental_page_table_init (¤t->spt);
if (!supplemental_page_table_copy (¤t->spt, &parent->spt))
goto error;
#else
if (!pml4_for_each (parent->pml4, duplicate_pte, parent))
goto error;
#endif
/* TODO: Your code goes here.
* TODO: Hint) To duplicate the file object, use `file_duplicate`
* TODO: in include/filesys/file.h. Note that parent should not return
* TODO: from the fork() until this function successfully duplicates
* TODO: the resources of parent.*/
/* 힌트) 파일 객체를 복제하려면 include/filesys/file.h에서 `file_duplicate`를 사용하세요.
이 함수가 부모의 리소스를 성공적으로 복제할 때까지 부모는 fork()에서 반환해서는 안 됩니다.
*/
if(parent -> fdidx >= MAX_FD_NUM){
goto error;
}
current -> file_descriptor_table[0] = parent->file_descriptor_table[0];
current -> file_descriptor_table[1] = parent->file_descriptor_table[1];
for (int i = 2; i < MAX_FD_NUM; i++){
struct file *f = parent->file_descriptor_table[i];
if (f == NULL){
continue;
}
current -> file_descriptor_table[i] = file_duplicate(f);
}
current -> fdidx = parent -> fdidx;
sema_up(¤t -> sema_fork);
process_init ();
/* Finally, switch to the newly created process. */
if (succ)
do_iret (&if_);
error:
// thread_exit ();
exit_syscall(-1);
}
int
process_wait (tid_t child_tid UNUSED) {
/* XXX: 힌트) pintos exit if process_wait(initd), process_wait를 구현하기 전에 여기에
무한 루프를 추가하는 것이 좋습니다. */
struct thread *child = get_child(child_tid); //넘어온 tid 값과 같은 자식 리스트의 스레드를 가져온다.
if (child == NULL){ //없다면 리턴 -1
return -1;
}
if (child->is_waited){ //아직 기다리라고 한 자식이면 리턴 -1
return -1;
}
else { //자식이 있고 기다리라고 했던 적이 없다면
child -> is_waited = true; //자식을 기다리라고 한다.
}
sema_down(&child -> sema_wait); //자식이 wait 상태인동안 인터럽트 활성화
int exit_status = child -> exit_status;
list_remove(&child->child_list_elem); //자식 제거
sema_up(&child -> sema_free); //free할 수 있도록 인터럽트 해제
// while (1){}
// thread_set_priority(thread_get_priority()-1);
return exit_status; // 종료 상태를 리턴
}
반응형
'CS > Computer System' 카테고리의 다른 글
2. PINTOS :: project 2 - User Program (0) | 2024.10.07 |
---|---|
2. PINTOS :: project 1 - Priority Scheduling (1) | 2024.10.01 |
1. PINTOS :: project 1 - alarm (0) | 2024.10.01 |
[C언어] csapp 11장 Homework (0) | 2024.09.18 |
[C언어] tiny 웹 서버 구현하기 (0) | 2024.09.17 |