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

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

1. I/O. I/O Unix I/O RIO (robust I/O) package Metadata, sharing, and redirection Standard I/O Conclusions and examples.
Carnegie Mellon 1 System-Level I/O : Introduction to Computer Systems 14 th Lecture, Oct. 12, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
System-Level I/O November 4, 2003 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
System-Level I/O October 8, 2008 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O class14.ppt.
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.
System-Level I/O November 11, 2004 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
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.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
Introduction to Operating Systems File I/O Most of the following slides are adapted from slides of Gregory Kesden and Markus Püschel of Carnegie Mellon.
System-level I/O Alan L. Cox Some slides adapted from CMU slides.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
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”
System-Level I/O Topics Unix I/O Robust reading and writing
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 18, 2012 Instructors: Dave O’Hallaron, Greg Ganger,
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, Oct. 16, 2014 Instructors: Greg Ganger, Greg Kesden,
Files and Directories File types stat functions for file information
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 25, 2013 Instructors: Gregory Kesden.
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
System-Level I/O March 25, 2004 Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O “The.
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”
System-Level I/O.
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?
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”
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
1 System-Level I/O. 2 Outline Unix I/O Reading File Metadata Sharing Files & I/O redirection Robust I/O Standard I/O Suggested Reading –10.1~10.3, 10.5~10.7,
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition System-Level I/O : Introduction to Computer.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Carnegie Mellon /18-243: Introduction to Computer Systems Instructors: Bill Nace and Gregory Kesden (c) All Rights Reserved. All work.
Files Oct. 28, 2008 Topics Mapping file offsets to disk blocks File system buffering and you The directory hierarchy lecture-18.ppt “The course.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Carnegie Mellon 1 System-Level I/O / : Introduction to Computer Systems 15 th Lecture, June 24, 2014 Instructors: Gregory Kesden.
Week 12 - Thursday CS222.
System-Level I/O.
Supplementary Material on Unix System Calls and Standard I/O
System-Level I/O : Introduction to Computer Systems
Introduction to Operating Systems File I/O
CS 105 “Tour of the Black Holes of Computing”
System-Level I/O Topics Unix I/O Robust reading and writing
Programming in C Input / Output.
File I/O We are used to reading from and writing to the terminal:
Operating System Hebrew University Spring 2004
System-Level I/O October 9, 2008
“The course that gives CMU its Zip!”
File Structure Related system calls
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Reference: Advanced Programming in the UNIX Environment, Third Edition, W. Richard Stevens and Stephen A. Rago, Addison-Wesley Professional Computing Series,
CS 201 The Unix System Interface
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
IPC Prof. Ikjun Yeom TA – Hoyoun
System-Level I/O CSCI 380: Operating Systems
Chien-Chung Shen CIS, UD
CS 105 “Tour of the Black Holes of Computing”
Instructors: David Trammell
Presentation transcript:

File I/O (1) Prof. Ikjun Yeom TA – Hoyoun Lee(hy2930hy@naver.com) TA – Bedeuro Kim(secbdrkim@naver.com) Computer Network Laboratory Sungkyunkwan University http://comnet.skku.edu

Contents File in Unix System calls for File I/O

File Metadata Data about data, in this case file data. Maintained by kernel, accessed by users with the stat and fstat functions. /* Metadata returned by the stat and fstat functions */ struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ mode_t st_mode; /* protection and file type */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device type (if inode device) */ off_t st_size; /* total size, in bytes */ unsigned long st_blksize; /* blocksize for filesystem I/O */ unsigned long st_blocks; /* number of blocks allocated */ time_t st_atime; /* time of last file access */ time_t st_mtime; /* time of last file modification */ time_t st_ctime; /* time of last inode change */ }; /* statbuf.h included by sys/stat.h */

