CSCE 510 - Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.

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

Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Kernel File Interface operating systems (or programming I/O in Unix)
Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O   4. Standard I/O Library 5. Files and Directories 6. System Data.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
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.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
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”
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.
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.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 3: Processes.
UNIX Files File organization and a few primitives.
UNIX UTILITY PACKAGE Shell Simulator Added Shell Commands File System Explorer ( LNC ) System Information Process Tree Viewer.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Files and Directories File types stat functions for file information
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?
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”
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Process Management Azzam Mourad COEN 346.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
CSCE Systems Programming Lecture 02 - System Calls CSCE 510 Jan 16, 2013.
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,
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Files Oct. 28, 2008 Topics Mapping file offsets to disk blocks File system buffering and you The directory hierarchy lecture-18.ppt “The course.
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.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Advanced Programming in the UNIX Environment Hop Lee.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
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.
Week 12 - Thursday CS222.
Error handling I/O Man pages
Lecture 11 File input/output
Introduction to Operating Systems File I/O
System-Level I/O Topics Unix I/O Robust reading and writing
Operating System Hebrew University Spring 2004
“The course that gives CMU its Zip!”
File Structure Related system calls
“The course that gives CMU its Zip!”
Text and Binary File Processing
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Persistence: File System API
Advanced UNIX progamming
System-Level I/O CSCI 380: Operating Systems
Chien-Chung Shen CIS, UD
Module 12 Input and Output
CS 105 “Tour of the Black Holes of Computing”
File I/O & UNIX System Interface
Presentation transcript:

CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013

Overview Last Time  Agile –  Make – implementation  Process creation –  Fork  execve  Systems limits – not really Readings for today  Stdio  Chapter 13 – I/O buffering  Shells:  Basics: read command into doubly linked list  Shell variables, set command  background,  Substitutions: variable substitutions, pseudo filename completion, history substitution,  Simple I/O redirection  Shell version 2: signals, pipes, command substitution

Fork revisited - CSCE Slide I/O Buffering  rv=fork()  Properties inherited (shorter list last time)  Open files, uids, cwd, root directory, umask,  signal mask and table,  environment, shared,memory  Properties not inherited  Return value of fork, pid ppid, times,  file locks,  pending signals

vfork - CSCE Slide I/O Buffering 

Exec family of functions - CSCE Slide I/O Buffering int execle( const char *pathname, const char *arg,... /*, (char *) NULL, char *const envp[] */ ); int execlp( const char *filename, const char *arg,... /*, (char *) NULL */); int execvp( const char *filename, char *const argv[]); int execv( const char *pathname, char *const argv[]); int execl( const char *pathname, const char *arg,...

Chapter 12. System and Process Info. - CSCE Slide I/O Buffering  12.1 The /proc File System  Obtaining Information About a Process: /proc/PID  System Information Under /proc  Accessing /proc Files  12.2 System Identification: uname()  ps -e

PS EXAMPLES - CSCE To see every process on the system using standard syntax: ps -e ps -ef ps -eF ps -ely To see every process using BSD syntax: ps ax ps axu To print a process tree: ps -ejH ps axjf To get info about threads: ps -eLf ps axms To get security info: ps –eo euser, ruser, suser, fuser, f, comm, label ps axZ ps –eM To see every process running as root (real & effective ID) in user format: ps -U root -u root u Print only the process IDs of syslogd: ps -C syslogd -o pid=

Figure 12-1 /proc hierarchy - CSCE Slide I/O Buffering

- CSCE Slide I/O Buffering  hermes> cd sysinfo  hermes> ls Makefile procfs_pidmax.c procfs_user_exe.c t_uname.c procfs_pidmax procfs_user_exe t_uname

FILE I/O Buffering - CSCE Slide I/O Buffering  13.1 Kernel Buffering of File I/O: The Buffer Cache  13.2 Buffering in the stdio Library  13.3 Controlling Kernel Buffering of File I/O  13.4 Summary of I/O Buffering  13.5 Giving the Kernel Hints About I/O Patterns: posix_fadvise()  13.6 Bypassing the Buffer Cache: Direct I/O  13.7 Mixing Library Functions and System Calls for File I/O  13.8 Summary  13.9 Exercises

Stat structure revisted - CSCE Slide I/O Buffering struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ 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 ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };

