Goals of this Lecture

Similar documents
I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Process Management! Goals of this Lecture!

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

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

CS240: Programming in C

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

Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

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

CSE 333 SECTION 3. POSIX I/O Functions

2009 S2 COMP File Operations

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

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

Operating System Labs. Yuanbin Wu

UNIX System Programming

Files. Eric McCreath

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

File Access. FILE * fopen(const char *name, const char * mode);

UNIX input and output

CS240: Programming in C

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

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

CSE 410: Systems Programming

Summer June 15, 2010

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

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

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

CSE 333 SECTION 3. POSIX I/O Functions

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

Input / Output Functions

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

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

Input and Output System Calls

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

Naked C Lecture 6. File Operations and System Calls

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

Standard File Pointers

Operating systems. Lecture 7

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

PROGRAMMAZIONE I A.A. 2017/2018

Basic and Practice in Programming Lab 10

Operating System Labs. Yuanbin Wu

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

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

System Software Experiment 1 Lecture 7

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

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

C Programming 1. File Access. Goutam Biswas. Lect 29

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

Lecture 21 Systems Programming in C

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

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

Chapter 10. The UNIX System Interface

Recitation 8: Tshlab + VM

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Lecture 8: Structs & File I/O

CSI 402 Lecture 2 Working with Files (Text and Binary)

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

25.2 Opening and Closing a File

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

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

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

Process Creation in UNIX

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Data and File Structures Chapter 2. Basic File Processing Operations

Lecture 9: File Processing. Quazi Rahman

FILE SYSTEMS. Jo, Heeseung

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

File I/O. Arash Rafiey. November 7, 2017

File I/0. Advanced Programming in the UNIX Environment

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

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

Computer Programming: Skills & Concepts (CP) Files in C

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

ENG120. Misc. Topics

Fundamental File Processing Operations 2. Fundamental File Processing Operations

C Basics And Concepts Input And Output

Input/Output. Input/Output and Files. Input/Output. I/O System Objectives

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

UNIX System Calls. Sys Calls versus Library Func

Lecture 7: file I/O, more Unix

EM108 Software Development for Engineers

8. Characters, Strings and Files

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

C File Processing: One-Page Summary

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Files and Directories

Transcription:

I/O Management 1

Goals of this Lecture Help you to learn about: The Unix stream concept Standard C I/O functions Unix system-level functions for I/O How the standard C I/O functions use the Unix system-level functions Additional abstractions provided by the standard C I/O functions Streams are a beautiful Unix abstraction 2

Stream Abstraction Any source of input or destination for output E.g., keyboard as input, and screen as output E.g., files on disk or CD, network ports, printer port, Accessed in C programs through file pointers E.g., FILE *fp1, *fp2; E.g., fp1 = fopen("myfile.txt", "r"); Three streams provided by stdio.h Streams stdin, stdout, and stderr Typically map to keyboard, screen, and screen Can redirect to correspond to other streams E.g., stdin can be the output of another program E.g., stdout can be the input to another program 3

Sequential Access to a Stream Each stream has an associated file position Starting at beginning of file (if opened to read or write) Or, starting at end of file (if opened to append) file file Read/write operations advance the file position Allows sequencing through the file in sequential manner Support for random access to the stream Functions to learn current position and seek to new one 4

Standard I/O Functions Portability Generic I/O support for C programs Specific implementations for various host OSes Invokes the OS-specific system calls for I/O Abstractions for C programs Streams Line-by-line input Formatted output Additional optimizations user Buffered I/O OS Safe writing Appl Prog Stdio Library File System 5

Example: Opening a File FILE *fopen("myfile.txt", "r") Open the named file and return a stream Includes a mode, such as r for read or w for write Creates a FILE data structure for the file Mode, status, buffer, Assigns fields and returns a pointer Opens or creates the file, based on the mode Write ( w ): create file with default permissions Read ( r ): open the file as read-only Append ( a ): open or create file, and seek to the end 6

