“The course that gives CMU its Zip!”

Slides:



Advertisements
Similar presentations
© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
Advertisements

1. I/O. I/O Unix I/O RIO (robust I/O) package Metadata, sharing, and redirection Standard I/O Conclusions and examples.
Carnegie Mellon 1 System-Level I/O : Introduction to Computer Systems 14 th Lecture, Oct. 12, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
System-Level I/O November 4, 2003 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
System-Level I/O October 8, 2008 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O class14.ppt.
System-Level I/O May 28, 2008 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
System-Level I/O November 11, 2004 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
System-Level I/O Nov 14, 2002 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O class24.ppt
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
I/O April 16, 2002 Topics Files Unix I/O Standard I/O Reading: (Beta) or (New) Problems: 12.9 (Beta) or 12.4 (New) class24.ppt
System-Level I/O May 22, 2006 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
Introduction to Operating Systems File I/O Most of the following slides are adapted from slides of Gregory Kesden and Markus Püschel of Carnegie Mellon.
System-level I/O Alan L. Cox Some slides adapted from CMU slides.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
System-Level I/O Topics Unix I/O Robust reading and writing
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 18, 2012 Instructors: Dave O’Hallaron, Greg Ganger,
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
UNIX Files File organization and a few primitives.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 16, 2014 Instructors: Greg Ganger, Greg Kesden,
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 25, 2013 Instructors: Gregory Kesden.
System-Level I/O March 25, 2004 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
Lee CSCE 312 TAMU 1 System Level I/O Instructor: Dr. Hyunyoung Lee Based on slides provided by Randy Bryant and Dave O’Hallaron.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing CS 105 “Tour of the Black Holes of Computing”
System-Level I/O.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing CS 105 “Tour of the Black Holes of Computing”
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
CS 105 “Tour of the Black Holes of Computing”
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
1 System-Level I/O. 2 Outline Unix I/O Reading File Metadata Sharing Files & I/O redirection Robust I/O Standard I/O Suggested Reading –10.1~10.3, 10.5~10.7,
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition System-Level I/O : Introduction to Computer.
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Files Oct. 28, 2008 Topics Mapping file offsets to disk blocks File system buffering and you The directory hierarchy lecture-18.ppt “The course.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 24, 2014 Instructors: Gregory Kesden.
Week 12 - Thursday CS222.
Robust I/O package Chapter 11 practice problems
System-Level I/O.
Supplementary Material on Unix System Calls and Standard I/O
System-Level I/O : Introduction to Computer Systems
Introduction to Operating Systems File I/O
CS 105 “Tour of the Black Holes of Computing”
CS703 - Advanced Operating Systems
System-Level I/O Topics Unix I/O Robust reading and writing
Virtual Memory: Systems
Operating System Hebrew University Spring 2004
System-Level I/O October 9, 2008
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Virtual Memory.
Reference: Advanced Programming in the UNIX Environment, Third Edition, W. Richard Stevens and Stephen A. Rago, Addison-Wesley Professional Computing Series,
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
IPC Prof. Ikjun Yeom TA – Hoyoun
System-Level I/O.
CSE 333 – Section 3 POSIX I/O Functions.
System-Level I/O CSCI 380: Operating Systems
Virtual Memory: Systems CSCI 380: Operating Systems
Chien-Chung Shen CIS, UD
CS 105 “Tour of the Black Holes of Computing”
Instructors: David Trammell
Presentation transcript:

15-213 “The course that gives CMU its Zip!” I/O Nov 15, 2001 Topics Files Unix I/O Standard I/O class24.ppt

A typical hardware system CPU chip register file ALU system bus memory bus main memory bus interface I/O bridge I/O bus Expansion slots for other devices such as network adapters. USB controller graphics adapter disk controller mouse keyboard monitor disk

Reading a disk sector (1) CPU chip CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller. register file ALU main memory bus interface I/O bus USB controller graphics adapter disk controller mouse keyboard monitor disk

Reading a disk sector (2) CPU chip Disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory. register file ALU main memory bus interface I/O bus USB controller graphics adapter disk controller mouse keyboard monitor disk

