Supplementary-standard-IO

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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern