void exit (int status);
해당 프로세스를 종료하는 시스템 콜이다. 인자로 들어오는 status는 종료의 상태 정보를 갖고 있다. 때때로 일부 프로세스들은 다른 프로세스가 종료되기를 기다리기 때문에 구조체 내에 해당 스레드(프로세스) 종료 상태를 나타내는 exit_code라는 변수를 추가해둔 후 이 status 값이 스레드의 exit_code 에 저장되도록 할 것이다.
/* thread.h 에서 thread 구조체 정의 부분 */
short exit_code; //* 쓰레드가 종료할떄 상태인 exit_code
exit이 호출되면 시스템콜 핸들러에 의해 syscall_exit 이 실행된다.
// 해당 프로세스 종료
void syscall_exit(struct intr_frame *f){
thread_current()->exit_code = f->R.rdi;
thread_exit();
}
특정 프로세스가 종료되었는지 여부를 알 수 있도록 스레드 구조체 내에 exit_code 멤버변수에 exit 시스템콜에서 인자로 받은 값을 추가해 준 후 thread_exit()
호출을 통해서 해당 프로세스를 종료한다.
void thread_exit(void)
{
ASSERT(!intr_context());
#ifdef USERPROG
process_exit();
#endif
intr_disable();
do_schedule(THREAD_DYING);
NOT_REACHED();
}
/* Deschedules the current thread and destroys it. Never
returns to the caller. */
void thread_exit(void)
{
ASSERT(!intr_context());
#ifdef USERPROG
process_exit();
#endif
/* Just set our status to dying and schedule another process.
We will be destroyed during the call to schedule_tail(). */
intr_disable();
do_schedule(THREAD_DYING);
NOT_REACHED();
}
유저 프로세스의 종료를 다루는 함수. 프로세스가 종료할 때는 부모 프로세스가 확인할 수 있도록 child_info의 exit_code 값을 업데이트해주고, 현재 스레드의 children_list 및 fd_list를 지우고 열려있는 파일을 닫은 후 free 해준다.
또한 현재 스레드가 실행중인 파일이 있다면 해당 파일을 close하고 thread의 exec_file 도 NULL로 업데이트 해준다. 이후 process_cleanup을 해준 후 sema_up을 하여 종료를 기다리고 있는 부모 스레드를 깨워준다.
/* Exit the process. This function is called by thread_exit (). */
void process_exit(void)
{
struct thread *curr = thread_current();
struct thread *parent = thread_current()->parent_thread;
update_child_exit_code();
clear_children_list();
clear_fd_list();
if (curr->pml4 > KERN_BASE)
printf("%s: exit(%d)\\n", curr->name, curr->exit_code);
if (thread_current()->exec_file != NULL)
{
file_close(thread_current()->exec_file);
thread_current()->exec_file = NULL;
}
process_cleanup();
sema_up(&parent->wait_sema);
}