1 / 19

File I/O

File I/O. Five basic UNIX functions open read write lseek close Unbuffered IO kernel calls. File Descriptors . Files tracked by file descriptors non-negative integers (positive or 0) open, creat return FD read, write, lseek use FD shells use 0, 1, 2 as stdin, stdout, stderr

dougal
Download Presentation

File I/O

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. File I/O • Five basic UNIX functions • open • read • write • lseek • close • Unbuffered IO • kernel calls

  2. File Descriptors • Files tracked by file descriptors • non-negative integers (positive or 0) • open, creat return FD • read, write, lseek use FD • shells use 0, 1, 2 as stdin, stdout, stderr • not kernal function • POSIX maps to STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO • max FD’s system dependent (OPEN_MAX)

  3. open Function • Open or create files • called with pathname, oflag, mode • pathname is pathname of file to open/create • oflag is options applied • mode is file attribute set • returns lowest numbered FD available, -1 on error

  4. creat Function • can be used to create new files • takes pathname, mode • creates file in ‘write’ mode only • outdated by new options to ‘open’ • returns FD or -1 on error

  5. close Function • closes open files • takes FD • releases record locks • program termination closes all open files in UNIX • returns 0 on success, -1 on error

  6. lseek Function • all open files have ‘current file offset’ • normally non-negative # of bytes from beginning of file • set to zero on file open, assuming no O_APPEND • pointer to location in file for next read/write • takes FD, offset, whence • whence specifies option for how offset is updated • SEEK_SET, relative to beginning of file • SEEK_CUR, relative to current value • SEEK_END, relative to end of file • offset positive or negative • returns new offset or -1 for error (check explicitly for -1)

  7. read Function • reads data from open file • takes FD, *buff, # of bytes • returns # of bytes read, 0 on end of file, -1 on error • can return less than # of bytes requested

  8. write Function • writes to open file • takes FD, *buff, # of bytes • returns bytes written, -1 on error

  9. File Sharing • UNIX allows processes to share open files • kernal uses three structures for each file • process FD table • file table • v-node table

  10. process FD table • file descriptor flags • pointer to file table entry

  11. file table • file status flags • current file offset (lseek) • pointer to v-node table entry

  12. v-node table • file type information • functions that operate on file • i-node information • current file size

  13. File Sharing Model • process table entries map to file table entries • file table entries map to v-node table entries • processes sharing files have separate file table entries for each shared file • ‘relative to process’ attributes: such as current file offset • each open file has one v-node entry, regardless of how many processes are sharing

  14. Atomic Operations • Atomic operations solve the problem of overlapping operations by separate processes • Atomic operations represent indivisible collections of separate functions, all performed together, or none performed at all

  15. dup and dup2 Functions • used to duplicate FD’s • dup takes FD • dup2 takes FD, new FD • dup returns next available FD, -1 on error • dup2 returns specified new FD, -1 on error • dup2 atomic

  16. fcntl Function • changes properties of open file • dup existing FD • get/set FD flags (process table) • get/set file status flags (file table) • get/set async I/O ownership • get/set record locks • takes FD, cmd, cmd dependent val • return dependent on command, -1 on error

  17. ioctl Function • No single standard • takes FD, request • possible requests taken from device specific headers • no single standard

  18. /dev/fd • Primarily intended to ‘clean up’ shell pipelines • opening the path /dev/fd/n equivalent to duplicating FD n • shared file table entry

  19. Write a program that will do the following: • Print to standard out the associated descriptive text of any given error number input as an argument, or a list of all possible error numbers and descriptions if given a particular flag • Input will be either an error number, or a flag as follows: • –help • print a usage statement to standard out • –all • print a full list of possible error numbers and their descriptions to standard out • Any input NOT within the range of valid error numbers will generate an error message (including a list of defined numbers), and the usage message to standard out. Your program will need to determine all defined error numbers. For purposes of this project, you need not check for definition beyond the range 0-1000.

More Related