μμ€ν
μ½μ ν΅ν΄ read(fd, buf, size)μ²λΌ νΈμΆμ΄ λ€μ΄μ€λ©΄,
struct file *μ μ°Ύμ ν΄λΉ νμΌμ μ κ·Όνλ€.μ μ λ 컀λ λ΄λΆ ꡬνμ λͺ°λΌλ νμΌμ μ‘°μν μ μλ€.
| FD λ²νΈ | μλ―Έ |
|---|---|
| 0 | νμ€ μ λ ₯ (stdin) |
| 1 | νμ€ μΆλ ₯ (stdout) |
| 2 | νμ€ μλ¬ (stderr) - μ¬μ©νμ§ μμ |
| 3β | μ μ κ° openμΌλ‘ μ° νμΌλ€ |
PintOSμμλ κ° μ€λ λκ° μμ λ§μ FD ν μ΄λΈμ κ°λλ€.
struct file **fd_table; // μ΄λ¦° νμΌλ€μ κ°λ¦¬ν€λ ν¬μΈν° λ°°μ΄
int fd_idx; // λ€μμ ν λΉν FD λ²νΈ (λ³΄ν΅ 3λΆν° μμ)
fd_table[3] = νμΌ Aμ ν¬μΈν°fd_table[4] = νμΌ Bμ ν¬μΈν°int fd = open("test.txt");
-> 컀λ λ΄λΆ λμ:
"test.txt"λΌλ μ΄λ¦μ νμΌμ΄ μ‘΄μ¬νλμ§ νμΈfilesys_open()μΌλ‘ νμΌμ μ°λ€. μ±κ³΅ μ struct file *fκ° λ°νλλ€.fd_table[fd_idx] = fλ‘ μ μ₯νκ³ , fd_idx++μ μ μ μ₯μμ νμΌμ΄ μλλΌ μ«μλ§ λ°μ μ
컀λμ κ·Έ μ«μλ₯Ό λ³΄κ³ ν΄λΉ νμΌμ μ°Ύμ λμν¨
write(fd, buf, 100);
-> 컀λ λ΄λΆ λμ:
fdκ° fd_table λ²μ μμ μλμ§ νμΈstruct file *μ΄ μ‘΄μ¬νλμ§ νμΈfile_write(f, buf, 100) μν컀λμ
fd β struct file *λ³ννλ κ³Όμ μ κ±°μΉλ€
close(fd);
-> 컀λ λ΄λΆ λμ:
fd_table[fd]κ° NULLμ΄ μλμ§ νμΈfile_close(fd_table[fd])λ‘ νμΌ μμ ν΄μ fd_table[fd] = NULLλ‘ μ²λ¦¬ν΄ ν΄λΉ FDλ μ¬μ¬μ© κ°λ₯νκ² λ§λ λ€if (fd < 0 || fd >= FD_LIMIT) return -1;
if (fd_table[fd] == NULL) return -1;
PANIC λ°μ κ°λ₯μ± μμopen():
read()/write():
close():
| μμ€ν μ½ | κ΄λ ¨ ν μ€νΈ μμ |
|---|---|
| open() | open-normal, open-null, open-bad-ptr, open-twice |
| close() | close-normal, close-twice, close-bad-fd |
| read() | read-normal, read-bad-fd, read-zero |
| write() | write-normal, write-bad-fd, write-zero |
open β write/read β close μ 체 νλ¦μ μ΄ν΄νκ³ κ΅¬νν΄μΌ μ μμ μΈ ν
μ€νΈ ν΅κ³Όκ° κ°λ₯νλ€.