NCHU System & Network Lab Lab 13 File I/O & Standard I/O.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
NCHU System & Network Lab Lab 15 Record Locking. NCHU System & Network Lab Record Locking (1/4) What happens when two process attempt to edit the same.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
Kernel File Interface operating systems (or programming I/O in Unix)
Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O   4. Standard I/O Library 5. Files and Directories 6. System Data.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
File I/O.
CS1061 C Programming Lecture 18: Sequential File Processing A. O’Riordan, 2004, 2007 updated.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
Operating Systems Recitation 1, March th, 2002.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
UNIX Files File organization and a few primitives.
File IO and command line input CSE 2451 Rong Shi.
NCHU System & Network Lab Lab 11 Memory Mapped File.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 7 Files By C. Shing ITEC Dept Radford University.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
NCHU System & Network Lab Lab 14 File and Directory.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
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.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
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.
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
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.
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.
Advanced Programming in the UNIX Environment Hop Lee.
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Error handling I/O Man pages
Lecture 11 File input/output
File I/O.
What you need for the 1st phase of project
Files I/O, Streams, I/O Redirection, Reading with fscanf
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Henning Schulzrinne Columbia University
Advanced UNIX progamming
File Access (7.5) CSE 2031 Fall February 2019.
FILE I/O File Descriptors I/O Efficiency File Sharing
Module 12 Input and Output
Lab 10 Paging.
Standard I/O Library Implementation
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

NCHU System & Network Lab Lab 13 File I/O & Standard I/O

NCHU System & Network Lab Introduction Each regular file, device, connection socket, directory…etc, are treated as a file by Linux. –We can perform I/O operations on these different types of files. Ex : open, read, write … etc –We will start our discussion of these functions on: Basic file I/O Standard I/O library

NCHU System & Network Lab File I/O A set of methods dealing with files provided by Linux. Each opened file is referred to a file descriptor: –fd is a non-negative integer. –creat() and open() functions return a fd to process. UNIX system shells associate file descriptors : –0 : standard input –1 : standard output –2 : standard error

NCHU System & Network Lab File I/O (cont.) These are functions introduced later : #include #include int open (const char *pathname, int oflag, mode_t mode); int creat (const char *pathname, mode_t mode); int close (int filedes); off_t lseek (int filedes, off_t offset, int whence); ssize_t read (int filedes, void *buf, size_t nbytes); ssize_t write (int filedes, const void *buf, size_t nbytes);

NCHU System & Network Lab open() A file is opened or created by calling open() function. –pathname is the name of the file. –This function has a multitude of options are specified by oflag argument. –mode specifies the access permission of file. #include int open (const char *pathname, int oflag, mode_t mode);

NCHU System & Network Lab open (cont.) oflags of open() Flagsdescription O_RDONLYOpen for reading only O_WRONLYOpen for writing only O_RDWROpen for reading and writing O_APPENDAppend to the end of file on each write O_CREATCreate a file if it doesn’t exist. This requires mode argument O_EXCLGenerate an error if O_CREAT is set and the file already exists. O_TRUNCIf the file exists and is opened,truncate its length to 0. O_NONBLOCKSet non-blocking mode on this opened file. O_NOCTTYDo not allocate the device as the controlling terminal. O_RSYNCRead operation on this fd waits until any pending writes finished. O_DSYNCEach write wait for physical I/O to complete except file attributes. O_SYNCEach write wait for physical I/O to complete,include file attributes.

NCHU System & Network Lab creat() This create function is equivalent to open (pathname, O_WRONLY|O_CREAT|O_TRUNC, mode) –One deficiency with creat() is that the file is opened only for writing. –A better way is to use open() with O_CREAT instead of creat(). #include int creat (const char *pathname, mode_t mode);

NCHU System & Network Lab lseek() Every open file has an associated “current file offset” –A non-negative integer that measures the number of bytes from the beginning of the file. –Read/Write operation increments cur_offset value. –The interpretation of the offset argument depends on the value of whence argument. #include off_t lseek (int filedes, off_t offset, int whence); whence SEEK_SETFrom the beginning SEEK_CURFrom the current file offset SEEK_ENDFrom the end of the file

NCHU System & Network Lab read()/write() Data is read from an open file with the read() function. –It will read nbytes bytes into buf from file filedes. Return values –Count number of read bytes, 0 if EOF, -1 on error #include ssize_t read (int filedes, void *buf, size_t nbytes); ssize_t write (int filedes, void *buf, size_t nbytes);

