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,

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.
File Types in Unix regular file - can be text, binary, can be executable directory file - "folder type" file FIFO file - special pipe device file, allows.
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
File System – Unix baed. An entry of Active File table: 1. Access Right: r/w/x 2. Process Count: no. of processes which are now referring to the file.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
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() –
Design and Implementation of the Log-based File I/O Library for Sandboxing CSE 542 Operating Systems Fall 2005 Qi Liao and Xuwen Yu.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
File Systems CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of File Systems2 References In order of relevance... 1)Maurice J. Bach, The.
CSE 451 Section 4 Project 2 Design Considerations.
Lecture 17 FS APIs and vsfs. File and File Name What is a File? Array of bytes. Ranges of bytes can be read/written. File system consists of many files,
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
CS 6560 Operating System Design Lecture 13 Finish File Systems Block I/O Layer.
CSCC69: Operating Systems Assignment 3 Review. Assignment Review Implement the file-related system calls – open, close, dup2 – read, write, lseek – chdir,
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
UNIX Files File organization and a few primitives.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
NCHU System & Network Lab Lab 14 File and Directory.
Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets.
CSCC69: Operating Systems Tutorial 10. Hints on testing Assignment 3 How to test tlb replacement algorithms? – Write a program that creates an array larger.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
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.
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.
CSCE 210 Data Structures and Algorithms
Chapter 16-2 Linked Structures
Operating System Hebrew University Spring 2004
Network Programming CSC- 341
Pointers & Functions.
CSE 333 – Section 3 POSIX I/O Functions.
File System Implementation
Henning Schulzrinne Columbia University
CSE 333 – Section 3 POSIX I/O Functions.
Function Overloading.
Pointers and dynamic objects
Pointers & Functions.
Modifying Objects Assignment operation Assignment conversions
File I/O in C++ II.
B+-tree Implementation
Presentation transcript:

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, size_t nbyte) ssize_t write(int fd, const void *buf, size_t nbyte) int close(int fd) off_t lseek(int fd, off_t offset, int reference) int stat(const char *file_name, struct stat *buf)

Each process records all the files opened in its context, the file descriptor table in the process table entry. Process context process descriptor file descriptor table File info file descriptor table contains all the files opened by the process

open/creat: find the first empty slot in the file descriptor table, use the slot to store a pointer to the file information. –Linear search close(fd) : among other things, makes the entry fd in the file descriptor table available. read/write: get the file information (such as current position) through the file descriptor table.

//example1.cpp #include using namespace std; main() { int f1, f2, f3, f4, f5, f6; f1 = open(“open.cpp”, O_RDONLY); cout << “f1 = “ << f1 << “\n”; f2 = open(“open.cpp”, O_RDONLY); cout << “f2 = “ << f2 << “\n”; f3 = open(“open.cpp”, O_RDONLY); cout << “f3 = “ << f3 << “\n”; close(f1); f4 = open(“open.cpp”, O_RDONLY); cout << “f4 = “ << f4 << “\n”; } Why f1 = 3, but not 1?

First three slots in the file descriptor table are occupied for standard I/O operation when the process is created. –Slot 0 ==> for standard input. –Slot 1 ==> for standard output. –Slot 2 ==> for standard error. Every time UNIX searches for an available slot in the open file table, it does a linear search starting from slot 0. The return of the first open == ?

Slots 0, 1, and 2 in the file descriptor table –used for standard I/O – They can be used just like regular files. Main() { main() { cout write(1, “hello, world!!\n”, } strlen(“hello, world!!\n”); } // example2.cpp –no need to open and close files 0, 1, 2 -- taken care of by the OS when it creates the process.

Let us redo the following program with read calls: main() { int I; cin >> I; cout << I; } See example2a.cpp

What if we close the standard I/O files? See example2b.cpp. Lost I/O capability!!

What if we close the standard I/O files and open files again? What is return value of the open call? See example4.cpp Now what happens when you do cout in example4.cpp? –This is called I/O redirection, instead of read from keyboard and print to the screen, cin will read from a file and cout will print to a file!!

#include main() { int f1, f2, f3, f4, f5, f6; f1 = open(“open.cpp”, O_RDONLY); cout << “f1 = “ << f1 << “\n”; f2 = open(“open.cpp”, O_RDONLY); cout << “f2 = “ << f2 << “\n”; close(1); f3 = open(“aaa”, O_RDONLY | O_WRONLY | O_CREAT, 0666); cout << “f3 = “ << f3 << “\n”; cerr << “f3 = “ << f3 << “\n”; } // example4.cpp, Output redirection What happens in This program?

Another way of redirect a file into standard I/O Duplicate an file descriptor: dup() #include int dup(int fd); return a new file descriptor having the common open file, file pointer and access mode with fd. Note: UNIX does a linear search in the file descriptor table for the available slot.

Difference between dup and open/create (Why dup instead of open) Using dup, the file position of the two duplicated open files is shared. Using open one file two times, there are two file positions for the two open files (open file != file) See extra1.cpp and extra2.cpp.

Using dup() to do standard I/O redirection. Implementing I/O redirection #include main() { char buf[100]; int fd; fd = open(“tmp111”, O_CREAT|O_WRONLY, 00777); close(1); dup(fd); close(fd); cout << “what is going on?\n”; cerr << “ \n”; } // example5.cpp

execv system call revisit: format: int execv(const char *path, char * argv[]) Wipe out the original process and load the new executable path into memory and execute path. When the system call execv is successful, every thing after the execv call in the original program will NOT be executed. Wipe out everything??? NOT precise!!!!! The open file descriptor table remains unchanged -->implication? If you do something to the file descriptor table before you call execv, you can redirect the standard I/O in the command to be executed!!!

void runcommand(char *cmd, char*argv[], char* readfile, char* writefile) { int status; if (fork() == 0) { if (readfile != NULL) { int fd = open(readfile, O_RDONLY); if (fd == -1) {cerr << “error\n”, exit(0);} close(0); dup(fd); close(fd); } if (writefile != NULL) { int fd = open(writefile, O_CREAT | O_WRONLY, 00777); if (fd == -1) {cerr << “error\n”, exit(0);} close(1); dup(fd); close(fd); } int stat = execv(cmd, argv); if (stat == (-1)) {cerr << “error\n”, exit(10);} } int stat = wait(&status); if (stat == -1) {cerr << “error return from wait\n”;} } // example6.cpp Running cmd with I/O redirection