Lecture 23: System-Level I/O

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

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

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

File Input and Output (I/O)

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

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

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

All the scoring jobs will be done by script

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

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

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

All the scoring jobs will be done by script

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

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

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

System-Level I/O March 31, 2005

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

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

Foundations of Computer Systems

Operating System Labs. Yuanbin Wu

UNIX System Calls. Sys Calls versus Library Func

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

structs as arguments

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

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

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

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

Operating System Labs. Yuanbin Wu

Unix programming interface for file I/O operations and pipes

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

System-Level I/O April 6, 2006

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

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

File System (FS) Highlights

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

CS , Spring Sample Exam 3

CSC 271 Software I: Utilities and Internals

Design Choices 2 / 29

CSE 333 SECTION 3. POSIX I/O Functions

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

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

Process Management 1

39. File and Directories

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

Process Management! Goals of this Lecture!

Advanced Systems Security: Ordinary Operating Systems

CSci 4061 Introduction to Operating Systems. File Systems: Basics

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

Inter-Process Communication

Files and Directories Filesystems from a user s perspective

Computer Science & Engineering Department I. I. T. Kharagpur

File Descriptors and Piping

Process Management! Goals of this Lecture!

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

CS631 - Advanced Programming in the UNIX Environment

Naked C Lecture 6. File Operations and System Calls

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CSE 333 SECTION 3. POSIX I/O Functions

Linux Forensics. Newbug Tseng Oct

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

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

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

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

INTRODUCTION TO UNIX FILE SYSTEM:

Automated Test Generation in System-Level

628 Lecture Notes Week 4

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

15-213/18-243, Spring 2011 Exam 2

CSE 410: Systems Programming

Processes COMPSCI 386

Master Calcul Scientifique - Mise à niveau en Informatique Written exam : 3 hours

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

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

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

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

Files and Directories

OPERATING SYSTEMS: Lesson 2: Operating System Services

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

UNIX System Programming

Thesis, antithesis, synthesis

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

CS240: Programming in C

Process Creation in UNIX

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

UNIX Kernel. UNIX History

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

Recitation 8: Tshlab + VM

IPC and Unix Special Files

Basic OS Progamming Abstrac7ons

Basic OS Progamming Abstrac2ons

Transcription:

CSCI-UA.0201-001/2 Computer Systems Organization Lecture 23: System-Level I/O Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O Hallaron

CPU Memory

I/O Devices Very diverse devices behavior (i.e., input vs. output vs. storage) partner (who is at the other end?) data rate I/O Design affected by many factors (expandability, resilience) Performance: access latency throughput connection between devices and the system the memory hierarchy the operating system A variety of different users

Application programs If this is enough Language Run-time Systems high-level facility for I/O (e.g. ANSI C standard I/O) Kernel Level I/O system calls Why bother learning this?

Why Bother? Understanding kernel-level I/O will help you understand other systems concepts I/O plays a key role in process creation and execution Process creation plays a key role in how files are shared by different processes Sometimes language run-time is not enough to do what you want

Unix I/O A file is a sequence of m bytes. All I/O devices are modeled as files. All I/O is performed by reading and writing the appropriate files. Opening a file: an application wants to use an I/O device. Kernel gives the application a file descriptor (nonnegative integer) Changing the current file position: position is a byte offset from the beginning of the file (kept by kernel) Reading and writing files Closing files

Unix I/O UNIX abstracts many things into files E.g. regular files, devices (/dev/sda2), FIFO pipes, sockets Allow a common set of syscalls for handling I/O E.g. reading and writing to files/pipes/sockets: read and write

Overview of File System implementation in UNIX file data dir block root / inode 0 dir block inode 1 f1.txt 2 inode 2 home user Inode 3 f2.txt 2 Inodes contain meta-data about files/directories Last modification time, size, user id Hard links: multiple names for the same file (/home/f1.txt and /usr/f2.txt refer to the same file)

UNIX I/O (i.e. I/O related syscalls) Getting meta-data (info maintained in i- nodes) stat Directory operations opendir, readdir, rmdir Open/close files Open,close Read/write files read/write

File Metadata Access file meta-data using stat syscall Example: rkmatch.c void read_file(const char *fname, char **doc, int *doc_len) { struct stat st; if (stat(fd, &st)!= 0) { perror("read_file: fstat "); exit(1); } } *doc = (char *)malloc(st.st_size); You need: #include <sys/stat.h>

File Metadata Access file meta-data using stat syscall struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ 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 ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* block size for file system I/O */ blkcnt_t st_blocks; /* number of 512B 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 status change */ };