Accessing File Metadata /* statcheck.c - Querying and manipulating a file’s meta data */ int main (int argc, char **argv) { struct stat st; char *type, *readok; stat(argv[1], &st); if (S_ISREG(st.st_mode)) /* file type */ type = "regular"; else if (S_ISDIR(st.st_mode)) type = "directory"; else type = "other"; if ((st.st_mode & S_IRUSR)) /* OK to read? */ readok = "yes"; readok = "no"; printf("type: %s, read: %s\n", type, readok); exit(0); } bass> ./statcheck statcheck.c type: regular, read: yes bass> chmod 000 statcheck.c type: regular, read: no

Standard I/O Functions The C standard library (libc.a) contains a collection of higher-level standard I/O functions Documented in Appendix B of K&R. Examples of standard I/O functions: Opening and closing files (fopen and fclose) Reading and writing bytes (fread and fwrite) Reading and writing text lines (fgets and fputs) Formatted reading and writing (fscanf and fprintf)

Standard I/O Streams Standard I/O models open files as streams Abstraction for a file descriptor and a buffer in memory C programs begin life with three open streams (defined in stdio.h) stdin (standard input) stdout (standard output) stderr (standard error) #include <stdio.h> extern FILE *stdin; /* standard input (descriptor 0) */ extern FILE *stdout; /* standard output (descriptor 1) */ extern FILE *stderr; /* standard error (descriptor 2) */ int main() { fprintf(stdout, "Hello, world\n"); }

Buffering in Standard I/O Standard I/O functions use buffered I/O printf("h"); printf("e"); printf("l"); printf("l"); printf("o"); buf printf("\n"); h e l o \n . fflush(stdout); write(1, buf += 6, 6);

Unix I/O vs. Standard I/O Standard I/O are implemented using low-level Unix I/O. Which ones should you use in your programs? fopen fdopen fread fwrite fscanf fprintf sscanf sprint fgets fputs fflush fseek fclose rio_readn rio_written rio_readinitb rio_readlineb rio_readnb C application program Standard I/O functions RIO functions Unix I/O functions (accessed via system calls) open read write lseek stat close

Pros/Cons of Unix I/O Pros Cons The most general and lowest overhead form of I/O. All other I/O packages are implemented on top of Unix I/O functions. Unix I/O provides functions for accessing file metadata. Cons System call overheads for small-sized I/O. Dealing with short counts is tricky and error prone. Efficient reading of text lines requires some form of buffering, also tricky and error prone. These issues are addressed by the standard I/O.

Pros/Cons of Standard I/O Buffuring increases efficiency by decreasing the number of read() and write() system calls. Shout counts are handled automatically. Cons Provides no function for accessing file metadata. Standard I/O is not appropriate for input and output on network sockets. But there is a way using fdopen()

Summary Unix file I/O When to use standard I/O? open(), read(), write(), close(), ... A uniform way to access files, I/O devices, network sockets, kernel data structures, etc. When to use standard I/O? When working with disk or terminal files. When to use raw Unix I/O When you need to fetch file metadata. When you read or write network sockets or pipes. In rare cases when you need absolute highest performance.

Remind 6 System calls open() close() read() write() lseek() stat() / fstat()

Exercise Lab exercise #1: Let’s make xcat, xcp utilities using “buffering”. xcat prints a file on the standard output. xcp copies contents of a file into a new file. Basically, executing xcat and xcp will be same as executing cat and cp without any options, respectively. Your job is to make xcat and xcp by using system calls provided by Linux.

Malloc / Free Both need <stdlib.h> Malloc Free struct my_struct *p = (struct my_struct *)malloc(sizeof(my_struct)); Free free(p)

Linked List struct my_struct s1 struct my_struct s2 Data next Data NULL head struct my_struct head

Exercise Lab exercise #2: Let’s make xsort utility using linked list cat in.txt | ./xsort > out.txt It should work same as “sort” Your job is to make xsort by using system calls provided by Linux and <stdlib>.

Test of xsort We have genesis.txt diff –bsq sort.out xsort.out