Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Similar documents
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.

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

All the scoring jobs will be done by script

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

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

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

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 Nov 14, 2002

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

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

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

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

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

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

UNIX System Calls. Sys Calls versus Library Func

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

Lecture 23: System-Level I/O

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

CSC 271 Software I: Utilities and Internals

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

Operating System Labs. Yuanbin Wu

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

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

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

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

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

File Input and Output (I/O)

Operating System Labs. Yuanbin Wu

Files and Directories Filesystems from a user s perspective

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

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

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Operating System Labs. Yuanbin Wu

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

File I/0. Advanced Programming in the UNIX Environment

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

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

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

Preview. lseek System Calls. A File Copy 9/18/2017. An opened file offset can be explicitly positioned by calling lseek system call.

OPERATING SYSTEMS: Lesson 2: Operating System Services

structs as arguments

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

CSE 333 SECTION 3. POSIX I/O Functions

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Lecture 21 Systems Programming in C

39. File and Directories

File System (FS) Highlights

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

Unix programming interface for file I/O operations and pipes

CS , Spring Sample Exam 3

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

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

CSci 4061 Introduction to Operating Systems. File Systems: Basics

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

Design Choices 2 / 29

Files and Directories Filesystems from a user s perspective

Foundations of Computer Systems

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Advanced Systems Security: Ordinary Operating Systems

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Automated Test Generation in System-Level

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

UNIX System Programming

Files and Directories

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

CSE 333 SECTION 3. POSIX I/O Functions

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

CS631 - Advanced Programming in the UNIX Environment

Files. Eric McCreath

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating System Labs. Yuanbin Wu

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

File Descriptors and Piping

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

File and Directories. Advanced Programming in the UNIX Environment

Recitation 8: Tshlab + VM

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

Linux Forensics. Newbug Tseng Oct

Operating systems. Lecture 7

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

File System. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

CMPSC 311- Introduction to Systems Programming Module: Input/Output

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

System-Level I/O April 6, 2006

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

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

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

Process Creation in UNIX

Input and Output System Calls

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Basic OS Progamming Abstrac7ons

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

Basic OS Progamming Abstrac2ons

Thesis, antithesis, synthesis

Transcription:

