File I/O (1) Prof. Ikjun Yeom TA – Mugyo

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

January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
January 13, Files – Chapter 2 Basic File Processing Operations.
Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
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.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
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() –
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.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
1 Project: File System Textbook: pages Lubomir Bic.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
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 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
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.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
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”
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”
File management. Outline File Concept and Structure Directory Structures File Organizations Access Methods Protection Unix file system calls.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
File Systems - Part I CS Introduction to Operating Systems.
I/O Software CS 537 – Introduction to Operating Systems.
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.
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.
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.
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.
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 -
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Andrew Hanushevsky: Basic I/O API’s
Today topics: File System Implementation
Robust I/O package Chapter 11 practice problems
Supplementary Material on Unix System Calls and Standard I/O
Lecture 31: Introduction to File System
Advanced Unix Programming
Introduction to Operating Systems File I/O
Files CMSC 15400: Introduction to Computer Systems Chapter 10
System-Level I/O Topics Unix I/O Robust reading and writing
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
“The course that gives CMU its Zip!”
CSE 333 – Section 3 POSIX I/O Functions.
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Reference: Advanced Programming in the UNIX Environment, Third Edition, W. Richard Stevens and Stephen A. Rago, Addison-Wesley Professional Computing Series,
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
IPC Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
FILE I/O File Descriptors I/O Efficiency File Sharing
CS 105 “Tour of the Black Holes of Computing”
Standard I/O Library Implementation
File I/O & UNIX System Interface
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Chapter 12: File-System Implementation CSS503 Systems Programming
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

File I/O (1) Prof. Ikjun Yeom TA – Mugyo Jung(renkropod1@gmail.com) TA – Sungho Lee(sk1528@naver.com) Computer Network Laboratory Sungkyunkwan University http://comnet.skku.edu

Announcement (1) All the scoring jobs will be done by script Even if your code has some minor flaws like print format, that would not be allowed Instead, you can fix your code and hand in late. -10% per each day Programming Assignment #0 & #1

Announcement (2) Grading policy 90% PA#0 PA#1 PA#2 PA#3 PA#4 PA#5 (10%) PA#1 (10%) PA#2 (10%) PA#3 (20%) PA#4 (20%) PA#5 (20%) Attendance (10%) midterm final Delay : -10% per day (max : -60%) After 6 day, you will get 0 point.

Contents File in Unix System calls for File I/O

Unix Files A Unix file is a sequence of m bytes: B0, B1, .... , Bk , .... , Bm-1 All I/O devices are represented as files: /dev/sda1 (hard disk partition) /dev/tty2 (terminal) Even the kernel is represented as a file: /dev/kmem (kernel memory image) /proc (kernel data structures)

Unix File Types Regular file Directory file Binary or text file Unix does not know the difference! Directory file A file that contains the names and locations of other files. Character special and block special files Terminals (character special) and disks (block special) FIFO (named pipe) A file type used for inter-process communication Socket A file type used for network communication between processes

Unix I/O Characteristics The elegant mapping of files to devices allows kernel to export simple interface called Unix I/O. All input and output is handled in a consistent and uniform way (“byte stream”) Basic Unix I/O operations (system calls): Opening and closing files open() and close() Changing the current file position (seek) lseek() Reading and writing a file read() and write()

Opening Files Opening a file informs the kernel that you are getting ready to access that file. Returns a small identifying integer file descriptor fd == -1 indicates that an error occurred Each process created by a Unix shell begins life with three open files associated with a terminal: 0: standard input 1: standard output 2: standard error int fd; /* file descriptor */ if ((fd = open("/etc/hosts", O_RDONLY)) < 0) { perror("open"); exit(1); }

Closing Files Closing a file informs the kernel that you are finished accessing that file. Closing an already closed file is a recipe for disaster in threaded programs (more on this later) Moral: Always check return codes, even for seemingly benign functions such as close() int fd; /* file descriptor */ int retval; /* return value */ if ((retval = close(fd)) < 0) { perror("close"); exit(1); }

Reading Files Reading a file copies bytes from the current file position to memory, and then updates file position. Returns number of bytes read from file fd into buf nbytes < 0 indicates that an error occurred. short counts (nbytes < sizeof(buf)) are possible and are not errors! char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open file fd ... */ /* Then read up to 512 bytes from file fd */ if ((nbytes = read(fd, buf, sizeof(buf))) < 0) { perror("read"); exit(1); }

Writing Files Writing a file copies bytes from memory to the current file position, and then updates current file position. Returns number of bytes written from buf to file fd. nbytes < 0 indicates that an error occurred. As with reads, short counts are possible and are not errors! char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open the file fd ... */ /* Then write up to 512 bytes from buf to file fd */ if ((nbytes = write(fd, buf, sizeof(buf)) < 0) { perror("write"); exit(1); }

File Offset An offset of an opened file can be set explicitly by calling lseek(), lseek64() Returns the new offset of the file fd. nbytes < 0 indicates that an error occurred. An offset can be set beyond the end of the file. If data is written at that point, a file “hole” is created. char buf[512]; int fd; /* file descriptor */ off_t pos; /* file offset */ /* Get current file offset */ pos = lseek(fd, 0, SEEK_CUR); /* The file offset is incremented by written bytes */ write(fd, buf, sizeof(buf)); /* Set file position to the first byte of the file */ pos = lseek(fd, 0, SEEK_SET);

Unix I/O Example Copying standard input to standard output one byte at a time. int main(void) { char c; while(read(0, &c, 1) != 0) write(1, &c, 1); exit(0); }

Dealing with Short Counts Short counts can occur in these situations: Encountering (end-of-file) EOF on reads. Reading text lines from a terminal. Reading and writing network sockets or Unix pipes. Short counts does not occur in these situations: Reading from disk files (except for EOF) Writing to disk files. How should you deal with short counts in your code? One way is to use the RIO (Robust I/O) package from your textbook’s csapp.c file (Appendix B).

Example #1 (1) H e l o . \r \n W h a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 0 size: 20 H e l o . \r \n W h a r y u ? EOF

Example #1 (2) H e l o . \r \n W h a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); // "Hello." read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 6 size: 20 H e l o . \r \n W h a r y u ? EOF

Example #1 (3) H e l o . \r \n W h a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); // "Hello.\r\n" lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 8 size: 20 H e l o . \r \n W h a r y u ? EOF

Example #1 (4) H e l o . \r \n W h a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 6 size: 20 H e l o . \r \n W h a r y u ? EOF

Example #1 (5) H e l o . \n W h a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 7 size: 20 H e l o . \n W h a r y u ? EOF

Example #1 (6) H e l o . \n W h a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 8 size: 20 H e l o . \n W h a r y u ? EOF

Example #1 (7) H e l o . \n w a r y u ? char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) path: "hello-dos.txt" position: 11 size: 20 H e l o . \n w a r y u ? EOF

Example #1 (8) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST); File state (FD: 3) :CLOSED

Example #1 (9) char filename[] = "hello-dos.txt"; int fd; char buffer[16]; off_t pos = 0; // long long; fd = open(filename, O_RDWR | O_CREAT, 0755); read(fd, buffer, 6); read(fd, buffer+6, 2); lseek(fd, -2, SEEK_CUR); buffer[0] = '\n'; write(fd, buffer, 1); lseek(fd, 8, SEEK_SET); strcpy(buffer, "How"); write(fd, buffer, 3); close(fd); fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0755); if (fd < 0) printf("errno : %d, error code - EEXIST : %d\n", errno, EEXIST);