190 likes | 203 Views
Learn about the basics of UNIX file I/O functions, such as open, read, write, lseek, and close. Explore file descriptors, unbuffered IO, and file sharing in UNIX systems. Develop a program to display error number descriptions.
E N D
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 • not kernal function • POSIX maps to STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO • max FD’s system dependent (OPEN_MAX)
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
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
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
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)
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
write Function • writes to open file • takes FD, *buff, # of bytes • returns bytes written, -1 on error
File Sharing • UNIX allows processes to share open files • kernal uses three structures for each file • process FD table • file table • v-node table
process FD table • file descriptor flags • pointer to file table entry
file table • file status flags • current file offset (lseek) • pointer to v-node table entry
v-node table • file type information • functions that operate on file • i-node information • current file size
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
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
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
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
ioctl Function • No single standard • takes FD, request • possible requests taken from device specific headers • no single standard
/dev/fd • Primarily intended to ‘clean up’ shell pipelines • opening the path /dev/fd/n equivalent to duplicating FD n • shared file table entry
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.