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.

Slides:



Advertisements
Similar presentations
File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
Advertisements

© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
January 13, Files – Chapter 2 Basic File Processing Operations.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Folk/Zoellick/Riccardi, File Structures 1 Objectives: To get familiar with Logical files vs. physical files File processing operations File processing.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
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() –
1 System Calls, Stdio, and Course Wrap-Up COS 217 Professor Jennifer Rexford.
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
1 System Calls and Standard I/O Professor Jennifer Rexford
CSE 451 Section 4 Project 2 Design Considerations.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
1 Unix File System API Operating System Hebrew University Spring 2007.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Lecture 3: System Calls & API Standards. Lecture 3 / Page 2AE4B33OSS Silberschatz, Galvin and Gagne ©2005 Contents Implementation of API System call types.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
Operating Systems Recitation 1, March th, 2002.
Chapter Thirteen File Management1 System Programming File Management.
1 System Programming Chapter 3 File I/O. 2 Announcement The first exercise is due today. We will NOT accept late assignment this time. –Submission site.
UNIX Files File organization and a few primitives.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
System Interface Interface that provides services from the OS (Higher than BIOS) Memory Scheduler File/Storage System Inter-process Communication and Network,
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Lecture 3 Introduction to Unix Systems Programming: Unix File I/O System Calls.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
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.
1 PART II System Programming. 2 Chapter 10 The File and I/O System.
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.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Overview Dr. Xiao Qin Auburn University
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 -
Lecture 3: System Calls & API Standards
Lecture 2 Linux Basic Commands,Shell and Make
Error handling I/O Man pages
Andrew Hanushevsky: Basic I/O API’s
Operating Systems Moti Geva
Lecture 12 CIS 208 Friday, March 3rd , 2005.
Lecture 31: Introduction to File System
Lecture 11 File input/output
CSE 333 – Section 3 POSIX I/O Functions.
Unix File Interface int open(const char* path, int flags, mode_t permissions); ssize_t read(int fd, void* buffer, size_t count); ssize_t write(int fd,
Operating System Hebrew University Spring 2004
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.
Text and Binary File Processing
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
CSE 333 – Section 3 POSIX I/O Functions.
Henning Schulzrinne Columbia University
CSE 333 – Section 3 POSIX I/O Functions.
FILE I/O File Descriptors I/O Efficiency File Sharing
Operating System Hebrew University Spring 2009
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

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 operation in C/C++ –printf(), scanf() in C –cin, cout, cerr in C++ At the UNIX OS interface, File and I/O operations in the UNIX system share the same interface. –open(), read(), write(),close() …… –I/O devices are treated as special files (see /dev directory)

File.vs. open file –file - passive container of data; a named sequence of bytes –open file – a file that is opened in a process active sources and sinks for data, need to maintain current position of the open file. –File –Current file position »Maintained by the OS »May shared among different processes

File and I/O system calls –open a file int open(const char* path, int flags, mode_t modes) // path: file name, flags: type of open, modes: file permission, only when the file is being created. // return a non negative number when successful, -1 when fail Example: fd = open(“/etc/passwd”, O_RDONLY); –Each open creates a new open file (even if the same file is opened). –read a file ssize_t read(int fd, void *buf, size_t nbyte) // read nbyte data from file fd to buffer buf // fd: file descriptor // buf: pointer to the buffer to hold the data from the file // nbyte: number of bytes to be read // return total number of bytes read, 0 for end of file, -1 when error Example: number = read(fd, buffer, count);

File and I/O system calls (continue) –write to a file ssize_t write(int fd, const void *buf, size_t nbyte); Example: number = write(fd, buffer, count); –close a file int close(int fd); –create a file int creat(const char *path, mode_t mode); // the meaning of mode, // also determined by mask, see man -a umask. creat(path, mode) = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)

void FileCopy(char *fromFile, char *toFile) { int fromFD = open(fromFile, O_RDONLY); if (fromFD < 0) { cerr << “Error opening” << fromFile << “\n”; return; } int toFD = creat(toFile, 0666); if (toFD < 0) { cerr << “Error opening” << toFile << “\n”; close(fromFD); return; } while (1) { char ch; int n = read (fromFD, &ch, 1); if (n <= 0) break; // end of file n = write(toFD, &ch, 1); if (n < 0) {cerr << “Error\n”; return;} } close(fromFD); close(toFD); } Example1.cpp

Control the position to read/write: lseek from/to where the file is read/written: (file pointer, an implicit parameter) #include off_t lseek(int fd, off_t offset, int whence) whence: SEEK_SET begin + offset SEEK_CUR current + offset SEEK_END end + offset Example: lseek(fd, (off_t)(-1), SEEK_CUR); lseek(fd, (off_t)0, SEEK_END); lseek(fd, (off_t)0, SEEK_SET);

// Example2.cpp void ReverseFile(char *fromFile, char *toFile) { int fromFd = open(fromFile, O_RDONLY); int toFd = creat(toFile, 0666); lseek(fromFd, (off_t)(-1), SEEK_END); while(1) { char ch; int n = read(fromFd, &ch, 1); n = write(toFd, &ch, 1); if (n < 0) {cerr << “Error\n”; exit(0);} if (lseek(fromFd, (off_t)(-2), SEEK_CUR) < 0) break; } close(toFd); close(fromFd); }

File meta-information - information about the file that isn’t in the file, such as: –Owner, permissions, timestamps, size, etc. –Try an “ls -l” on a typical UNIX file #include int stat(const char *path, struct stat *buf); –Determine whether file *path exists. –Determine whether the file is executable. –Other meta-information, such as access time, etc. –check out what you can do with ‘utime’.

// example3.cpp int check_executable(char *filename) { struct stat buf; int j = stat(filename, &buf); if (j == (-1)) return FILE_NOT_EXIST; if (buf.st_mode & 0111) return EXECUTABLE; return NOT_EXECUTABLE; }