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

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

Standard C Library Functions

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

CS240: Programming in C

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

Intermediate Programming, Spring 2017*

Input / Output Functions

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

File (1A) Young Won Lim 11/25/16

HIGH LEVEL FILE PROCESSING

System Software Experiment 1 Lecture 7

UNIX System Programming

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

PROGRAMMAZIONE I A.A. 2017/2018

EM108 Software Development for Engineers

Standard File Pointers

Input/Output and the Operating Systems

UNIT IV-2. The I/O library functions can be classified into two broad categories:

Standard I/O in C, Computer System and programming in C

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

CS 261 Fall Mike Lam, Professor. Structs and I/O

CSI 402 Lecture 2 (More on Files) 2 1 / 20

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

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

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

ENG120. Misc. Topics

File I/O. Preprocessor Macros

2009 S2 COMP File Operations

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

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

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

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

Chapter 12. Files (reference: Deitel s chap 11) chap8

C Basics And Concepts Input And Output

Data File and File Handling

25.2 Opening and Closing a File

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Computer Programming Unit v

Goals of this Lecture

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

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

System Programming. Standard Input/Output Library (Cont d)

Naked C Lecture 6. File Operations and System Calls

C File Processing: One-Page Summary

C-Refresher: Session 10 Disk IO

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Computer programming

Lecture 9: File Processing. Quazi Rahman

C PROGRAMMING. Characters and Strings File Processing Exercise

Computer System and programming in C

Input/Output: Advanced Concepts

Ch 11. C File Processing (review)

File IO and command line input CSE 2451

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

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

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

C Programming Language

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Chapter 11 File Processing

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Lecture6 File Processing

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

File Handling. Reference:

CSE 410: Systems Programming

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

Course organization. Course introduction ( Week 1)

Lecture 8: Structs & File I/O

Introduction to file management

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

mywbut.com 12 File Input/Output

File and Console I/O. CS449 Spring 2016

Operating System Labs. Yuanbin Wu

Lecture 23: System-Level I/O

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

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

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

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

#define PERLIO_NOT_STDIO 0 /* For co-existence with stdio only */ #include <perlio.h> /* Usually via #include <perl.h> */

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

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

DATA STRUCTURES USING C

M.CS201 Programming language

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

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

CSCI 171 Chapter Outlines

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

C mini reference. 5 Binary numbers 12

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

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

UNIX input and output

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

Transcription:

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

I/O Topics First, cover high-level I/O Next, talk about low-level device I/O I/O not part of the C language!

High-level I/O Hide device More than raw bytes Features Stream abstraction Formatted I/O String-based I/O Line-oriented buffering

Streams and Files Unix FILE object Delivers an ordered sequence of bytes Defined in <stdio.h> User-space library Built on top of low-level file descriptors Three default streams stdin stdout stderr

Inside a FILE Points to actual file Current offset Buffering (open for write) Internal character buffer of size BUFSIZE Writes are done to in-memory buffer When buffer is full (BUFSIZE), write out buffer Or if line-buffered stream (e.g. stdout) when \n is written

Inside a FILE (cont d) Buffering (open for read) Reads are done from in-memory buffer When buffer is empty, we read a chunk of BUFSIZE Why is the use of an internal buffer for I/O advantageous? Fewer underlying system calls (read and write) are needed

Buffering: FFLUSH Sometime we want to force buffer to spill to the OS why? see data immediately (OS auto-flushes K sec) int fflush (FILE *stream); fprintf (F, your output is %d ); fflush (F); // force it stderr always causes buffer to be flushed stdout with \n as well

OPEN/CLOSE #include <stdio.h> // NULL on failure, errno also set FILE *fopen (const char *filename, const char *mode); mode = r, w, a (others) int fclose (FILE *stream);

Character-based I/O #include <stdio.h> // SUBSET of calls int getc (FILE* stream); int putc (int char, FILE *stream); int ungetc (int char, FILE *stream); goes back 1 character Which header are things located? use man pages grep getc /usr/include/*.h

String-based I/O // reads nsize-1 characters or up to a // newline \n or EOF into buf (caller allocs) // appends \0 at the end of buf char *fgets (char *buf, int nsize, FILE *inf); what must be true of buf? // outputs buf better be \0 terminated int fputs (const char *buf, FILE *outf); Failure?

Importance of newline \n stdin is a line-oriented device \n (EOL), input is read when \n is seen ^D is EOF fgets on stdin will include \n in the string! gets does not

What about I/O for data types? Non-character or string? Formatted I/O! Formatted output and output strings int fprintf (FILE *outf, const char *fmt, args) int sprintf (char *string, // alloc! const char *fmt, args)

Formatted Output (cont d) Formats: %d, %f %c, %x, others int x=4; char str [100]; FILE *f; F = fopen ( myfile, w ); fprintf (F, %d, x); sprintf (str, %d %d %s, 12, x, hello ); => 12 3 hello FORMAT fields must match in type and #

Formatted Input Formatted input and input strings fscanf (FILE *in, const char *fmt, <ptr args allocated>); sscanf (FILE *in, const char *fmt, <ptr args allocated >);

Formatted Input (cont d) int i1, i2; float flt; char str1[10], str2[10]; char *str1, *str2; // ok? scanf ( 11 12 34.07 keith ben, %d %d %f %s %s, &i1, &i2, &flt, str1, str2); Do not forget to allocate and use &!?

Binary I/O We like ascii char-based files: cat,emacs, But they take up space Use binary I/O to read/write fewer bytes which saves space and is faster Works for fixed-size records and lots of em fread/fwrite (void *buffer, size_t size, size_t nitems, FILE *f);

Binary I/O Example typedef struct { int ss; // 8 digits int phone; // 10 digits } info_t; info_t mine = {12345678, 538493747}; fprintf (F, %d %d, mine.ss, mine.phone); fwrite ((void *)&mine, sizeof (info_t), 1, F); ascii file: 12345678 538493747 = (8+10)*1 bytes binary file: ^a93e&^%sd8s8 =? (4+4)*1 suppose info_t mine [100000];

Random I/O Thusfar, all I/O was assumed to be sequential fgets, fgets, returns consecutive lines current file ptr advances sequentially Sometime we need random I/O Think of any examples of applications that access data in a non-sequential pattern?

Example Suppose a file residents contains occupants of a hotel Line i is the occupant of room i Simple assumption: lines (even empties) are padded out to the same length, MAX_LINE Joe Smith Dave Jones Bill Wilson John Jay

Who is in room i? Sequential I/O How much must we read? Random I/O Advance file offset pointer w/o reading #include <stdio.h> int fseek (FILE *stream, long off, int whence); new offset from the beginning whence of one SEEK_SET, SEEK_CUR, SEEK_END

Room i? // move offset from the beginning fseek (F, (i-1)*max_line, SEEK_SET); // do the read fgets (F, ); // other call: what is the current file offset? long ftell (FILE *stream);

Next Time Continue discussion of input/output low-level Read Chapter 4 R&R