CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.

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

CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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() –
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
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,
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
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.
1 UNIX Systems Programming Interprocess communication.
Chapter 5 Files and Directories Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
CS 241 Section (04/29/2010). In Section Today… MP7 HW3 Clarifications File System Topics.
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.
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.
Files and Directories File types stat functions for file information
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
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.
Navigating Directories
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
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.
CSCI 330 UNIX and Network Programming
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.
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.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
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.
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.
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.
Chapter 39 File and Directory Chien-Chung Shen CIS/UD
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.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 3.
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 31: Introduction to File System
Systems Programming.
Lecture 11 File input/output
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
CSE 333 – Section 3 POSIX I/O Functions.
Operating System Hebrew University Spring 2004
CSE 333 – Section 3 POSIX I/O Functions.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
CSE 333 – Section 3 POSIX I/O Functions.
Text and Binary File Processing
CSE 333 – Section 3 POSIX I/O Functions.
Persistence: File System API
CSE 333 – Section 3 POSIX I/O Functions.
FILE I/O File Descriptors I/O Efficiency File Sharing
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Chapter 12: File-System Implementation CSS503 Systems Programming
CSE 333 – Section 3 POSIX I/O Functions.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

CS 241 Section Week #8 (10/29/09)

Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links

MP5 Overview

You are to create a “deadlock resilient semaphore” library. You should implement six functions. Since we only allow one instance of each resource, you do not need to implement the Banker’s algorithm for deadlock prevention. You may use a resource allocation graph instead. In deadlock detection mode, once a deadlock is detected, you need only send a SIGINT signal. The library does NOT need to worry about how SIGINT is handled. The given test cases are far from complete. You should derive your own test cases.

Files and I/O

Unix File Structure / /bin/ /home/ /home/someuser/ /home/someuser/somefile.txt /usr/ /usr/bin/ /usr/lib/

Internal File Structure A file is just a series of bytes: Thiswnixfile.

Internal File Structure A file is just a series of bytes: Thiswnixfile. Start of FileEnd of FileCurrent Offset

I/O Libraries in C kernel system call handler file I/O terminal I/O pipe I/O network I/O audio I/O open, read, write, close, select, poll,... (direct to kernel I/O) stdio: fopen, fread, fwrite, fclose,... (buffered I/O) User Process Kernel

I/O Libraries in C kernel system call handler file I/O terminal I/O pipe I/O network I/O audio I/O open, read, write, close, select, poll,... (direct to kernel I/O) stdio: fopen, fread, fwrite, fclose,... (buffered I/O) User Process Kernel

Buffered I/O Advantages We’ve previously used: – printf(…) – fprintf(…) Why use buffers? – I/O operations are SLOW! – Every time you write just one byte, you don’t want to have to access your hard drive.

File Descriptors The UNIX operating system uses a file descriptor table to store information about open files: 0stdin… (sof, eof, offset values, etc) 1stdout… 2stderr… 36/usr/home/myfile.txt…

open() int open(const char *pathname, int flags); – returns an int, which is the file descriptor – takes in either a relative or full path name – various flag options allow a file to only be appended to (O_APPEND), opened as write only (O_WRONLY), and more.

open() To open a file for reading: int ifd = open(“./input.txt”, O_RDONLY); To open OR create a file for writing, with given permissions: – int ofd = open(“output.txt”, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);

fopen() FILE *fopen( const char *filename, const char *mode); Rather than an int (file descriptor), fopen returns a FILE stream.

File Permissions In UNIX, the file permissions system is relatively basic. – Each file has a single owner and a single group associated with it. – Each file also has permissions associated with itself for the owner, members of the group the file is in, and for everyone else.

File Permissions These permissions are stored as a three-octal- digit number (000 to 777). 755

File Permissions The most-significant number is the owner’s permission. 755 Owner

File Permissions The middle number is the group’s permission. 755 Group

File Permissions The least-significant number is everyone else’s permission. 755 Other

File Permissions Each octal number is simply three bits: a read bit, a write bit, and an execute bit Read: Write: Execute:

File Permissions Thus: – 755 means “everyone can read and execute by file, but only the owner can write to (edit) my file” – 644 means “everyone can read my file, only the owner can write to my file, and no one can execute it” – 660 means “only members of the file’s group and the file’s owner may read or edit the file; others cannot even read it”

Other C file commands! close(int fd) – Close the file associated with the given file descriptor number. – Can you close stdout? Try it. fclose(FILE *stream) – fclose can close stdout.

Other C file commands! ssize_t read(int fd, void *buf, size_t count); – Read up to count bytes from a file descriptor into the buffer buf. ssize_t write(int fd, void *buf, size_t count); – Write count bytes to a file descriptor from the buffer buf.

Buffered I/O versions… size_t fread(void *ptr, size_t size, size_t count, FILE* stream); – Read up to count*size bytes from a file descriptor into the buffer ptr. size_t fwrite(void *ptr, size_t size, size_t count, FILE* stream); – Write count*size bytes to a file descriptor from the buffer ptr.

Other C file commands! off_t lseek(int fd, off_t offset, int whence); – Seek to a different point in the file. – lseek(fd, 4, SEEK_SET) Seek four bytes after the beginning of the file. – lseek(fd, -4, SEEK_END) Seek four bytes before the end of the file. – lseek(fd, 16, SEEK_CUR) Seek sixteen bytes ahead of the current position.

Other C file commands! int fseek(FILE *stream, long int offset, int origin); – fseek(stream, 4, SEEK_SET) Seek four bytes after the beginning of the file. – fseek(stream, -4, SEEK_END) Seek four bytes before the end of the file. – fseek(stream, 16, SEEK_CUR) Seek sixteen bytes ahead of the current position.