Reading a disk sector (3) CPU chip When the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special “interrupt” pin on the CPU) register file ALU main memory bus interface I/O bus USB controller graphics adapter disk controller mouse keyboard monitor disk

Unix files A Unix file is a sequence of n bytes: B0, B1, .... , Bk , .... , Bn-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) Kernel identifies each file by a small integer descriptor: 0: standard input 1: standard output 2: standard error

Unix file types Regular file Directory file Character special file Binary or text file. Unix does not know the difference! Directory file A file that contains names and locations of other files. Character special file For certain types of streaming devices (e.g., terminals) Block special file A file type typically used for disks. FIFO (named pipe) A file type used for interprocess comunication Socket A file type used for network communication between processes

Standard I/O functions fopen fread fwrite fprintf fscanf fgets fseek fclose C Application program application process Standard I/O functions (buffered) Unix I/O system calls (unbuffered) kernel open read write lseek stat close Hardware devices

Standard I/O vs Unix I/O When should I use the Standard I/O functions? Whenever possible! Many C programmers are able to do all of their work using the standard I/O functions. Why would I ever need to use the lower level Unix I/O functions? Fetching file metadata (e.g., size, modification date) must use lower level stat function Doing network programming there are some bad interactions between sockets and the standard library functions. Needing high performance.

Meta-data returned by stat() /* * file info returned by the stat function */ struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ mode_t st_mode; /* protection and file type */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device type (if inode device) */ off_t st_size; /* total size, in bytes */ unsigned long st_blksize; /* blocksize for filesystem I/O */ unsigned long st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last change */ };

Kernel’s file representation Before fork: i-node table entries file A file A parent’s per process descriptor table file pos ... refcnt = 1 st_mode ... st_size file B file B file pos ... refcnt = 1 st_mode ... st_size

Kernel’s file representation (cont) After fork (processes inherit open file tables): parent’s per process descriptor table i-node table entries file A file A file pos ... refcnt = 2 st_mode ... st_size file B file B child’s per process descriptor table file pos ... refcnt = 2 st_mode ... st_size

Unix file I/O: open() Must open() a file before you can do anything else. open() returns a small integer (file descriptor) fd < 0 indicates that an error occurred predefined file descriptors: 0: stdin 1: stdout 2: stderr int fd; /* file descriptor */ if ((fd = open(“/etc/hosts”, O_RDONLY)) < 0) { perror(“open”); exit(1); }

Unix file I/O: read() read() allows a program to fetch the contents of a file from the current file position. read() returns the number of bytes read from file fd. nbytes < 0 indicates that an error occurred. short counts (nbytes < sizeof(buf)) are possible and not errors! places nbytes bytes into memory address buf char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* open the file */ ... /* read up to 512 bytes from file fd */ if ((nbytes = read(fd, buf, sizeof(buf))) < 0) { perror(“read”); exit(1); }

