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

Similar documents
CSE 333 SECTION 3. POSIX I/O Functions

structs as arguments

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

Lecture 23: System-Level I/O

CSE 333 SECTION 3. POSIX I/O Functions

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

Operating System Labs. Yuanbin Wu

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

All the scoring jobs will be done by script

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

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

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?

UNIX System Calls. Sys Calls versus Library Func

CS , Spring Sample Exam 3

All the scoring jobs will be done by script

Operating System Labs. Yuanbin Wu

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

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

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

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

File System (FS) Highlights

System-Level I/O Nov 14, 2002

Design Choices 2 / 29

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

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

CSC 271 Software I: Utilities and Internals

CSE 333 Lecture 8 - file and network I/O

Files and Directories Filesystems from a user s perspective

CSE 333 Lecture 8 - system calls, intro to file I/O

CS240: Programming in C

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

File Input and Output (I/O)

CSE 333 Lecture 6 - system calls, intro to file I/O

System-Level I/O March 31, 2005

C provides some basic facilities C libraries help make those primitive facilities useful

Automated Test Generation in System-Level

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

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

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

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

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

Naked C Lecture 6. File Operations and System Calls

CSE 410: Systems Programming

Linux Forensics. Newbug Tseng Oct

OPERATING SYSTEMS: Lesson 2: Operating System Services

Files. Eric McCreath

Advanced Systems Security: Ordinary Operating Systems

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

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

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

Operating Systems. Processes

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

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

Thesis, antithesis, synthesis

Operating System Labs. Yuanbin Wu

CS631 - Advanced Programming in the UNIX Environment

Foundations of Computer Systems

Memento: Time Travel for the Web

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

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

UNIX System Programming

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

Low-Level I/O, C++ Preview

Unix programming interface for file I/O operations and pipes

HEC POSIX I/O API Extensions Rob Ross Mathematics and Computer Science Division Argonne National Laboratory

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

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

Operating systems. Lecture 7

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

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

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

CSci 4061 Introduction to Operating Systems. File Systems: Basics

39. File and Directories

File and Directories. Advanced Programming in the UNIX Environment

Files and Directories

Do not turn the page until 5:00.

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

File IO and command line input CSE 2451

void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size);

Goals of this Lecture

I/O Management! Goals of this Lecture!

2009 S2 COMP File Operations

I/O Management! Goals of this Lecture!

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

Lecture 21 Systems Programming in C

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

Operating System Labs. Yuanbin Wu

CS240: Programming in C. Lecture 14: Errors

Systems Programming. 09. Filesystem in USErspace (FUSE) Alexander Holupirek

Systems Programming/ C and UNIX

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

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

void clearerr(file *stream); int feof(file *stream); int ferror(file *stream); int fileno(file *stream); #include <dirent.h>

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Experiment 6 The Real World Interface

Transcription:

CSE333 SECTION 5

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

String API vs. Byte API Recall: Strings are character arrays terminated by \0 The String API (functions that start with str< >) rely on the null terminating character The Byte API (functions that start with mem< >) ask for a number of bytes to process Examples strcpy(src, dst) strcmp(str1, str2) strchr(str, char) memcpy(dst, src, bytes) memcmp(str1, str2, bytes) memchr(data, char, bytes)

File I/O in C - Streams Reading and Writing using the notion of a stream Input can either be text or binary data Streams are either buffered (default) or unbuffered Standard Streams: stdin(fd 0), stdout(fd 1), stderr(fd 2)

Lib C File I/O Utilizes FILE * for I/O. #include <stdio.h> File *f; FILE *fopen( char *filename, char *mode) Modes: r - read only r+ - Read and Write And more! man fopen

Lib C File I/O int fclose(file) Returns 0 on success, otherwise EOF and set errno size_t fread(data, size of chunks, number of chunks, FILE) Returns the number of chunks read size_t fwrite(data, size of chunks, number of chunks, FILE) Returns the number of chunks written

#include <stdio.h> #include <stdlib.h> #include <errno.h> fread_example.c #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 printf(...) is equivalent to fprintf(stdout,...) 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

Buffered I/O Potential Problems? Data written using fwrite( ) is copied into a buffered allocated by stdio and written into memory when, When fflush( ) is called Buffer size is exceeded For stdout, when a new line is reached ( line buffered ) When fclose( ) is called When your process exits gracefully Are there any potential problems?

Why is this a gotcha? 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 ( zero copy )

What to do about it Turn off buffering with setbuf( ) this, too, may cause performance problems e.g., if your program does many small fwrite( ) s, each of which will now trigger a system call into the Linux kernel Use a different set of system calls POSIX provides open( ), read( ), write( ), close( ), and others no buffering is done at the user level but...what about the layers below? the OS caches disk reads and writes in the FS buffer cache disk controllers have caches too!

stat Returns the information about a specific file int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); 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 */ };

POSIX I/O What s the difference? Unbuffered at the user level Less convenient When would I use it? Networking How do I use it? #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/uio.h> man 2 <open, close, read, write> POSIX I/O uses file descriptors instead of FILE Essentially an int representing the file

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 is just an int #include <fcntl.h>... 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 return -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 and many others

How to read( ) n bytes #include <errno.h> #include <unistd.h>... 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( ), closedir( ) -- get a directory listing