File I/O Hyo-bong Son (proshb@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

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 (hard disk partition) /dev/tty2 (terminal) Even the kernel is represented as a file: /dev/kmem /proc (kernel memory image) (kernel data structures) 2

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 communication Socket A file type used for network communication between processes 3

Unix I/O Characteristics The elegant mapping of files to devices allows kernel to export simple interface called Unix I/O. All input and output is handled in a consistent and uniform way ( byte stream ) Basic Unix I/O operations (system calls): Opening and closing files open() and close() Changing the current file position (seek) lseek() Reading and writing a file read() and write() 4

Opening Files Opening a file informs the kernel that you are getting ready to access that file. int fd; /* file descriptor */ if ((fd = open( /etc/hosts, O_RDONLY)) < 0) { perror( open ); exit(1); } 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 5

Opening Files Return int open(char *filename, int flags, mode_t mode); small identifying integer file descriptor if OK, -1 on error Parameters <char *filename> indicates which file to open <int flags> indicates how the process intends to access the file O_RDONLY, O_WRONLY, O_RDWR O_CREAT, O_TRUNC, O_APPEND <mode_t mode> specifies the access permission bits of new files optional field S_IRUSR, S_IWUSR, S_IXUSR S_IRGRP, S_IWGRP, S_IXGRP S_IROTH, S_IWOTH, S_IXOTH 6

Closing Files Closing a file informs the kernel that you are finished accessing that file. int fd; /* file descriptor */ int retval; /* return value */ if ((retval = close(fd)) < 0) { perror( close ); exit(1); } Closing an already closed file is a recipe for disaster in threaded programs Moral: Always check return codes, even for seemingly benign functions such as close() 7

Reading Files Reading a file copies bytes from the current file position to memory, and then updates file position. char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open file fd... */ /* Then read up to 512 bytes from file fd */ if ((nbytes = read(fd, buf, sizeof(buf))) < 0) { perror( read ); exit(1); } Returns number of bytes read from file fd into buf nbytes < 0 indicates that an error occurred. short counts (nbytes < sizeof(buf)) are possible and are not errors! 8

Reading Files ssize_t read(int fd, void *buf, size_t count); Return Number of bytes read if OK, 0 on EOF, -1 on error Short counts may occur! Parameters <int fd> file descriptor fd to read <void *buf> reads into the buffer starting at buf <size_t count> reads up to count bytes 9

Writing Files Writing a file copies bytes from memory to the current file position, and then updates current file position. char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open the file fd... */ /* Then write up to 512 bytes from buf to file fd */ if ((nbytes = write(fd, buf, sizeof(buf)) < 0) { perror( write ); exit(1); } Returns number of bytes written from buf to file fd. nbytes < 0 indicates that an error occurred. As with reads, short counts are possible and are not errors! 10

Writing Files ssize_t write(int fd, void *buf, size_t count); Return Number of bytes read if OK, 0 on EOF, -1 on error Short counts may occur! Parameters <int fd> file descriptor fd to write <void *buf> source to write start from buf <size_t count> writes up to count bytes 11

File Offset An open file s offset can be set explicitly by calling lseek Off_t lseek(int fd, off_t offset, int whence) Returns New file offset if OK, -1 on error whence SEEK_SET: the file s offset is set to set to offset SEEK_CUR: current value plus offset SEEK_END: set to size of file plus offset 12

Hole File 13

Hole File Contents of the hole file Difference between two files Both files are the same size, but consumption of disk blocks is different. 14

Dealing with Short Counts When short counts can occur? Encountering (end-of-file) EOF on reads. Reading from disk files (except for EOF) Reading text lines from a terminal. Reading and writing network sockets or Unix pipes. Writing to disk files. 15

Dealing with Short Counts Short counts can occur in these situations: Encountering (end-of-file) EOF on reads. Reading text lines from a terminal. Reading and writing network sockets or Unix pipes. Short counts does not occur in these situations: Reading from disk files (except for EOF) Writing to disk files. How should you deal with short counts in your code? One way is to use the RIO (Robust I/O) package from your textbook s csapp.c file (Appendix B). 16

File Metadata Data about data, in this case file data. Maintained by kernel, accessed by users with the stat and fstat functions. /* Metadata returned by the stat and fstat functions */ 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 file access */ time_t st_mtime; /* time of last file modification */ time_t st_ctime; /* time of last inode change */ }; /* statbuf.h included by sys/stat.h */ 17

Accessing File Metadata /* statcheck.c - Querying and manipulating a file s meta data */ int main (int argc, char **argv) { struct stat st; char *type, *readok; stat(argv[1], &st); if (S_ISREG(st.st_mode)) /* file type*/ type = "regular"; else if (S_ISDIR(st.st_mode)) type = "directory"; else type = "other"; if ((st.st_mode & S_IRUSR)) /* OK to read?*/ readok = "yes"; else readok = "no"; bass>./statcheck statcheck.c type: regular, read: yes bass> chmod 000 statcheck.c bass>./statcheck statcheck.c type: regular, read: no } printf("type: %s, read: %s\n", type, readok); exit(0); int stat(const char *filename, struct stat *buf) int fstat(int fd, struct stat *buf) 18

Sharing Files The kernel represents open files using three related data structures: Descriptor table, file table, v-node table 19

Sharing Files Two descriptors sharing the same disk file through two open file table entries. 20

I/O Redirection Hoinky $ ls > foo.txt dup & dup2 duplicates existing file descriptors int dup(int filedes); int dup2(int filedes, int filedes2); dup is guaranteed to return the lowest-numbered available file descriptor. dup2 specifies the value of the new descriptor with the filedes2 21

Sharing Files Dup2(4, 1); 22

Sharing Files Dup2(4, 1); 0 23

Pros/Cons of Unix I/O Pros The most general and lowest overhead form of I/O. All other I/O packages are implemented on top of Unix I/O functions. Unix I/O provides functions for accessing file metadata. Cons System call overheads for small-sized I/O. Dealing with short counts is tricky and error prone. Efficient reading of text lines requires some form of buffering, also tricky and error prone. These issues are addressed by the standard I/O. 24

Summary Unix file I/O open(), read(), write(), close(),... A uniform way to access files, I/O devices, network sockets, kernel data structures, etc. When to use raw Unix I/O When you need to fetch file metadata. When you read or write network sockets or pipes. In rare cases when you need absolute highest performance. 25

Exercises Copying standard input to standard output one byte at a time. Find 10 words appeared most in the given novel Novel: http://www.gutenberg.org/files/829/829-0.txt Words are separated by \n and \t, otherwise ignore special characters and attach two words. Hello2me hellome http://www.skku.edu httpwwwskkuedu Output example: [RANK 0][the][4096] 26