240-491 Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files 240-491 Special Topics in Comp. Eng. 2 Semester.

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

Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
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.
4.1 Operating Systems Lecture 11 UNIX Pipes Read Handout "An Introduction to Concurrency..."
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.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
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() –
1 System Calls, Stdio, and Course Wrap-Up COS 217 Professor Jennifer Rexford.
1 System Calls and Standard I/O Professor Jennifer Rexford
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,
1 Unix File System API Operating System Hebrew University Spring 2007.
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.
Chapter 4 UNIX I/O Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Operating Systems Recitation 1, March th, 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.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
Chapter Thirteen File Management1 System Programming File Management.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
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.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
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.
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.
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.
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.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
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.
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.
Lecture 31: Introduction to File System
Advanced Unix Programming
Week 12 - Wednesday CS222.
CS111 Computer Programming
Operating System Hebrew University Spring 2004
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
CSE 333 – Section 3 POSIX I/O Functions.
File Structure Related system calls
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
I/O and Process Management
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
FILE I/O File Descriptors I/O Efficiency File Sharing
Standard I/O Library Implementation
File I/O & UNIX System Interface
Week 12 - Wednesday CS222.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester 2, Low-level File I/O

Adv. UNIX: lowIO/162 Overview 1. Basic Operations 2. Why use Low-level Operations? 3. A Simple Example 4. File Descriptors 5. File Permissions Again continued

Adv. UNIX: lowIO/ open() 12. lseek() 7. creat() 13. Hotel Guests 8. close() 14. unlink() 9. read() 15. fcntl() 10. write() 16. Duplicating FDs 11. copyfile 17. File Pointers

Adv. UNIX: lowIO/ Basic Operations  NameMeaning open() Opens a file for reading or writing. creat() Creates an empty file for writing. close() Closes an open file. read() Reads data from a file. write() Writes data to a file. lseek() Moves to a specified byte in file. unlink() Removes a file.

Adv. UNIX: lowIO/ Why use Low-level Operations? v No buffering of I/O –useful for network programming and reading/writing to certain peripherals (e.g. tape drives) v No conversion of I/O –no transformation of input to integers, floats, etc.; output can be any byte sequence v Building blocks for more complex I/O.

Adv. UNIX: lowIO/ A Simple Example: oprd.c #include #include #include #include #include void main() { int fd, nread; char buf[1024]; if ((fd = open("data-file", O_RDONLY)) < 0) { perror("open"); exit(-1); } nread = read(fd, buf, 1024); close(fd); buf[nread] = '\0'; /* so can print */ printf("buf: %s\nnread: %d\n", buf, nread); }

Adv. UNIX: lowIO/ File Descriptors v A file descriptor is a small, non-negative integer. v A file descriptor identifies an open file to the low-level operations. v Standard descriptors: 0represents stdin 1 stdout 2 stderr

Adv. UNIX: lowIO/ File Permissions Again  Earlier chmod examples used letters to represent permissions: –e.g. chmod a+r file  Most low-level operations (and chmod ) represent permissions as octal values: –e.g. chmod 0644 file

Adv. UNIX: lowIO/169 File Permissions as Octals OctalSymbolic Meaning of Permission 0400r w x r w x r w x

Adv. UNIX: lowIO/1610 Example  Add octal values together to get the complete file permission: OctalMeaning 0400r w r r-- =============== 0644rw- r-- r--  Use in chmod : chmod 0644 file

Adv. UNIX: lowIO/ open()   #include #include #include int open(char *pathname, int flag /*, mode_t mode */ ); v Return file descriptor if ok, -1 for error an octal value (as in chmod), used when creating a file

Adv. UNIX: lowIO/1612 Some open() Flags O_RDONLY Open file for reading only. O_WRONLY Open file for writing only. O_RDWR Open file for reading & writing. Possibly combined with: O_APPEND Append to file when writing. O_CREAT Create file if is does not exist (requires the 3rd mode argument). O_EXCL Return -1 error if file is to be created and already exists.

Adv. UNIX: lowIO/1613 Using Bitwise OR  open() flags can be combined with the bitwise OR operator ‘|”: fd = open(file, O_WRONLY | O_APPEND);  Now each write(fd, buf, BUFSIZE) means append to the end of the file.

Adv. UNIX: lowIO/ creat()  #include #include #include int creat(char *pathname, mode_t mode); v Creates a new file, or truncates an old one. v Return file descriptor if ok, -1 on error.

Adv. UNIX: lowIO/1615  mode gives access permission of resulting new file: 0644(rw- r-- r--)  mode only has meaning if the file is new –so it cannot be used to change the mode of an existing file

