Supplementary-standard-IO - Unix Files A Unix file is a...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS 3214 Computer Systems Supplementary Material on Unix System Calls and Standard I/O Unix Files • A Unix file is a sequence of m bytes: B 0 , B 1 , .... , B k , .... , B m-1 • All I/O devices are represented as files: – /dev/sda2 ( /usr disk partition) – /dev/tty2 (terminal) • Even the kernel is represented as a file: – /dev/kmem (kernel memory image) – /proc (kernel data structures) CS 3214 Unix File Types • Regular file – Binary or text file. – Unix does not know the difference! • Directory file – A file that contains the names and locations of other files. • Character special and block special files – Terminals (character special) and disks(block special) • FIFO (named pipe) – A file type used for interprocess comunication • Socket – A file type used for network communication between processes CS 3214 Unix I/O • The elegant mapping of files to devices allows kernel to export simple interface called Unix I/O. • Key Unix idea: All input and output is handled in a consistent and uniform way. • Basic Unix I/O operations (system calls): – Opening and closing files • open() and close() – Changing the current file position (seek) • lseek ()– side note: Use off_t – may be 64bits! – Reading and writing a file • read() and write() CS 3214 Opening Files • Opening a file informs the kernel that you are getting ready to access that file. • Returns a small identifying integer file descriptor fd == -1 indicates that an error occurred • Each process created by a Unix shell begins life with three open files associated with a terminal: – 0: standard input – 1: standard output – 2: standard error int fd; /* file descriptor */ if ((fd = open(“/etc/hosts”, O_RDONLY)) < 0) { perror(“open”); exit(1); } CS 3214 A note on Unix errors • Most Unix system functions return 0 on success – Notable exceptions: write, read, lseek – Always check the section “RETURN VALUE” in man page if in doubt • If return value indicates error, a global variable ‘errno’ is set with more information – errno.h include symbolic constants – perror() prints the error in a standard form • Error values can be unspecific at times • Reminder: in the project, we will deduct points if return values of system calls aren’t checked CS 3214
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Closing Files Closing a file informs the kernel that you are finished accessing that file. Closing an already closed file is a recipe for disaster in threaded programs (more on this later) Moral: Always check return codes, even for seemingly benign functions such as close() int fd; /* file descriptor */ int retval; /* return value */ if ((retval = close(fd)) < 0) { perror(“close”); exit(1); } CS 3214 Reading Files • Reading a file copies bytes from the current file position to memory, and then updates file position. • Returns number of bytes read from file
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

Page1 / 5

Supplementary-standard-IO - Unix Files A Unix file is a...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online