πŸ“€ KAIST:PINTOS | Implementation | open

μ΄μˆœκ°„Β·2025λ…„ 5μ›” 22일

KAIST:PINTOS

λͺ©λ‘ 보기
20/23
pass tests/userprog/open-normal  
pass tests/userprog/open-missing  
pass tests/userprog/open-boundary  
pass tests/userprog/open-empty  
pass tests/userprog/open-null  
pass tests/userprog/open-bad-ptr  
pass tests/userprog/open-twice  

λͺ©ν‘œ

int sys_open(const char *file);

  • μœ μ € ν”„λ‘œκ·Έλž¨μ΄ open()을 ν˜ΈμΆœν–ˆμ„ λ•Œ
  • 파일 μ‹œμŠ€ν…œμ—μ„œ ν•΄λ‹Ή μ΄λ¦„μ˜ νŒŒμΌμ„ μ°Ύμ•„ μ—΄κ³ 
  • 파일 λ””μŠ€ν¬λ¦½ν„°(fd)λ₯Ό λ°˜ν™˜
  • μ‹€νŒ¨ μ‹œ -1 λ°˜ν™˜

μˆ˜μ •ν•œ 파일

  • threads/thread.h
  • threads/thread.c
  • userprog/syscall.c
  • userprog/syscall.h

1. thread ꡬ쑰체에 파일 λ””μŠ€ν¬λ¦½ν„° ν…Œμ΄λΈ” μΆ”κ°€

파일: threads/thread.h

#define FD_MAX 128 // μ—΄λ¦° 파일의 μ΅œλŒ€ 수

struct thread {
  ...
  struct file *fd_table[FD_MAX]; // μ—΄λ¦° 파일 ν…Œμ΄λΈ”
  int next_fd; // λ‹€μŒμœΌλ‘œ ν• λ‹Ήν•  fd
  ...
};

2. μŠ€λ ˆλ“œ μ΄ˆκΈ°ν™” μ½”λ“œμ— μ΄ˆκΈ°κ°’ μ„€μ •

파일: threads/thread.c

ν•¨μˆ˜: init_thread(struct thread *t, ...)

t->next_fd = 2;
for (int i = 0; i < FD_MAX; i++)
  t->fd_table[i] = NULL;

3. syscall.c에 sys_open ν•¨μˆ˜ κ΅¬ν˜„

파일: userprog/syscall.c

int sys_open(const char *file) {
  if (file == NULL || !is_user_vaddr(file) || pml4_get_page(thread_current()->pml4, file) == NULL)
    sys_exit(-1); // 포인터 μœ νš¨μ„± 검사

  lock_acquire(&file_lock); // 락 νšλ“
  struct file *f = filesys_open(file); // 파일 μ—΄κΈ°
  lock_release(&file_lock); // 락 ν•΄μ œ

  if (f == NULL)
    return -1; // 파일 μ—†μœΌλ©΄ μ‹€νŒ¨

  struct thread *cur = thread_current();
  int fd = cur->next_fd;

  while (fd < FD_MAX && cur->fd_table[fd] != NULL)
    fd++; // λΉ„μ–΄ μžˆλŠ” fd μ°ΎκΈ°

  if (fd >= FD_MAX)
    return -1; // μ—΄ 수 μžˆλŠ” 파일 초과

  cur->fd_table[fd] = f;
  cur->next_fd = fd + 1;

  return fd; // fd λ°˜ν™˜
}

4. syscall_handler에 open 등둝

파일: userprog/syscall.c

case SYS_OPEN:
  f->R.rax = sys_open((const char *)f->R.rdi);
  break;

5. syscall.h에 ν•¨μˆ˜ μ„ μ–Έ μΆ”κ°€

파일: userprog/syscall.h

int sys_open(const char *file);

ν…ŒμŠ€νŠΈ κ²°κ³Ό

ν•΄λ‹Ή κ΅¬ν˜„μ„ 톡해 μ•„λž˜ ν…ŒμŠ€νŠΈλ₯Ό ν†΅κ³Όν•˜μ˜€λ‹€.

  • open-normal
  • open-missing
  • open-boundary
  • open-empty
  • open-null
  • open-bad-ptr
  • open-twice

파일 이름 μœ νš¨μ„±, 포인터 검사, 쀑볡 μ˜€ν”ˆ 처리 λ“± λ‹€μ–‘ν•œ 경우λ₯Ό 닀루고 있으며
fd_tableκ³Ό next_fd의 μ˜¬λ°”λ₯Έ 섀계와 μ˜ˆμ™Έ μ²˜λ¦¬κ°€ μ€‘μš”ν•˜λ‹€.


κ²°λ‘ 

open μ‹œμŠ€ν…œ μ½œμ„ κ΅¬ν˜„ν•˜λ©΄μ„œ μœ μ € 포인터 검증, 락 동기화,
파일 λ””μŠ€ν¬λ¦½ν„° ν…Œμ΄λΈ” μ„€κ³„λΌλŠ” μ€‘μš”ν•œ ꡬ쑰듀을 이해할 수 μžˆμ—ˆλ‹€.
μ΄λŠ” 이후 read, write, fork κ΅¬ν˜„μ˜ 기반이 λ˜λŠ” 핡심 μš”μ†Œμ΄λ‹€.
이제 close μ‹œμŠ€ν…œ 콜 κ΅¬ν˜„μœΌλ‘œ λ„˜μ–΄κ°ˆ 수 μžˆλ‹€.

profile
μ„œνˆ΄μ§€μ–Έμ • 늘 행동이 먼저이기λ₯Ό

0개의 λŒ“κΈ€