1 / 31

Advanced functions for file

제 09 강 : Advanced Functions for File. Advanced functions for file. Reference: Stevens, Advanced Programming in UNIX, Addison Wesley. Two methods of accessing files. case (1) data file. sys call. user. kernel. case (2) a.out file in paging system. page fault. user.

kenny
Télécharger la présentation

Advanced functions for file

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제09강 : Advanced Functions for File Advanced functions for file Reference: Stevens, Advanced Programming in UNIX, Addison Wesley.

  2. Two methods of accessing files case (1) data file sys call user kernel case (2) a.out file in paging system page fault user

  3. Memory Mapped I/O • Problems with read(), write() system call • system call invocation overhead • trap, check permission on every invocation, … • data copy overhead • device  kernel buf  user var • memory space • data delivered to N a.out’s (N processes share a file) • Can we treat “file read” as page fault, not read(2)? Ref Ch 12.9. Stevens, Advanced Programming in UNIX, Addison Wesley.

  4. Process Image To map, simply call mmap(2) mmap (start, length, fd, offset, ….) heap Uninitialized data (bss) offset fd: Initialized data Text len

  5. Process Image To map, simply call mmap(2) mmap (start, length, fd, offset, ….) heap Uninitialized data (bss) offset fd: Initialized data this portion is memory mapped P1 P2 P3 Text len

  6. Process Image To map, simply call mmap(2) mmap (start, length, fd, offset, ….) start: P1 P2 P3 this portion is memory mapped len heap Uninitialized data (bss) offset fd: Initialized data this portion is memory mapped P1 P2 P3 Text len

  7. Process Image To map, simply call mmap(2) mmap (start, length, fd, offset, ….) start: P1 P2 P3 this portion is memory mapped len m1 heap Uninitialized data (bss) offset fd: Initialized data this portion is memory mapped P1 P2 P3 Text len

  8. After mmap(), you use memory access interface such as: “read data from memory location 1234” instead of explicitly invoking read(2), write(2) then kernel automatically access file. ** System calls are expensive due to: invocation overhead, data copy, permission, .. Process Image 1234 start: P1 P2 P3 this portion is memory mapped len heap Uninitialized data (bss) offset fd: this portion is memory mapped Initialized data P1 P2 P3 Text len

  9. Advantage • faster read, write • fewer data copying • single copy in memory is shared by all • space, page faults, … • update is immediately seen by other processes • Disadvantage • lose OS kernel file system call support • protection • consistency in concurrent updates • atomicity of access • some files cannot be mapped -- network, terminal, ...

  10. readv(), writev() scattered read gather write main memory buffer 1: text addr length (x) fd: buffer 2: data addr length a.out (rw) addr length buffer 4: stack

  11. Non-blocking I/O • Some file system functions -- “slow return” • open(modem) returns when modem is active • read(pipe) returns when data written • read(shared file) returns when record is unlocked* • what if we click icon and it never returns? • “slow” system call • system call that can block forever * man flock Ref Ch 12.2. Stevens, Advanced Programming in UNIX, Addison Wesley.

  12. Specify the file as “non-blocking” • open( … O_NONBLOCK), or • fcntl(… O_NONBLOCK), if already open • If read (or write, open) cannot be completed, return immediately with error flag noting that “operation would have blocked” “there is no data yet”

  13. I/O Subsystem

  14. File vs I/O Process 1 user inode offset File I/O devswtab disk drivers tty drivers lp drivers

  15. user a.out my code invokes getchar () library which invokes read() system call system call read(fd) access struct user get offset get inode get device number access devswtab access device queue insert into dev queue start device device interrupt & read dev kernel a.out File subsystem I/O subsystem

  16. file R/W devswtab • Size of device drivers • very big (majority) -- so many devices • still growing fast • strength of Linux -- supports many dev • devswtab[dev, op] -- (2-dimensional array) • offers hardware independence • Two type of device drivers • block dev sw table dwrite, reada (asynchronously) • char dev sw table synchronous read/write eg tape-to-disk copy, tty, ... dev I/O

  17. Size of device driver code • Linux kernel version 2.0 • 470 K lines C • 8 K lines assembler • Proportions* C Asm device drivers 377K 100 network 25K VFS 13K 13 file system 50K init 4K 2800 * Source: M. Beck et. al., Linux Kernel Internals, 2nd Ed., Addison-Wesley

  18. Caution: terminology “block dev” does not mean “transfer in units of block” “char dev” does not mean “transfer in units of char” “char” means “not use buf cache” -- hence “no dwrite, no reada”, synchronous r/w “block” means “use buf cache” -- hence “dwrite, reada”, and asynchronous r/w Same disk can be operated either as block dev of char dev eg file system partition -- block device swap partition -------- char device

  19. Terminal I/O

  20. tty meta-data Process 1 Process 2 Process 3 PCB PCB PCB Output queue Raw (Input) queue hello _ % CPU mem disk tty metadata CPU mem disk tty

  21. tty-metadata content kernel metadata line speed, parity error code echo erase, kill … screen size (row, col) code to move cursor code to scroll screen … • Used by kernel (interrupt handler) • struct termios (in kernel) • Used by any code (editor, game …) • terminfo  (regular file)

  22. Split tty meta-data (kernel struct & regular file) Process 1 Process 2 Process 3 PCB PCB PCB kernel struct regular file CPU mem disk termios terminfo CPU mem disk

  23. Accessing termios • From (program) library fuctions • tcgetattr(3) get current setting • tcsetattr(3) modify these setting • from (terminal) shell • stty(1) • stty -a to see current setting erase, eof, stop, echoing, …? kernel termios terminfo

  24. Echoing Process 1 Process 2 PCB PCB program output echo hello _ % CPU mem disk tty metadata CPU mem disk

  25. echoing My Program stty -echo stty echo program output echo kernel Output queue Raw queue (Input ) metadata

  26. When to wake up program sh vi -------- Line editing ---------- $ I am a boy ^H char backspace $ I am a boy ^W word delete $ I am a boy ^U line erase When any char input No line editing [raw mode] When a line is entered Line editing allowed [cooked mode] hello _ % tty metadata

  27. raw/canonical modes • (1) Raw mode • eg game, vi editor • Program responds to each input character • program is waken up even by each character • (2) Canonical (Cooked) mode • eg shell • Program responds only if full line is input • line editing (delete char, line) --- by kernel • program is waken up by complete line

  28. Raw mode game Any characters Output queue echo Raw queue

  29. Canonical mode shell One line Canon queue Output queue echo line edit Raw queue

  30. Standard code for tty future tty’s modern tty’s original tty size font brightness color Other features move scroll blink erase …… still changing ….. space char’s were only thing that could be standardized ASCII code standard

  31. terminfo kernel • Codes for unprintable characters were not standardized • other “terminal capabilities”: • screen size col, row • control char to move the cursor, to scroll the screen, to erase part of the screen to change appearance (colors, bright, … etc.) • tells programs what this terminal is capable of doing. • terminfo file ---- one file for each terminal type • /usr/lib/terminfo/v/vt100 • environment variable for terminal type: TERM • try echo $TERM • DB of terminal capabilities (formerly termcap) termios terminfo

More Related