UNIX File Systems

inode: per-file data structure Advantage Efficient for small files Flexible if the size changes Disadvantage File must fit in a single disk partition

UNIX File Systems inode (continued) Storing Large Files

Directories are files too! Directories, like files, have inodes with attributes and pointers to disk blocks

Directories are files too! Directories, like files, have inodes with attributes and pointers to disk blocks Each directory contains the name and i- node for each file in the directory.

Directories are files too! Directories, like files, have inodes with attributes and pointers to disk blocks Each directory contains the name and i- node for each file in the directory.

Directory functions #include Change the directory int chdir(const char *path); Get the current working directory char *getcwd(char *buf, size_t size);

Directory reading functions #include Open the directory DIR *opendir(const char *dirname); Close the directory int closedir(DIR *dirp); Read the directory struct dirent *readdir(DIR *dirp);

What’s in a directory entry? struct dirent Member Fields char d_name[256] Null-terminated file name ino_t d_ino inode number unsigned char d_reclen Length of this record unsigned char d_type Type of file (DT_REG, DT_DIR, DT_FIFO, DT_SOCK, DT_CHR, DT_BLK, DT_UNKNOWN)

Example 1 Use opendir and readdir to print all the filenames in the current directory: #include … DIR *dir; struct dirent *entry; dir = opendir(“.”); while(entry = readdir(dir)){ printf(“%s\n”,entry->d_name); } closedir(dir); Remember to include error checking!!

Example 2 Modify Example 1 to use the member fields of struct dirent to display the inode for each file, as well as whether the file is a directory or a regular file. #include … DIR *dir; struct dirent *entry; dir = opendir(“.”); while(entry = readdir(dir)){ printf(“%s %lu\n”,entry->d_name, entry->d_ino); if(entry->d_type == DT_DIR) printf(“Directory “); else if(entry->d_type == DT_REG) printf(“File ”); } closedir(dir); Remember to include error checking!!

More Directory Functions #include Set the position of next readdir void seekdir(DIR *dir, off_t offset); Set the position back to the start of the directory void rewinddir(DIR *dirp); Get the current location of directory stream off_t telldir (DIR *dir);

Warning! opendir and readdir are NOT thread- safe. DO NOT open two directories at the same time!

How to recursively traverse a directory tree 1.Open the directory ( opendir ) 2.Read each entry ( readdir ) If the file is a directory ( d_type == DT_DIR ), store it (e.g. in an array of strings). 3.Close the directory ( closedir ) 4.Traverse each saved subdirectory, EXCEPT '.' and '..'

File information: stat Use the stat functions to view the file’s inode’s attributes. #include For a file: int stat(const char *restrict path, struct stat *restrict buf); For a link: int lstat(const char *restrict path, struct stat *restrict buf); For a file descriptor: int fstat(int fildes, struct stat *buf);

Useful fields and macros in struct stat stat.st_size File size, in bytes stat.st_mode File type S_ISDIR(stat.st_mode) Is this a directory? User permissions Etc. stat.st_mtime Time of last modification

Example 3 Modify Example 2 to also give file information about each file. How large is each file? Which files are world-readable? Which files have been modified in the last 24 hours? Hint: man 2 stat

#include int main(int argc, char **argv) { DIR *dir; struct dirent *entry; time_t now = time(NULL); if((dir = opendir(".")) == NULL) { perror("Can't open directory"); exit(-1); } while((entry = readdir(dir)) != NULL) { struct stat status; if(entry->d_type == DT_DIR) printf("Directory "); else printf("File "); printf("%s is at inode %d\n", entry->d_name, entry->d_ino); if(stat(entry->d_name, &status)) { perror("\tCan't get stat info"); continue; } printf("\t %d bytes, ", status.st_size); if(status.st_mode & S_IROTH) printf("World-readable, "); if(difftime(now,status.st_mtime)<86400) printf("Recently updated, "); } closedir(dir); }

Links Hard Link Directory Entry e.g. all regular files Symbolic Link Also called a Soft Link A special file that serves as a reference to another file

Link Functions #include To create a new link: int link(const char *oldpath, const char *newpath); Same as ln To remove an entry from the directory: int unlink(const char *path); Same as rm Returns 0 if successful, -1 with errno set if unsuccessful

Hard Link Example Command Line ln /dirA/name1 /dirB/name2 C Code Segments if (link("/dirA/name1", "/dirB/name2") == -1) perror("Failed to make a new link in /dirB");

Hard Link Example (contd) Q: What happens if /dirA/name1 is deleted and recreated?

Hard Link Example (contd) A: /dirA/name1 and /dirB/name2 are now two distinct files.

Symbolic Link Function #include To create a symbolic link: int symlink(const char *oldpath, const char *newpath); Same function as command ln –s Returns 0 if successful, -1 with errno set if unsuccessful

Soft Link Example Command Line ln –s /dirA/name1 /dirB/name2 C Code Segments if (symlink("/dirA/name1", "/dirB/name2") == -1) perror("Failed to create a symbolic link in /dirB");

Soft Link Example (contd) Q: What happens if /dirA/name1 to is deleted and recreated?

Soft Link Example (contd) A: /dirA/name1 has a different inode, but /dir/name2 still links to it.

Link number The link number (the st_nlink field in stat ) tells how many directory entries link to this inode. The link number is: Set to 1 when a file is created Incremented when link is called Decremented when unlink is called The link number appears in the second column of the output of ls –l. Try it! The link number only counts hard links, not soft links.