Adv. UNIX: lowIO/1616 Create a new file #include #include #include #include void main() { int fd; if ((fd = creat(“new-file”,0644)) #include #include #include void main() { int fd; if ((fd = creat(“new-file”,0644)) < 0){ printf(“cannot create new-file\n”); exit(1); } /* rest of program */

Adv. UNIX: lowIO/1617 Replacing creat() with open()  creat() only opens a file for writing; to read it the file must be closed and opened for reading.  creat() can be replaced by open() : open(“new-file”, O_WRONLY | O_CREAT | O_TRUNC, 0644)

Adv. UNIX: lowIO/1618 Other Versions  creat() version which can read and write: open(“new-file”, O_RDWR | O_CREAT | O_TRUNC, 0644)  Avoid truncation of old file (append instead): open(“new-file”, O_WRONLY|O_CREAT|O_APPEND, 0644)

Adv. UNIX: lowIO/1619 One User at a Time  Create lock if it doesn’t already exist, otherwise fail (and return -1): fd = open(“lock”, O_WRONLY|O_CREAT|O_EXCL, 0644);  Use lock to protect access to another file –open “ accounts ” only if lock can be opened –after processing “ accounts ”, delete lock

Adv. UNIX: lowIO/1620 Diagram of Locking "lock" file "accounts" file : if ((fd = open("lock", O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0)) do nothing else { open "accounts" : close "accounts" delete "lock" } program 1 : if ((fd = open("lock", O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0)) do nothing else { open "accounts" : close "accounts" delete "lock" } program 2

Adv. UNIX: lowIO/ close()  #include int close(int fd); v Close a file: return 0 if ok, -1 on error. v Useful since the number of open files is limited (~20)

Adv. UNIX: lowIO/ read()  #include int read(int fd, void *buffer, unsigned int nbytes);  Attempts to read nbytes into the buffer array –no conversion of bytes (characters) –may read less than specified amount (e.g. at end) v Returns the number of bytes read, 0 if end of file, -1 on error.

Adv. UNIX: lowIO/1623 The Read-Write Pointer v The R-W Pointer records the position of the next byte in the file to be read (or written).  It is implicit (hidden) in each use of read() and write() 11+n read(..., n)

Adv. UNIX: lowIO/1624 Count Characters: countchars.c #include #include #include #include #include #define SIZE 512 /* block of chars read at a time */ int main() { char buffer[SIZE]; int fd, j; : continued

Adv. UNIX: lowIO/1625 long total = 0; if ((fd = open(“foo”, O_RDONLY)) 0) total += j; printf(“total chars: %ld\n”, total); close(fd); return 0; }

Adv. UNIX: lowIO/1626 v UNIX systems are often configured to read/write in blocks of 512 or 1024 bytes (the disk blocking factor).  Can use BUFSIZ from stdio.h which contains a default disk blocking factor. Notes

Adv. UNIX: lowIO/ write()  #include int write(int fd, void *buffer, unsigned int nbytes); v Returns number of bytes written if ok, -1 on error.  write() starts at the current R-W pointer position, and pointer is moved as file is written.

Adv. UNIX: lowIO/1628 Example : int fd; char header1[512], header2[512]; : if ((fd = creat(“foo”, 0644)) != -1) : write(fd, header1, 512); write(fd, header2, 512); :

Adv. UNIX: lowIO/ copyfile #include #include #include #include #include int copyfile(char *nm1, char *nm2); int main() { int result; result = copyfile(“test.in”, “test.out”); printf("Result code of copy: %d\n", result); return 0; } continued

Adv. UNIX: lowIO/1630 int copyfile(char *nm1, char *nm2) { int infd, outfd, nread; char buffer[BUFSIZ]; if ((infd = open(nm1,O_RDONLY))< 0) return -1; if ((outfd = creat(nm2,0644)) < 0){ close(infd); return -2; } : continued

Adv. UNIX: lowIO/1631 while ((nread = read(infd, buffer,BUFSIZ)) > 0) { if (write(outfd, buffer, nread) < nread) { close(infd); close(outfd); return -3; /* write error */ } } close(infd); close(outfd); return 0; }

Adv. UNIX: lowIO/1632 copyfile() & BUFSIZ v BUFSIZ Real timeUser timeSystem time 13: : : : : : : : : : : : : :01.1 v Most saving is made by reducing no. of system calls. $ time copyfile

Adv. UNIX: lowIO/ lseek() v #include #include long int lseek(int fd, long int offset, int start); v Change position of R-W pointer: return new file offset if ok, -1 on error. v Start names (and numbers): –SEEK_SET 0start of file –SEEK_CUR 1current R-W pointer posn –SEEK_END 2end of file

Adv. UNIX: lowIO/1634 Examples   : fd = open(filename, O_RDWR); lseek(fd, 0L, SEEK_END); write(fd, buffer, BUFSIZ); : v v /* get filesize in bytes */ long int filesize; filesize = lseek(fd, 0L, SEEK_END);

Adv. UNIX: lowIO/ Hotel Guests  The guests file has the format:..... name of guest, ending in '\n'; always 41 chars

Adv. UNIX: lowIO/1636 Task v Write the function: int get_guest(int fd, int room_num, char *name);  Return 1 if the guest in room_num is found ( name is also bound); 0 otherwise.

Adv. UNIX: lowIO/1637 Code #include #include #include #include #include #define NAMELEN 41 #define NROOMS 5000 : continued

Adv. UNIX: lowIO/1638 int main() { int fd, i; char name[NAMELEN]; if ((fd = open(“guests”,O_RDONLY))== -1){ fprintf(stderr,“Cannot read guests\n”); exit(1); } for (i=1; i <= NROOMS; i++) if (get_guest(fd, i, name) == 1) printf(“Room %d, %s\n”, i, name); else printf(“Error in room %d\n”, i); return 0; } continued

Adv. UNIX: lowIO/1639 int get_guest(int fd, int rnum, char *nm) { long int offset; int nread; offset = (rnum-1)*NAMELEN; if (lseek(fd, offset, SEEK_SET) < 0) return 0; nread = read(fd, nm, NAMELEN); if (nread < NAMELEN) return 0; /* read() error */ else nm[nread-1] = ‘\0’; /* overwrite \n */ return 1; }

Adv. UNIX: lowIO/ unlink()  #include int unlink(char *pathname); v Remove a file: return 0 of ok, -1 on error. –e.g. unlink(“/tmp/tmpfile”); v Must have permissions to execute and write in the file’s directory –for the cd and change to the contents

Adv. UNIX: lowIO/ fcntl()  #include #include #include int fcntl(int fd, int cmd, int arg); v Used to view/alter the behaviour of the file.  Two (of 10) cmd values: F_GETFL get current status flags F_SETFL set a status flag

Adv. UNIX: lowIO/1642 Some File Status Flags for fcntl() FlagDescription O_RDONLY Open for reading only (0). O_WRONLY Open for writing only (1). O_RDWR Open for reading and writing (2). O_APPEND Append on each write. :

Adv. UNIX: lowIO/1643 Using O_ACCMODE  O_RDONLY, O_WRONLY and O_RDWR are not separate bits (e.g. not 0, 2, 4), and so their values can be hard to test.  One solution is to use the O_ACCMODE mask.

Adv. UNIX: lowIO/1644 : int accmode, val; if ((val = fcntl(fd, F_GETFL, 0)) < 0) printf(“Some error\n”); else { accmode = val & O_ACCMODE; if (accmode == O_RDONLY) printf(“read”); else if (accmode == O_WRONLY) printf(“write”); else if (accmode == O_RDWR) printf(“read write”); else printf(“access mode error”); if (val & O_APPEND) printf(“, append”); :

Adv. UNIX: lowIO/1645 Using F_SETFL  Main use is to change O_APPEND : if (fcntl(fd, F_SETFL, O_APPEND) < 0) printf(“Setting append failed\n”); else...

Adv. UNIX: lowIO/ Duplicating File Descriptors   #include int dup(int fd);  Make a new file descriptor ( fd2 ) refer to the same file as fd : fd2 = dup(fd) v Useful for concurrent programming, where two processes refer to the same file.

Adv. UNIX: lowIO/1647 Duplicating Diagram file fd 3 fd2 = dup(fd); file fd 3 fd2 4

Adv. UNIX: lowIO/1648 Another Approach   #include int dup2(int fd, int fd2);  Make the existing fd2 descriptor refer to the same file as fd.  If fd2 is already open, close its file before redirecting it.

Adv. UNIX: lowIO/1649  Main use is for redirecting stdio : dup2(fd, 0) v Same as the ‘hack’: close(0); dup(fd);

Adv. UNIX: lowIO/1650 dup2(fd,0) Diagram file fd program Before After file fd program stdin 0

Adv. UNIX: lowIO/ File Pointers  Connect a file pointer to fd ’s file with: #include FILE *fdopen(int fd, char *mode);  mode is usual thing: “ r ”, “ w”, “ rb ”, etc. v Useful for doing formatted I/O on top of pipes and network comunication.