Stdio - library - CSCE Slide I/O Buffering  IO/syscalls  FILE structure  FILE * functions  fopen(path, “r | w | …” [, mode] )  int fscanf(FILE *stream, const char *format,...);  int fprintf(FILE *stream, const char *format,...);  Scanf, sscanf, printf, sprintf versions  fclose(), fread, fwrite,  fgetc, fputc, fgets, fputs, fflush  Fdopen, freopen  Ungetc  mktemp

FILE structure - CSCE Slide I/O Buffering Use to be in /usr/include/stdio.h int _fileno; int _blksize; int _flags2; int _cnt; _IO_off_t _old_offset; /* This used to be _offset but…*/ Now, cleverly /usr/include/stdio.h contains typedef struct _IO_FILE FILE;

FILE structure - CSCE Slide I/O Buffering struct _IO_FILE { int _flags; /* High-order word is _IO_MAGIC; rest is flags. */ #define _IO_file_flags _flags /* The following pointers correspond to the C++ streambuf protocol. */ /* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */ char* _IO_read_ptr; /* Current read pointer */ char* _IO_read_end; /* End of get area. */ char* _IO_read_base; /*Start of putback+get area. */ char* _IO_write_base; /* Start of put area. */ char* _IO_write_ptr; /* Current put pointer. */ char* _IO_write_end; /* End of put area. */ char* _IO_buf_base; /* Start of reserve area. */ char* _IO_buf_end; /* End of reserve area. */ /* The following fields are used to support backing up and undo. */ char *_IO_save_base; /* Pointer to start of non- current get area. */ char *_IO_backup_base; /* Pointer to first valid character of backup area */ char *_IO_save_end; /* Pointer to end of non- current get area. */

FILE structure - CSCE Slide I/O Buffering struct _IO_FILE *_chain; int _fileno; #if 0 int _blksize; #else int _flags2; #endif struct _IO_marker *_markers; _IO_off_t _old_offset; /* This used … #define __HAVE_COLUMN /* temporary */ /* 1+column number of pbase(); 0 is unknown. */ unsigned short _cur_column; signed char _vtable_offset; char _shortbuf[1]; /* char* _save_gptr; char* _save_egptr; */ _IO_lock_t *_lock; #ifdef _IO_USE_OLD_IO_FILE };

Stdin, stdout, stderr - CSCE  extern FILE *stdin;  extern FILE *stdout;  extern FILE *stderr;

Examples/FILE.c - CSCE Slide I/O Buffering  gcc –E FILE.c > FILE.cpp_out  getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.  What I expected!  #define getchar() getc(stdin)  #define getc(p) ((--(p)->_cnt _ptr++)  NOTE!!! Rescanning of macro expansions

Getc macro - CSCE Slide I/O Buffering #define getc(p) ((--(p)->_cnt _ptr++) FILE *p;

Which is faster read() or getc()? - CSCE Slide I/O Buffering  readoneAtTime.c  getc_version.c  getchar_version.i  getchar and getc are supposed to be macros!!!

Table Time required to copy - CSCE Slide I/O Buffering  Table Time required to duplicate a file of 100 million bytes  BUF_SIZE TimeCPU-totalUserSys     …    

TLPI/filebuff/write_bytes.c - CSCE Slide I/O Buffering  Table 13-2

- CSCE Slide I/O Buffering SETBUF(3) Linux Programmer's Manual SETBUF(3) NAME setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations SYNOPSIS #include void setbuf(FILE *stream, char *buf); void setbuffer(FILE *stream, char *buf, size_t size); void setlinebuf(FILE *stream); int setvbuf(FILE *stream, char *buf, int mode, size_t size); DESCRIPTION The three types of buffering available are unbuffered, block buffered, and line buffered.

Flags - CSCE Slide I/O Buffering  _IONBF unbuffered  _IOLBF line buffered  _IOFBF fully buffered  setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ); 

Flushing a stdio buffer, etc - CSCE Slide I/O Buffering  fflush()  Fsync()

Fig 13-1 Summary of I/O buffering - CSCE Slide I/O Buffering

Shell Implementation - CSCE Slide I/O Buffering  What does the shell do?

Shell Substitutions - CSCE Slide I/O Buffering

Shell variables / Environment - CSCE Slide I/O Buffering

History Mechanism - CSCE Slide I/O Buffering

- CSCE Slide I/O Buffering