© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: 10.1 - 10.3 and 10.5-10.10 1st Ed: 11.1 - 11.3 and 11.5-11.10.

Slides:



Advertisements
Similar presentations
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
Advertisements

Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
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.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
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.
1 Unix File System API Operating System Hebrew University Spring 2007.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Computer Science 213 © 2006 Donald Acton 244 The Role of Unix I/O File system works at the block level Applications work at the byte level Unix I/O converts.
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”
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
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.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Lee CSCE 312 TAMU 1 System Level I/O Instructor: Dr. Hyunyoung Lee Based on slides provided by Randy Bryant and Dave O’Hallaron.
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”
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
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.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
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,
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
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.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Week 12 - Thursday CS222.
Today topics: File System Implementation
Protection of System Resources
Introduction to Operating Systems File I/O
CS111 Computer Programming
Operating System Hebrew University Spring 2004
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
“The course that gives CMU its Zip!”
File Structure Related system calls
“The course that gives CMU its Zip!”
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
IPC Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
CSCI 380: Operating Systems William Killian
Chien-Chung Shen CIS, UD
FILE I/O File Descriptors I/O Efficiency File Sharing
CS 105 “Tour of the Black Holes of Computing”
File I/O & UNIX System Interface
Chapter 12: File-System Implementation CSS503 Systems Programming
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

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

Unit 9 2 Learning Outcomes At the end of this unit you should be able to:  write a program that transfers data from one standard input to a file or from a file to standard output using Unix I/O calls  write a program that transfers data from one file to another using Unix I/O calls  write the same programs as above using C’s standard I/O functions  identify situations in which performance improves if we replace Unix I/O calls with C I/O functions and vice versa

Unit 9 3 The Role of Unix I/O I/O : the process that copies data between memory and external devices File system works at the block level Applications work at the byte level Unix I/O converts the byte level access to block level operations Why we study it?  helps understand how I/O functions provided by programming languages work  high level I/O functions provided by programming languages are not suitable for some applications  helps understand other system concepts Application Unix I/O File System Disk Drive File System Layering

Unit 9 4 Unix I/O API Some of the most common Unix I/O API functions used by applications are:  open()  close()  read()  write()  lseek()

Unit 9 5 Opening Files Opening a file informs the kernel that an application wants to access a file Allows the kernel to set aside resources Call: int open( char* filename, int flags ) Example: char* path; // file name... int source_fd; if ((source_fd = open(path, O_RDONLY)) < 0) { perror("Open source failed:"); exit(2); }

Unit 9 6 Opening cont’d Flags indicating access type:  O_RDONLY : read only  O_WRONLY : write only  O_RDWR: read/write  O_CREAT: create the file if doesn’t exist  O_APPEND: write at end  etc. Can also combine the options by (bitwise- inclusive) or'ing them  i.e. O_WRONLY | O_APPEND

Unit 9 7 Opening cont’d Open returns a small integer called a file descriptor A file descriptor is used by Unix to identify files that are opened by the current process Application passes this value back to the kernel in subsequent requests to work with a file Each process created starts with three open files and descriptors:  0: standard input (stdin)  1: standard output (stdout)  2: standard error (stderr) contains constants STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO for them

Unit 9 8 Closing Files Call: int close( int fd ) Closing a file tells the kernel it may free resources associated with managing the file close returns 0 if OK, -1 if error Example: int rc; if ((rc = close(source_fd)) < 0){ perror("close"); exit(10); }

Unit 9 9 Reading Files Call: int read( int fd, void *buf, int n) Each open file has a notion of a current position in the stream of bytes read() copies at most n bytes from the current file position to buf and updates the file position read() returns the number of bytes read  returns -1 if error  returns 0 if end-of-file (EOF) occurs read may return fewer bytes than requested (short reads)