Opening Files Open a file before access: Returns a small integer file descriptor (or -1 for error) int fd; /* file descriptor */ if ((fd = open( X", O_RDONLY)) < 0) { perror("open"); exit(1); } For more info, do man 2 open Why fd? Kernel maintains an array of info on currently opened files for a process fd indexes into this in-kernel array Each process starts out with three open files 0: standard input 1: standard output 2: standard error

Closing Files Closing a file informs kernel that you are finished accessing that file int fd; /* file descriptor */ if (close(fd) < 0) { perror("close"); exit(1); }

Simple read/write example Copying standard in to standard out, one byte at a time #include <stdio.h> int main(void) { char c; Returns # of bytes read, -1 for error } while(read(stdin_fileno, &c, 1) == 1){ write(stdout_fileno, &c, 1); } exit(0); cpstdin.c Returns # of bytes written, -1 for error

Kernel Presentation of Open Files Kernel uses 3 related data structures to represent open files Descriptor table: per process Indexed by the process open file descriptor Each entry points to an entry in the file table File table: Shared by all processes Each entry contains info about file position, reference count,, and a pointer to an entry in the v-node table v-node table: Shared by all processes contains info that can be read by stat syscall

Kernel tracks user processes opened files kernel state Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A (terminal) File pos refcnt=1 File access File size File type Info in stat struct File B (disk) File access File pos refcnt=1 File size File type

Kernel tracks user processes opened files Calling open twice with the same filename Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A (disk) File pos refcnt=1 File access File size File type File B (disk) File pos refcnt=1

Child process inherits its parent s open Before fork() call: files Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] File A (terminal) stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File pos refcnt=1 File access File size File type File B (disk) File access File pos refcnt=1 File size File type

Child process inherits its parent s open files After fork(): Child s descriptor table same as parent s, and +1 to each refcnt Descriptor table [one table per process] fd 0 fd 1 fd 2 fd 3 fd 4 Parent Open file table [shared by all processes] File A (terminal) File pos refcnt=2 v-node table [shared by all processes] File access File size File type fd 0 fd 1 fd 2 fd 3 fd 4 Child File B (disk) File pos refcnt=2 File access File size File type

Fun with File Descriptors (fork) #include <stdio.h> #include <fcntl.h> int main(int argc, char *argv[]) { int fd1; char c1, c2; char *fname = argv[1]; fd1 = open(fname, O_RDONLY, 0); read(fd1, &c1, 1); if (fork()) { /* Parent */ read(fd1, &c2, 1); printf("parent: c1 = %c, c2 = %c\n", c1, c2); } else { /* Child */ sleep(5); read(fd1, &c2, 1); printf("child: c1 = %c, c2 = %c\n", c1, c2); } return 0; } Solution: Parent: c1 = a, c2 = b Child: c1 = a, c2 = c What would this program print for file containing abcde?

Fun with File Descriptors (dup2) #include <stdio.h> #include <fcntl.h> int main(int argc, char *argv[]) { int fd1, fd2, fd3; char c1, c2, c3; char *fname = argv[1]; fd1 = open(fname, O_RDONLY, 0); fd2 = open(fname, O_RDONLY, 0); fd3 = open(fname, O_RDONLY, 0); dup2(fd2, fd3); read(fd1, &c1, 1); read(fd2, &c2, 1); read(fd3, &c3, 1); printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); return 0; } ffiles1.c Solution: c1 = a, c2= a, c3 = b What would this program print for file containing abcde?

I/O Redirection How does a shell redirect I/O? unix$ ls > foo.txt Use syscall dup2(oldfd, newfd) Copies descriptor table entry oldfd to entry newfd Descriptor table before dup2(4,1) Descriptor table after dup2(4,1) fd 0 fd 1 fd 2 fd 3 fd 4 a b fd 0 fd 1 fd 2 fd 3 fd 4 b b

I/O Redirection Example Step #1: open output file to which stdout should be redirected Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A File pos refcnt=1 File access File size File type Opened file has fd=4 File B File pos refcnt=1 File access File size File type

I/O Redirection Example (cont.) Step #2: call dup2(4,1) cause fd=1 (stdout) to refer to disk file pointed at by fd=4 Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] stdin stdout stderr fd 0 fd 1 fd 2 fd 3 fd 4 File A File pos refcnt=0 File access File size File type File B File access File pos refcnt=2 File size File type

Standard I/O Functions The C library (libc.so) contains a collection of higher-level standard I/O functions fopen fdopen fread fwrite fscanf fprintf sscanf sprintf fgets fputs fflush fseek fclose Internally invokes I/O syscalls open read write lseek stat close

Standard I/O Streams Standard I/O implements buffered streams Abstraction for a file descriptor and a buffer in memory. C programs begin life with three open streams stdin (standard input) stdout (standard output) stderr (standard error) #include <stdio.h> extern FILE *stdin; /* standard input (descriptor 0) */ extern FILE *stdout; /* standard output (descriptor 1) */ extern FILE *stderr; /* standard error (descriptor 2) */ int main() { fprintf(stdout, "Hello, world\n"); }

Unix I/O vs. standard I/O Unix I/O: Pros most general, lowest overhead. All other I/O packages are implemented using Unix I/O functions. Provides functions for accessing file metadata. async-signal-safe and can be used safely in signal handlers. Cons Efficient reading/writing may require some form of buffering

Unix I/O vs. Standard I/O: Standard I/O: Pros: Buffering increases efficiency by reducing # of read and write system calls Cons: Provides no function for accessing file metadata Not async-signal-safe, and not appropriate for signal handlers. Not appropriate for input and output on network sockets

Choosing I/O Functions General rule: use the highest-level I/O functions you can Many C programmers are able to do all of their work using the standard I/O functions When to use standard I/O When working with disk or terminal files When to use raw Unix I/O Inside signal handlers, because Unix I/O is async-signal-safe. When working with network sockets In rare cases when you want to tune for absolute highest performance.

Standard I/O Buffering in Action You can see this buffering in action for yourself use strace to monitor a program s syscall invocation: #include <stdio.h> void main() { char c; while ((c = getc(stdin))!='\n') { printf("%c",c); } printf("\n"); } linux% strace./a.out execve("./a.out", [./a.out"], [/* */]). read(0,"hello\n", 1024) = 6 write(1, "hello\n", 6) = 6 exit_group(0) =?

Conclusions UNIX/LINUX use files to abstract many I/O devices Accessing files can be done either by standard I/O or UNIX I/O