pass tests/userprog/close-normal
pass tests/userprog/close-twice
pass tests/userprog/close-bad-fd
void sys_close(int fd);
close(fd)๋ฅผ ํธ์ถํ์ ๋userprog/syscall.cuserprog/syscall.hโป open() ๊ตฌํ ์ ์์ฑํ fd_table[]๊ณผ next_fd ๊ตฌ์กฐ๋ฅผ ๊ทธ๋๋ก ํ์ฉํจ
ํ์ผ: userprog/syscall.c
void sys_close(int fd) {
if (fd < 2 || fd >= FD_MAX) return; // ์ ํจํ fd์ธ์ง ํ์ธ
struct thread *cur = thread_current(); // ํ์ฌ ์ค๋ ๋ ๊ฐ์ ธ์ค๊ธฐ
struct file *f = cur->fd_table[fd]; // fd์ ํด๋นํ๋ ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
if (f == NULL) return; // ์ด๋ ค ์๋ ํ์ผ์ด ์์ผ๋ฉด ๋ฌด์
lock_acquire(&file_lock); // ํ์ผ ์์คํ
๋ฝ ํ๋
file_close(f); // ํ์ผ ๋ซ๊ธฐ
lock_release(&file_lock); // ๋ฝ ํด์
cur->fd_table[fd] = NULL; // fd ํ
์ด๋ธ์์ ์ ๊ฑฐ
}
ํ์ผ: userprog/syscall.c
case SYS_CLOSE:
sys_close((int)f->R.rdi);
break;
ํ์ผ: userprog/syscall.h
void sys_close(int fd);
ํด๋น ๊ตฌํ์ ํตํด ์๋ ํ ์คํธ๋ฅผ ํต๊ณผํ์๋ค.
์ด ํ
์คํธ๋ค์ fd๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ์ ์ ํํ ๋ซ๋์ง,
์ค๋ณต close ์ ์๋ฌ ์์ด ๋ฌด์๋๋์ง,
์๋ชป๋ fd๊ฐ ๋ค์ด์๋ ์ปค๋ ํจ๋ ์์ด ๋์ด๊ฐ๋์ง๋ฅผ ํ์ธํ๋ค.
close ์์คํ
์ฝ์ open()์์ ์ค๊ณํ fd_table ๊ตฌ์กฐ๋ฅผ ํ์ฉํด
์์ ํด์ ๋ฐ ํ์ผ ํธ๋ค ๊ด๋ฆฌ๋ฅผ ์์ฑํ๋ ๊ณผ์ ์ด์๋ค.
๊ฐ๋จํ์ง๋ง ๊ผญ ํ์ํ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํฌํจํด์ผ ํ๋ฉฐ,
ํ์ผ ์์คํ
์ ๊ทผ ์ ๋๊ธฐํ๋ฅผ ์ฒ ์ ํ ํด์ผ ์์ ์ ์ธ ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค.
์ด์ read, write ์์คํ
์ฝ ๊ตฌํ์ผ๋ก ํ์ฅํ ์ ์๋ค.