Unit 9 10 Read Example char buf[512]; int chars_read; chars_read = read(source_fd, buf, sizeof(buf)); while (chars_read > 0) { // Do something chars_read = read(source_fd, buf, sizeof(buf)); } if (chars_read < 0) { perror("Reading error:"); exit(5); }

Unit 9 11 Writing Files Call: int write( int fd, void *buf, int n) write() copies at most n bytes from buf to the file position and updates position Returns the number of bytes written  returns -1 if error In some cases, it is possible to have fewer bytes written than requested (short writes) without having an error  when writing to a network socket (later)

Unit 9 12 Writing Example while (chars_read > 0) { if (write(stdout, buf, chars_read) < chars_read) { perror("Write problems:"); exit(4); } // Do another read and work }

Unit 9 13 Seek Call long lseek(int fd, long offset, int whence) Causes the logical position in the file to change  i.e. where the next read or write will commence from whence determines how position will change:  SEEK_SET : pointer is set to offset bytes.  SEEK_CUR: pointer is set to its current location plus offset.  SEEK_END: pointer is set to the size of the file plus offset.  SEEK_HOLE: returns the offset of next hole (contiguous range of bytes with value of zero) greater than or equal to the supplied offset  SEEK_DATA: pointer is set to start of the next non-hole region greater than or equal to the supplied offset

Unit 9 14 Seek example long new_offset; new_offset = lseek(fd, 2346, SEEK_CUR); new_offset = lseek(fd, 10, SEEK_SET); new_offset = lseek(fd, 25, SEEK_END);

Unit 9 15 Unix I/O Example Simple program that copies contents of file named by argument 1 to file named by argument 2 (i.e. the cp command) cs213copy fname1 [fname2]

Unit 9 16 Pseudo Code open argument 1 for input open argument 2 for output (if present) if arg 2 is present then connect stdout to this file read from input while read succeeds  write to stdout  read from input

Unit 9 17 Example: Unix Copy Command #include int main(int argc, char **argv) { if (argc <= 1) { printf("Usage: cs213cp source_file [destination_file]\n"); exit(1); } int source_fd; if ((source_fd = open(argv[1], O_RDONLY)) < 0) { perror("Open source failed:"); exit(2); }

Unit 9 18 Example (cont’) int dest_fd; if (argc > 2) { if ((dest_fd = open(argv[2], O_WRONLY | O_CREAT, 0600)) < 0) { perror("Destination open failed:"); int rc; if ((rc = close(source_fd)) < 0) { perror("close"); exit(10); } exit(3); } dup2(dest_fd, STDOUT_FILENO); }

Unit 9 19 Example (cont’) char buf[512]; int chars_read; chars_read = read(source_fd, buf, sizeof(buf)); while (chars_read > 0) { if (write(STDOUT_FILENO, buf, chars_read) < chars_read) { perror("Write problems:"); exit(4); } chars_read = read(source_fd, buf, sizeof(buf)); } if (chars_read < 0) { perror("Reading error:"); exit(5); }

Getting File Metadata In Linux, most of the file information that is in an inode, is stored in a structure called stat: struct stat { dev_t st_dev;/* Device. */ ino_t st_ino;/* File serial number.*/ mode_t st_mode;/* File mode. */ nlink_t st_nlink;/* Link count. */ uid_t st_uid;/* User ID of the file's owner.*/ gid_t st_gid;/* Group ID of the file's group.*/ dev_t st_rdev;/* Device number, if device. */ off_t st_size;/* Size of file, in bytes. */ blksize_t st_blksize;/* Optimal block size for I/O. */ blkcnt_t st_blocks;/* Number 512-byte blocks allocated. */ time_t st_atim;/* Time of last access. */ time_t st_mtim;/* Time of last modification. */ time_t st_ctim;/* Time of last status change. */ }; Unit 9 20

Getting File Metadata (cont) To retrieve info about a file, an application can use the following functions which are defines in : int stat(const char* filename, struct stat * buf); or int fstat(int fd, struct stat * buf); Both functions fill in buff with the file info from its stat structure In stat the file is given my its file name, while fstat accepts a file descriptor Unit 9 21

Unit 9 22 Unix I/O and Devices By making everything appear to be a file, the kernel can provide a single simple interface for performing I/O to a variety of devices Recall the basic operations are:  Opening and closing files –open() and close()  Changing the current file position –lseek()  Reading and writing files –read() and write()

Unit 9 23 Adding Other Devices Most devices tend to be producers or consumers of streams of data and fit UNIX I/O API model described Mouseproducer Joystickproducer Keyboardproducer Displayconsumer Audio deviceconsumer Tapeboth

Unit 9 24 New Devices Disk UNIX I/O Application File System Disk Drive KeyboardTerminalTapeAudio

Unit 9 25 The Kernel’s View of Open Files Calls to routines like open(), socket(), pipe(), etc. return file descriptors A file descriptor is just a small integer that is used by Unix to identify files that are opened by the current process When this “integer” is passed back to the kernel via calls like read() or write() the kernel manipulates the opened “file” the descriptor corresponds to

Unit 9 26 The Kernel’s View of Open Files (cont) Each process has its own separate descriptor table The file descriptor is just an index into this table Each entry in the descriptor table identifies an entry in a shared system wide open file table Each time open() succeeds, it creates an entry in the open file table

Unit 9 27 Open File Table Shared by all processes Each entry in the open file table contains:  a pointer to an entry in the v-node table that corresponds to that file  current position in the file, and  reference count of its usage close() decrements count v-node – virtual inode, i.e. a cache of an inode  contains the info in the file’s stat structure  may contain pointers to buffers/caches for the file/device  identifies legal operations on a file/device

Unit 9 28 The Kernel View of Open Files fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... stderr stdout stdin File access... File size File type File A Adapted from: Computer Systems: A Programmer’s Perspective The above is one struct in the open file table

Unit 9 29 Sharing Files At this point we have  File descriptors  The open file table  V-nodes It is relatively easy to explain what happens when file sharing results from:  open calls in the same process  open calls in different processes  fork()

Unit 9 30 Actions on open() fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File access... File size File type File A File B fd = open("B",…) Adapted from: Computer Systems: A Programmer’s Perspective

Unit 9 31 Same File Different Process Descriptor table (one table per process) Open file table (shared by all processes) v-node table File pos refcnt=1... File pos refcnt=1... fd 0 fd 1 fd 2 fd 3 fd 4 stderr stdout stdin File access... File size File type File A fd = open("A",…) fd 0 fd 1 fd 2 fd 3 fd 4 stderr stdout stdin Adapted from: Computer Systems: A Programmer’s Perspective File B File access... File size File type...

Unit 9 32 Same File Same Process Descriptor table (one table per process) Open file table (shared by all processes) v-node table File pos refcnt=1... File pos refcnt=1... fd 0 fd 1 fd 2 fd 3 fd 4 stderr stdout stdin File access... File size File type File A fd = open("A",…); Adapted from: Computer Systems: A Programmer’s Perspective

Unit 9 33 Close() Empty fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File access... File size File type File A File B close(4); refcnt=0

Unit 9 34 I/O Redirection deas:> ls > /tmp/out The above causes standard output (file descriptor 1) to be set to /tmp/out fd 0 fd 1 fd 2 fd 3 fd 4 Process file descriptor table stderr stdout stdin File pos refcnt=4 terminal File access... File size File type File access... File size File type File pos refcnt=1... /tmp/out refcnt=3... Adapted from: Computer Systems: A Programmer’s Perspective

Unit 9 35 dup2 The Unix system call dup2, which has the form dup2(fd, newfd) copies fd to newfd in the descriptor table. a b fd 0 fd 1 fd 2 fd 3 fd 4 b b fd 0 fd 1 fd 2 fd 3 fd 4 dup2(4,1) Adapted from: Computer Systems: A Programmer’s Perspective

Unit 9 36 dup2 example Process file descriptor table File pos terminal File access... File size File type File access... File size File type File pos... /tmp/out... int fd = open("/tmp/out",…); dup2(fd,1); close(fd); refcnt=1 fd 0 fd 1 fd 2 fd 3 fd 4 refcnt=0 refcnt=2

Unit 9 37 Pipe and fork fd 0 fd 1 fd 2 fd 3 fd 4 Parent v-node table stderr stdout stdin fd 0 fd 1 fd 2 fd 3 fd 4 Child stderr stdout stdin KeyboardTerminalpipe1pipe2 pipe() fork() dup2(3,1) close(4) dup(3, 0) close(3) close(4) close(3) Data ParentChild

Unit 9 38 How can we Improve Performance? Given what we know, are there interesting things we can do at the application layer to speed things up? A system call is several orders of magnitude more expensive than a function call Application Unix I/O File System Disk Drive File System Layering

Unit 9 39 Caching in the Application Applications can use caching to improve performance just like the kernel Most I/O has both  Spatial locality  Temporal locality Application level functions in the Standard I/O library of C take advantage of this and define I/O as a stream  use a buffer to keep the part of the file recently used  update the buffer when is needed All these functions are declared in the header stdio.h We'll call this standard I/O(stdio) or stream I/O to distinguish it from Unix I/O Unix I/O File System Disk Drive File System Layering Buffered I/O Application

Unit 9 40 STDIO Each Unix I/O call has a corresponding stdio call which is a standard C library function  open()  fopen()  close  fclose()  read()  fread()  write()  fwrite() Instead of returning a file descriptor fopen() returns a FILE * The FILE struct contains:  actual file descriptor  pointer to a buffer (called stream buffer)  position in buffer  other bookkeeping information

Unit 9 41 How it works - writes When fwrite() is called bytes are copied to the stream buffer If the stream buffer fills during the fwrite()  write() is called to “write” the stream buffer  stream buffer is cleared

Unit 9 42 fwrite() Buffer Buffer offset fd Kernel boundary write() Cached File Block

Unit 9 43 How it works - reads When fread() is called bytes are copied from the stream buffer to the application designated location If the stream buffer empties during the fread()  read() is called to refill the stream buffer  position in stream buffer is reset

Unit 9 44 fread() Buffer Buffer offset fd Kernel boundary read() Cached File Block

Unit 9 45 Analysis Costs over doing a system call  Need extra buffer space  One extra set of copies  Bookkeeping to ensure the stream buffer exactly matches real file location  I/O to random locations can be inefficient Advantage over system call  If application I/O requests much less data than underlying buffer holds then greatly reduces the number of system calls  System calls are very expensive Common practice:  use standard I/O everywhere except with sockets (networks)  use Unix I/O in networks

Summary Unix provide a small number of system calls for applications to deal with files and devices:  open(0,close(),read(),write(),lseek()  functions stat(),fstat() for file information Unix uses three data structures to maintain open files:  a descriptor table in each process  the open file table shared by all processes  the v-node table shared by all processes These structures allow easy file sharing and redirection The standarc I/O library is implemented on top of Unix I/O and provides buffered I/O which is good for most applications except networking Unit 9 46