The course that gives CMU its Zip! I/O Nov 15, 2001

Similar documents
System-Level I/O Nov 14, 2002

A Typical Hardware System The course that gives CMU its Zip! System-Level I/O Nov 14, 2002

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

All the scoring jobs will be done by script

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

CS 201. Files and I/O. Gerson Robboy Portland State University

System-Level I/O. William J. Taffe Plymouth State University. Using the Slides of Randall E. Bryant Carnegie Mellon University

System-Level I/O March 31, 2005

Lecture 23: System-Level I/O

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Contents. NOTICE & Programming Assignment 0 review. What will be next project? File IO & File IO exercise

All the scoring jobs will be done by script

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

System-Level I/O April 6, 2006

Introduction to Computer Systems , fall th Lecture, Oct. 19 th

File Input and Output (I/O)

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

Unix programming interface for file I/O operations and pipes

System- Level I/O. CS 485 Systems Programming Fall Instructor: James Griffioen

System-Level I/O : Introduction to Computer Systems 16 th Lecture, March 23, Instructors: Franz Franchetti & Seth Copen Goldstein

UNIX System Calls. Sys Calls versus Library Func

structs as arguments

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

Foundations of Computer Systems

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

System- Level I/O /18-243: Introduc3on to Computer Systems 16 th Lecture, Mar. 15, Instructors: Gregory Kesden and Anthony Rowe

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Operating System Labs. Yuanbin Wu

CSE 333 SECTION 3. POSIX I/O Functions

OPERATING SYSTEMS: Lesson 2: Operating System Services

Files and Directories Filesystems from a user s perspective

File Descriptors and Piping

Operating System Labs. Yuanbin Wu

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

File System (FS) Highlights

CS , Spring Sample Exam 3

Operating System Labs. Yuanbin Wu

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

CSC 271 Software I: Utilities and Internals

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

CSE 333 SECTION 3. POSIX I/O Functions

Design Choices 2 / 29

17: Filesystem Examples: CD-ROM, MS-DOS, Unix

Naked C Lecture 6. File Operations and System Calls

Logistics. Recitation 11: I/O Problems. Today s Plan. Why Use Robust I/O. Andrew Faulring Section A 18 November 2002

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

File Systems. q Files and directories q Sharing and protection q File & directory implementation

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

39. File and Directories

Virtual Memory: Systems

ECE 650 Systems Programming & Engineering. Spring 2018

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

CS240: Programming in C

Linux Forensics. Newbug Tseng Oct

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

UNIX System Programming

Inter-process communication (IPC)

15213 Recitation Section C

Page Which had internal designation P5

CSE 410: Systems Programming

Automated Test Generation in System-Level

CSE 410: Systems Programming

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

File Types in Unix. Regular files which include text files (formatted) and binary (unformatted)

Process Management! Goals of this Lecture!

UNIX input and output

CSci 4061 Introduction to Operating Systems. File Systems: Basics

628 Lecture Notes Week 4

Advanced Systems Security: Ordinary Operating Systems

Section 3: File I/O, JSON, Generics. Meghan Cowan

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

CS240: Programming in C

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

which maintain a name to inode mapping which is convenient for people to use. All le objects are

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Process Creation in UNIX

Basic OS Progamming Abstrac7ons

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

Basic OS Progamming Abstrac2ons

Processes COMPSCI 386

Virtual Memory: Systems

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Operating System Labs. Yuanbin Wu

Input and Output System Calls

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

Transcription:

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

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

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

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

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

Unix files A Unix file is a sequence of n bytes: B 0, B 1,., B k,., B n-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 6

Regular file Unix file types 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 7

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

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. 9

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 */ ; 10

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

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

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

Unix file I/O: read() read() allows a program to fetch the contents of a file from the current file position. 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); 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 14

Unix file I/O: write() write() allows a program to modify the file contents starting at the current file position. 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); 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. 15

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 */ 16

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; 17

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); lseek() changes the current file position. 18

Unix file I/O: dup2() dup2() copies entries in the per-process file descriptor table. /* 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) */ lseek(oldfd, newfd) overwrites the entry in the perprocess file table for newfd with the entry for oldfd. 19

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

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

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

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("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) =? 23

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; 24

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 */ else return -1; /* error, errno set by read() */ *ptr = 0; /* null terminate like fgets() */ return n; 25

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 26

Visualizing mmap() p = mmap(null, 4, PROT_READ, MAP_PRIVATE, fd, 2); Disk file (fd) Virtual memory hi p 0 lo 27

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); 28

The Unix bible: For further information 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. 29