홈
임의 문서로
로그인
설정
Disclaimers
~ldmsys
검색
현재 모든 문서는 작성 중입니다. 보시는 데 불편함이 있더라도 양해 부탁드립니다.
운영체제 기본/Linux의 파일 읽기 문서 원본 보기
←
운영체제 기본/Linux의 파일 읽기
문서 편집 작업을 수행할 권한이 없습니다. 다음 이유를 확인해주세요:
요청한 작업은 다음 권한을 가진 사용자에게 제한됩니다:
사용자
.
문서의 원본을 보거나 복사할 수 있습니다.
[[운영체제 기본|목차]] · 관련 총론: [[운영체제 기본/추상화|추상화]], [[운영체제 기본/파일과 입출력|파일과 입출력]] 응용 프로그램이 <code>read(fd, buf, count)</code>를 호출하면 커널은 fd로 열린 파일 객체를 찾고, 그 객체에 연결된 읽기 구현을 호출한다. 기준은 '''Linux v6.12'''다. 일반적인 <code>read()</code> 경로를 대상으로 한다. <code>io_uring</code>은 별도 경로이며, [[운영체제 기본/Linux의 파일 메모리 매핑|mmap을 통한 접근]]은 주소 공간 설정과 페이지 폴트로 이어진다. == fd에서 struct file까지 == <pre> fd = 3 | +--> current->files (files_struct) | +--> fdt (fdtable) | +--> fd[3] --> struct file </pre> <code>current</code>는 현재 실행 중인 태스크를 나타낸다. <code>current->files</code>로 접근하는 <code>files_struct</code>에 파일 디스크립터 테이블이 연결되어 있고, <code>fdtable.fd</code>는 <code>struct file</code> 포인터의 배열이다. 같은 fd 값도 어느 테이블에서 찾는지에 따라 다른 열린 파일을 가리킬 수 있다. 파일 디스크립터 테이블의 참조 관계: <pre> files = current->files; table = files->fdt; opened_file = table->fd[fd]; </pre> 실제 <code>ksys_read()</code>는 <code>fdget_pos()</code>를 통해 객체를 얻는다. 그 아래의 <code>fdget()</code>, <code>__fget_light()</code>와 조회 함수들은 참조와 동시 접근을 처리한다. 위 그림에서는 이 부분을 생략했다. 직접 배열을 조회하는 코드에는 이러한 보호가 없으므로 실제 조회 함수를 대신할 수 없다. 소스: * [https://elixir.bootlin.com/linux/v6.12/source/include/linux/fdtable.h include/linux/fdtable.h]의 <code>fdtable</code>, <code>files_struct</code>, <code>files_lookup_fd_raw()</code><ref name="kernel-1">[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/fdtable.h?h=v6.12 Linux v6.12, include/linux/fdtable.h].</ref>. * [https://elixir.bootlin.com/linux/v6.12/source/fs/file.c fs/file.c]의 <code>__fget_light()</code>, <code>fdget_pos()</code><ref name="kernel-2">[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/file.c?h=v6.12 Linux v6.12, fs/file.c].</ref>. == struct file에서 읽기 연산까지 == <code>struct file</code>의 <code>f_op</code>는 연산 함수들을 모은 <code>file_operations</code>를 가리킨다. 파일마다 응용 프로그램의 호출 형태를 바꾸는 대신, 커널 안에서 해당 대상에 맞는 연산을 선택하는 구조다. <pre> struct file | +--> f_op (file_operations) | +--> read | +--> read_iter </pre> <code>ksys_read()</code>는 얻은 객체를 <code>vfs_read()</code>에 넘긴다. <code>vfs_read()</code>는 접근 모드, 사용자 버퍼 등에 관한 검사를 거쳐 읽기 구현을 선택한다. <pre> if (opened_file->f_op->read) opened_file->f_op->read(...); else if (opened_file->f_op->read_iter) new_sync_read(...); </pre> <code>new_sync_read()</code>는 <code>read_iter</code> 호출에 필요한 요청과 버퍼 정보를 구성한다. 사용 가능한 함수 포인터에 따라 한 경로를 선택한다. 소스: * [https://elixir.bootlin.com/linux/v6.12/source/include/linux/fs.h include/linux/fs.h]의 <code>struct file</code>, <code>struct file_operations</code><ref name="kernel-3">[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/fs.h?h=v6.12 Linux v6.12, include/linux/fs.h].</ref>. * [https://elixir.bootlin.com/linux/v6.12/source/fs/read_write.c fs/read_write.c]의 <code>ksys_read()</code>, <code>vfs_read()</code>, <code>new_sync_read()</code><ref name="kernel-4">[https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/read_write.c?h=v6.12 Linux v6.12, fs/read_write.c].</ref>. == 파일시스템으로 넘어가는 경계 == <code>vfs_read()</code>는 [[운영체제 기본/Linux의 파일시스템/VFS와 파일 객체|VFS의 공통 파일 연산]]을 통해 실제 구현으로 연결된다. 열린 파일에 어떤 연산표가 설정되는지는 <code>open()</code>과 파일시스템의 객체 구성에서 결정된다. 응용 프로그램에는 '''fd와 읽기 인터페이스'''가 보인다. 커널 내부에서는 '''테이블 조회로 객체를 찾고, 연산 테이블로 구현을 선택'''한다. 다만 함수 포인터를 호출하는 것만으로 읽기가 끝나는 것은 아니다. 선택된 구현 아래에서 파일시스템, 캐시, 장치 드라이버 등의 처리가 이어질 수 있다. [[운영체제 기본/Linux의 파일시스템/ext4의 파일 읽기|ext4의 일반 파일 읽기]]는 공통 페이지 캐시로 이어진다. [[운영체제 기본/Linux의 파일시스템/procfs의 파일 읽기|/proc/uptime 읽기]]는 커널 상태로 문자열을 만든다. 같은 <code>read()</code>를 호출해도 데이터를 얻는 경로가 달라지는 지점이다. == 관련 각론 == * [[운영체제 기본/Linux의 파일시스템|Linux의 파일시스템]]: VFS, ext4, procfs와 공통 페이지 캐시. * [[운영체제 기본/Linux의 파일 메모리 매핑|mmap을 통한 파일 읽기]]: 주소 영역 등록, 첫 메모리 접근, 파일 폴트와 PTE 설치. == 참고 문헌 == <references />
운영체제 기본/Linux의 파일 읽기
문서로 돌아갑니다.