CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.

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.
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.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
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.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Unix File System API Operating System Hebrew University Spring 2007.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
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”
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
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.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
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.
File IO and command line input CSE 2451 Rong Shi.
Files and Directories File types stat functions for file information
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
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.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing CS 105 “Tour of the Black Holes of Computing”
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”
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
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.
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.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
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.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Week 12 - Thursday CS222.
Error handling I/O Man pages
Supplementary Material on Unix System Calls and Standard I/O
Introduction to Operating Systems File I/O
File I/O.
System-Level I/O Topics Unix I/O Robust reading and writing
Intro to File I/O, System Calls CSE 333 Spring 2018
CSE 333 – Section 3 POSIX I/O Functions.
Operating System Hebrew University Spring 2004
CSE 333 – Section 3 POSIX I/O Functions.
“The course that gives CMU its Zip!”
CSE 333 – Section 3 POSIX I/O Functions.
File Structure Related system calls
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
System-Level I/O CSCI 380: Operating Systems
Chien-Chung Shen CIS, UD
CS 105 “Tour of the Black Holes of Computing”
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
System Calls, POSIX I/O CSE 333 Spring 2019
CSE 333 – Section 3 POSIX I/O Functions.
Presentation transcript:

CSE333 SECTION 3

Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm

String vs Byte APIs Strings are character(byte) arrays that terminate in '\0' str___() API operates on strings mem____() API operates on blocks of memory strcpy(dst,src) is equivalent to memcpy(dst,src,strlen(src)+1) strcmp() vs memcmp() strchr() vs memchr()

File I/O C’s stdio provides the notion of a stream A stream is a way of reading or writing a sequence of characters from/to a device A stream can be either text or binary; Linux does not distinguish A stream is buffered by default: libc reads ahead of you Three streams are provided by default: stdin, stdout, stderr You can open additional streams to read/write to files

#include #define READBUFSIZE 128 int main(int argc, char **argv) { FILE *f; char readbuf[READBUFSIZE]; size_t readlen; if (argc != 2) { fprintf(stderr, "usage:./fread_example filename\n"); return EXIT_FAILURE; // defined in stdlib.h } // Open, read, and print the file f = fopen(argv[1], "rb"); // "rb" --> read, binary mode if (f == NULL) { fprintf(stderr, "%s -- ", argv[1]); perror("fopen failed -- "); return EXIT_FAILURE; } // Read from the file, write to stdout. while ((readlen = fread(readbuf, 1, READBUFSIZE, f)) > 0) fwrite(readbuf, 1, readlen, stdout); fclose(f); return EXIT_SUCCESS; // defined in stdlib.h } #include #define READBUFSIZE 128 int main(int argc, char **argv) { FILE *f; char readbuf[READBUFSIZE]; size_t readlen; if (argc != 2) { fprintf(stderr, "usage:./fread_example filename\n"); return EXIT_FAILURE; // defined in stdlib.h } // Open, read, and print the file f = fopen(argv[1], "rb"); // "rb" --> read, binary mode if (f == NULL) { fprintf(stderr, "%s -- ", argv[1]); perror("fopen failed -- "); return EXIT_FAILURE; } // Read from the file, write to stdout. while ((readlen = fread(readbuf, 1, READBUFSIZE, f)) > 0) fwrite(readbuf, 1, readlen, stdout); fclose(f); return EXIT_SUCCESS; // defined in stdlib.h } stderr is a stream for printing error output to a console fopen opens a stream to read or write a file perror writes a string describing the last error to stderr stdout is for printing non-error output to the console printf(...) is equivalent to fprintf(stdout,...) fread_example.c

File Stats Returns the information about a specific file int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf);

File Stats 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; /* blocksize 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 */ };

File I/O C standard library to access files Specifically, fopen, fread, fwrite, fclose, fseek These provide a (FILE*) stream abstraction These are convenient and portable… …but they are buffered …and they are implemented by using lower-level OS calls

A Gotcha By default, stdio turns on buffering for streams Data “written” by fwrite() is copied into a buffer The buffer will be drained to the destination… …when you call fflush() on the stream …when the buffer size is exceeded (often 1024 or 4096 bytes) …when writing to a console and a newline is written (“line buffered”) …when you call fclose() on the stream …when your process exits gracefully (call to exit() or return from main() )

Why is This Important? What happens if... …your computer loses power before the buffer is flushed? …your program assumes data is written to a file, and it signals another program to read it? What are the performance implications? Data is copied into the stdio buffer Consumes CPU cycles and memory bandwidth Can potentially slow down high performance applications, like a web server or database (see: “zero copy”)

What to Do About It Accept it Buffering exists for a reason Turn off buffering with setbuf() This, too, may cause performance problems E.g., if your program does many small fwrite() s, each will now trigger a separate system call into the kernel Use a different API POSIX provides open(), read(), write(), close(), and others No buffering is done at the user level Maps to system calls But...what about the layers below? The OS caches disk reads and writes in the FS buffer cache Disk controllers have caches too!

Lower-Level File Access Most UNIX-en support a common set of lower- level file access APIs open, read, write, close, lseek Similar in spirit to their fopen (etc.) counterparts Lower-level and unbuffered Also less convenient You will have to use these for network I/O, so we might as well learn them now

open / close To open a file... Pass in the filename and access mode, similar to fopen Get back a “file descriptor” Similar to a (FILE *) from fopen, but just an int #include... int fd = open("foo.txt", O_RDONLY); if (fd == -1) { perror("open failed"); exit(EXIT_FAILURE); }... close(fd); #include... int fd = open("foo.txt", O_RDONLY); if (fd == -1) { perror("open failed"); exit(EXIT_FAILURE); }... close(fd);

Reading from a File ssize_t read(int fd, void *buf, size_t count); Returns the # of bytes read Might be fewer bytes than you requested (!!!) Returns 0 if you’re at end-of-file Returns -1 on error Warning: read() has some very surprising error modes!

read() Error Modes On error, the errno global variable is set You need to check it to see what kind of error happened What errors might read() encounter? EBADF -- bad file descriptor EFAULT -- output buffer is not a valid address EINTR -- read was interrupted, please try again …many others

How to Use read() #include... char *buf =...; int bytes_left = n; int result = 0; while (bytes_left > 0) { result = read(fd, buf + (n-bytes_left), bytes_left); if (result == -1) { if (errno != EINTR)) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; } bytes_left -= result; } #include... char *buf =...; int bytes_left = n; int result = 0; while (bytes_left > 0) { result = read(fd, buf + (n-bytes_left), bytes_left); if (result == -1) { if (errno != EINTR)) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; } bytes_left -= result; }

Other Low-Level Functions Read the man pages to learn about write() -- write data fsync() -- flush data to the underlying device opendir(), readdir_r(), closedir() -- get a directory listing