NCHU System & Network Lab Consistency Traditional implementations of UNIX system have a buffer cache in the kernel which most disk I/O passes through. –Ex : delayed write –Three functions are provided to ensure consistency of the file system on disk with the data of buffer. sync() fsync() fdatasync() #include int fsync (int fd); int fdatasync (int fd); void sync(void);

NCHU System & Network Lab Consistency (cont.) Sync () –It simply schedules all the modified block buffers in RAM to be write into disk, and it returns without waiting for write completed. fsync() –It refers only to a single file, specified by the fd, and waits for the disk writes to complete before returning. fdatasync() –It affects only data portions of a file.

NCHU System & Network Lab Example : Basic I/O #include #define BUFFERSIZE 1 int main() { char buf[BUFFERSIZE]; int in,out,readn; in = open("file.in",O_RDONLY); out = open("file.out",O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR); while((readn = read(in,buf,sizeof(buf))) > 0) write(out,buf,readn); exit(0); }

NCHU System & Network Lab Standard I/O Library This library is specified by ISO C standard –Each opened file is associated with a stream. –A file is opened with a FILE structure. FILE is a structure that contains all information required by the standard I/O library to manage the stream. –Buffer allocation and optimal I/O.

NCHU System & Network Lab Buffering of Standard I/O The goal of the buffering is to use minimum number of read() and write() calls. –Types of buffering : Fully buffered Line buffered –The library performs I/O when a newline character is encountered. Unbuffered

NCHU System & Network Lab Buffering of Standard I/O (cont.) We can change the buffering by calling these two functions before any other operations on the stream: –setbuf() turns buffering on or off with a buffer buf of length BUFSIZ or NULL. –setvbuf() specifies exactly which type of buffering. #include void setbuf (FILE *restrict fp, char *restrict buf); int setvbuf (FILE *restrict fp, char *restrict buf, int mode, size_t size); _IOFBFFully buffered _IOLBFLine buffered _IONBFunbuffered

NCHU System & Network Lab Opening a Stream fopen() function opens a specified file and returns a *FILE pointer of the stream. Modedescription rOpen for reading wCreate for writing or truncate to 0 length aAppend, for writing at EOF r+Open for reading and writing w+Truncate to 0 length or create for R/W a+Open or create for R/W at EOF #include FILE *fopen (char *restrict pathname, char *restrict type);

NCHU System & Network Lab Other Functions for Standard I/O #include Int getc (FILE *fp); int fgetc (FILE *fp); int putc (FILE *fp); int fputc (int c,FILE *fp) char *gets (char *buf); char *fgets (char *buf, int n, FILE *fp); char *puts (char *str); char *fputs (char *str, FILE *fp); int printf (char *format, …); int fprintf (FILE *fp, const char *format, …); int scanf (const char *restrict format, … ); int fscanf (FILE *fp, char *format, …);

NCHU System & Network Lab File I/O vs. Standard I/O Standard I/O library ends up calling basic I/O routines. Standard I/O is specified by ISO C standard, and another one is specified by POSIX.1 (Portable Operating System Interface). –POSIX.1 includes ISO C standard library. fopen() deals with FILE structure whereas open() uses a file descriptor integer.

NCHU System & Network Lab File I/O vs. Standard I/O (cont.) More flexible buffering than basic I/O that take place with standard library. Device files only can be opened by open().

NCHU System & Network Lab Example : Standard I/O

NCHU System & Network Lab Lab I/O efficiency –We want to know how does File I/O or standard I/O work to improve I/O efficiency. Create a 2MB file and copy it into a new output file. Use “clock()” to measure the process time of each case. Repeat the step above in different cases and show the result of all : –Basic I/O »write() to output file 1 byte each time »write() to output file 64 bytes each time »open() with O_SYNC set, write() 64 bytes each time »write() followed by fsync(), 64 byes each time –Standard I/O »fopen() with setbuf( unbuffered ) »fopen()

NCHU System & Network Lab Lab (cont.) Clock() –Returns the number of clock ticks elapsed since the program was launched. –CLOCKS_PER_SEC represents the number of clocks in a second. #include #include clock_t start,end; start = clock(); delay(2000); end = clock(); elapsetime = (end-start)/(double)CLOCKS_PERSEC ;

NCHU System & Network Lab Lab (cont.)

NCHU System & Network Lab Reference Advanced Programming in the UNIX Environment 2nd Author : Richard Stevens, Stephen A.Rago, Publisher : Addison-Wesley Beginning Linux Programming Author : Richard Stones, Neil MatthewPublisher : Wrox jollen’s Bloghttp:// C++ Resource Networkhttp://