Unix file I/O: write() write() allows a program to modify the file contents starting at the current file position. write() returns the number of bytes written from buf to file fd. nbytes < 0 indicates that an error occurred. short counts are possible and are not errors. char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* open the file */ ... /* write up to 512 bytes from buf to file fd */ if ((nbytes = write(fd, buf, sizeof(buf)) < 0) { perror(“write”); exit(1); }

Robustly dealing with read short counts (adapted from Stevens) ssize_t readn(int fd, void *buf, size_t count) { size_t nleft = count; ssize_t nread; char *ptr = buf; while (nleft > 0) { if ((nread = read(fd, ptr, nleft)) < 0) { if (errno == EINTR) nread = 0; /* and call read() again */ else return -1; /* errno set by read() */ } else if (nread == 0) break; /* EOF */ nleft -= nread; ptr += nread; return (count - nleft); /* return >= 0 */

Robustly dealing with write short counts (adapted from Stevens) ssize_t writen(int fd, const void *buf, size_t count) { size_t nleft = count; ssize_t nwritten; const char *ptr = buf; while (nleft > 0) { if ((nwritten = write(fd, ptr, nleft)) <= 0) { if (errno == EINTR) nwritten = 0; /* and call write() again */ else return -1; /* errorno set by write() */ } nleft -= nwritten; ptr += nwritten; return count;

Unix file I/O: lseek() lseek() changes the current file position int fd; /* file descriptor */ /* open the file */ ... /* Move the file position to byte offset 100 */ if ((lseek(fd, 100, SEEK_SET) < 0) { perror(“lseek”); exit(1); }

Unix file I/O: dup2() dup2() copies entries in the per-process file descriptor table. lseek(oldfd, newfd) overwrites the entry in the per-process file table for newfd with the entry for oldfd. /* Redirect stderr to stdout (so that driver will get all output on the pipe connected to stdout) */ dup2(1, 2); /* copies fd 1 (stdout) to fd 2 (stderr) */

dup2() example Before calling dup2(4,1): open file table entries i-node table entries file A file A file pos ... refcnt = 1 st_mode ... st_size 1 file B 2 file B 3 4 file pos ... 5 refcnt = 1 st_mode 6 ... st_size 7

dup2() example After calling dup2(4,1): open file table entries i-node file A file A file pos ... refcnt = 0 st_mode ... st_size 1 file B 2 file B 3 4 file pos ... 5 refcnt = 2 st_mode 6 ... st_size 7

Buffering in Standard I/O Standard library functions use buffered I/O printf(“h”); printf(“e”); printf(“l”); printf(“l”); printf(“o”); buf printf(“\n”); h e l l o \n . . fflush(stdout); write(1, buf += 6, 6);

Buffering in action You can see this buffering for yourself, using the always fascinating Unix strace program: #include <stdio.h> int main() { printf("h"); printf("e"); printf("l"); printf("o"); printf("\n"); fflush(stdout); exit(0); } bass> strace hello execve("./hello", ["hello"], [/* ... */]). ... write(1, "hello\n", 6) = 6 munmap(0x40000000, 4096) = 0 _exit(0) = ?

Using buffering to robustly read text lines (Stevens) static ssize_t my_read(int fd, char *ptr) { static int read_cnt = 0; static char *read_ptr, read_buf[MAXLINE]; if (read_cnt <= 0) { again: if ( (read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) { if (errno == EINTR) goto again; return -1; } else if (read_cnt == 0) return 0; read_ptr = read_buf; read_cnt--; *ptr = *read_ptr++; return 1;

Robustly reading text lines (cont) ssize_t readline(int fd, void *buf, size_t maxlen) { int n, rc; char c, *ptr = buf; for (n = 1; n < maxlen; n++) { /* notice that loop starts at 1 */ if ( (rc = my_read(fd, &c)) == 1) { *ptr++ = c; if (c == '\n') break; /* newline is stored, like fgets() */ } else if (rc == 0) { if (n == 1) return 0; /* EOF, no data read */ else break; /* EOF, some data was read */ return -1; /* error, errno set by read() */ *ptr = 0; /* null terminate like fgets() */ return n;

mmap() revisited void *mmap(void *start, int len, int prot, int flags, int fd, int offset) Map len bytes starting at offset offset of the file specified by file description fd, preferably at address start (usually 0 for don’t care). prot: MAP_READ, MAP_WRITE flags: MAP_PRIVATE, MAP_SHARED Return a pointer to the mapped area

Visualizing mmap() p = mmap(NULL, 4, PROT_READ, MAP_PRIVATE, fd, 2); Virtual memory Disk file (fd) ... hi ... p lo ...

mmap() example /* mmapcopy - uses mmap to copy file fd to stdout */ void mmapcopy(int fd, int size) { char *bufp; /* ptr to memory mapped VM area */ bufp = Mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); Write(1, bufp, size); return; } /* mmapcopy driver */ int main(int argc, char **argv) struct stat stat; int fd; fd = Open(argv[1], O_RDONLY, 0); Fstat(fd, &stat); mmapcopy(fd, stat.st_size); exit(0);

For further information The Unix bible: W. Richard Stevens, Advanced Programming in the Unix Environment, Addison Wesley, 1993. Stevens is arguably the best technical writer ever. Produced authoritative works in: Unix programming TCP/IP (the protocol that makes the Internet work) Unix network programming Unix IPC programming. Tragically, Stevens died Sept 1, 1999.