Example: Formatted I/O int fprintf(fp1, "Number: %d\n", i) Convert and write output to stream in specified format int fscanf(fp1, "FooBar: %d", &i) Read from stream in format and assign converted values Specialized versions printf( ) is just fprintf(stdout, ) scanf( ) is just fscanf(stdin, ) 7

Layers of Abstraction User process Operating System Appl Pgm Stdio Library File System Storage Driver FILE * stream hierarchical file system variable-length segments disk blocks int fd File descriptor: An integer that uniquely identifies an open file Disk 8

System-Level Functions for I/O int creat(char *pathname, mode_t mode); Create a new file named pathname, and return a file descriptor int open(char *pathname, int flags, mode_t mode); Open the file pathname and return a file descriptor int close(int fd); Close fd int read(int fd, void *buf, int count); Read up to count bytes from fd into the buffer at buf int write(int fd, void *buf, int count); Writes up to count bytes into fd from the buffer at buf int lseek(int fd, int offset, int whence); Assigns the file pointer of fd to a new value by applying an offset 9

Example: open() Converts a path name into a file descriptor int open(const char *pathname, int flags, mode_t mode); Arguments Pathname: name of the file Flags: bit flags for O_RDONLY, O_WRONLY, O_RDWR Mode: permissions to set if file must be created Returns File descriptor (or a -1 if an error) Performs a variety of checks E.g., whether the process is entitled to access the file Underlies fopen() 10

Example: read() Reads bytes from a file descriptor int read(int fd, void *buf, int count); Arguments File descriptor: integer descriptor returned by open() Buffer: pointer to memory to store the bytes it reads Count: maximum number of bytes to read Returns Number of bytes read Value of 0 if nothing more to read Value of -1 if an error Performs a variety of checks Whether file has been opened, whether reading is okay Underlies getchar(), fgets(), scanf(), etc. 11

Example: A Simple getchar() int getchar(void) { char c; if (read(0, &c, 1) == 1) return c; else return EOF; } Read one character from stdin File descriptor 0 is stdin &c points to the buffer 1 is the number of bytes to read Read returns the number of bytes read In this case, 1 byte means success 12

Making getchar() More Efficient Poor performance reading one byte at a time Read system call is accessing the device (e.g., a disk) Reading one byte from disk is very time consuming Better to read and write in larger chunks Buffered I/O Read a large chunk from disk into a buffer Dole out bytes to the user process as needed Discard buffer contents when the stream is closed Similarly, for writing, write individual bytes to a buffer And write to disk when full, or when stream is closed Known as flushing the buffer 13

Better getchar() with Buffered I/O int getchar(void) { static char base[1024]; static char *ptr; static int cnt = 0; if (cnt--) return *ptr++; persistent variables base } cnt = read(0, base, sizeof(base)); if (cnt <= 0) return EOF; ptr = base; ptr return getchar(); But, many functions may read (or write) the stream 14

Details of FILE in stdio.h (K&R 8.5) #define OPEN_MAX 20 /* max files open at once */ typedef struct _iobuf { int cnt; /* num chars left in buffer */ char *ptr; /* ptr to next char in buffer */ char *base; /* beginning of buffer */ int flag; /* open mode flags, etc. */ char fd; /* file descriptor */ } FILE; extern FILE _iob[open_max]; #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2]) 15

A Funny Thing About Buffered I/O The standard library also buffers output; example: int main(void) { printf("step 1\n"); sleep(10); printf("step 2\n"); return 0; } Run a.out > out.txt & and then tail -f out.txt To run a.out in the background, outputting to out.txt And then to see the contents on out.txt Neither line appears till ten seconds have elapsed Because the output is being buffered Add fflush(stdout) to flush the output buffer fclose() also flushes the buffer before closing 16

Summary System-level I/O functions provide simple abstractions Stream as a source or destination of data Functions for manipulating streams Standard I/O library builds on system-level functions Calls system-level functions for low-level I/O Adds buffering Powerful examples of abstraction Application pgms interact with streams at a high level Standard I/O library interact with streams at lower level Only the OS deals with the device-specific details 17