Standard I/O Library Implementation

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
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.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
1 System Calls, Stdio, and Course Wrap-Up COS 217 Professor Jennifer Rexford.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
1 System Calls and Standard I/O Professor Jennifer Rexford
CSE 451 Section 4 Project 2 Design Considerations.
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.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
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.
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.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
UNIX Files File organization and a few primitives.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
File Systems cs550 Operating Systems David Monismith.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
Files A collection of related data treated as a unit. Two types Text
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.
Advanced Programming in the UNIX Environment Hop Lee.
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.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Error handling I/O Man pages
Introduction to Operating Systems
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 11 File input/output
Chapter 7 Text Input/Output Objectives
File I/O.
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Introduction to Operating Systems
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSE 333 – Section 3 POSIX I/O Functions.
FILE HANDLING IN C.
Programming Assignment # 2 – Supplementary Discussion
Text and Binary File Processing
File Input and Output.
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
I/O and Process Management
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
Module 12 Input and Output
File I/O & UNIX System Interface
Chapter 12: File-System Implementation CSS503 Systems Programming
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Standard I/O Library Implementation COS 217

Goals of Today’s Lecture Challenge: generic I/O support for C programs Provide C programs with functions for input and output Stream concept, line-by-line input, formatted output, ... Implement across a variety of host OSes Solution: abstraction, and division of functionality Standard I/O ANSI C standard model and I/O functions for files Specific C implementation for each different system Additional features (e.g., buffered I/O and safe writing) Low-level I/O System calls that invoke OS services UNIX examples: open, close, read, write, seek

Stream Abstraction Any source of input or destination for output E.g., keyboard as input, and screen as output E.g., files on disk or CD, network ports, printer port, … Accessed in C programs through file pointers E.g., FILE *fp1, *fp2; E.g., fp1 = fopen(“myfile.txt”, “r”); Three streams provided by stdio.h Streams stdin, stdout, and stderr Typically map to keyboard, screen, and screen Can redirect to correspond to other streams E.g., stdin can be the output of another program E.g., stdout can be the input to another program

Example Stdio Functions on Streams FILE *fopen(“myfile.txt”, “r”) Open the named file and return a stream Includes a mode, such as “r” for read or “w” for write int fclose(fp1) Close the stream Flush any unwritten data, and discard any unread input int fprintf(fp1, “Number: %d\n”, i) Convert and write output to stream in specified format Note: printf(…) is just fprintf(stdout, …) int fscanf(fp1, “FooBar: %d”, &i) Read from stream in format and assign converted values Note: scanf(…) is just fscanf(stdint, …)

Sequential Access to a Stream Each stream has an associated file position Starting at beginning of file (if opened to read or write) Or, starting at end of file (if opened to append) Read/write operations advance the file position Allows sequencing through the file in sequential manner Support for random access to the stream Functions to learn current position and seek to new one file file

Layers of Abstraction User process int fd hierarchical file system Appl Prog User process FILE * stream Stdio Library int fd File System hierarchical file system Operating System Storage variable-length segments Driver disk blocks Disk

System Calls Method by which user processes invoke operating system services: “protected” function call Unix has ~150 system calls; see man 2 intro /usr/include/syscall.h Appl Prog fopen,fclose, printf, fgetc, getchar,… Stdio Library user open, close, read, write, seek OS File System

System Calls Processor modes System calls User mode: can execute normal instructions and access only user memory Supervisor mode: can also execute privileged instructions and access all of memory (e.g., devices) System calls User cannot execute privileged instructions Users must ask OS to execute them System calls are often implemented using traps OS gains control through trap, switches to supervisor model, performs service, switches back to user mode, and gives control back to user

System-call Interface = ADTs operations File input/output open, close, read, write, lseek, dup Process control fork, exit, wait, kill, exec, ... Interprocess communication pipe, socket ...

Details of FILE in stdio.h (K&R 8.5) #define OPEN_MAX 20 /* max files open at once */ typedef struct _iobuf { int cnt; /* num chars left in buffer */ char *ptr; /* ptr to next char in buffer */ char *base; /* beginning of buffer */ int flag; /* open mode flags, etc. */ char fd; /* file descriptor */ } FILE; extern FILE _iob[OPEN_MAX]; #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2])

Main UNIX System Calls for Files Open: int open(char *pathname, int flags, mode_t mode); Open a the file pathname and return a file descriptor Creat: int creat(char *pathname, mode_t mode); Create a new file and assign a file descriptor Close: int close(int fd); Close a file descriptor fd Read: int read(int fd, void *buf, int count); Read up to count bytes from fd, into the buffer at buf Write: int write(int fd, void *buf, int count); Writes up to count bytes into fd, from the buffer at buf

Example: UNIX open() System Call Converts a path name into a file descriptor int open(const char *pathname, int flags, mode_t mode); Similar to fopen() in stdio Uses a pathname to identify the file Allows opening for reading, writing, etc Different from fopen() in stdio Returns an integer descriptor rather than a FILE pointer Specifies reading, writing, etc. through bit flags E.g., O_RDONLY, O_WRONLY, O_RDWR Specifies permissions to set if the file must be created No need to worry about this (see K&R 8.3 for details)

Implementing fopen()in stdio If mode is invalid, return NULL E.g,. mode of access needs to be ‘r’, ‘w’, or ‘a’ Search for an available slot in the IOB array Stop when unused slot is found, or return NULL if none Open or create the file, based on the mode Write (‘w’): create file with default permissions Read (‘r’): open the file as read-only Append (‘a’): open or create file, and seek to the end Assign fields in IOB structure, and return pointer Cnt of zero, base of NULL, flags based on mode, etc. See K&R Section 8.5 for the full details

Simple Implementation of getchar() int getchar(void) { static char c; if (read(0, &c, 1) == 1) return c; else return EOF; } Read one character from stdin File descriptor 0 is stdin &c points to the buffer 1 is the number of bytes to read Read returns the number of bytes read In this case, 1 byte means success

Making getchar() More Efficient Problem: poor performance reading byte at a time Read system call is accessing the device (e.g., a disk) Reading a single byte from a disk is very time consuming Insight: better to read and write in larger chunks Buffered I/O Read a larger chunk of data from disk into a buffer And dole individual bytes to user process as needed Discard the buffer contents when the stream is closed Similarly, for writing, write individual bytes to a buffer And write to disk when full, or when stream is closed Known as “flushing” the buffer

Better getchar() with Buffered I/O Solution: read a chunk and dole out as needed int getchar(void) { static char buf[1024]; static char *p; static int n = 0; if (n--) return *p++; n = read(0, buf, sizeof(buf)); if (n <= 0) return EOF; p = buf; return getchar(); }

Funny Thing About Buffered I/O int main() { printf(“Step 1\n”); sleep(10); printf(“Step2\n”); return(0); } Try running “a.out > out.txt &” and then “more out.txt” To run a.out in the background, outputting to out.txt And then to see the contents on out.txt Neither line appears till ten seconds have elapsed Because the output is being buffered Could add a fflush(stdout) to get the output flushed

Implementing getc() in stdio #define getc(p) \ (--(p)->_cnt >= 0 ? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) #define getchar() getc(stdin) Decrement the count (cnt) of remaining characters If any characters are left in the buffer Return the character, and increment the pointer to the next character Else if no characters are left Replenish the buffer, re-initialize the structure, and return character

So, Why is getc() a Macro? Invented in ~1975, when Computers had slow function-call instructions Compilers couldn’t inline-expand very well It’s not 1975 any more Moral: don’t invent new macros, use functions

Challenges of Writing Write system call int write(int fd, void *buf, int count); Writes up to count bytes into fd, from the buffer at buf Problem: might not write everything Can return a number less than count E.g., if the file system ran out of space Solution: safe_write Try again to write the remaining bytes Produce an error if it impossible to write more

Safe-Write Code p p q int safe_write(int fd, char *buf, int nbytes) { char *p = buf; char *q = buf + nbytes; while (p < q) { if ((n = write(fd, p, (q-p)*sizeof(char))) > 0) p += n/sizeof(char); else perror(“safe_write:”); } return nbytes; p p q

Conclusion Standard I/O library provides simple abstractions Stream as a source or destination of data Functions for manipulating files and strings Standard I/O library builds on the OS services Calls OS-specific system calls for low-level I/O Adds features such as buffered I/O and safe writing Powerful examples of abstraction User programs can interact with streams at a high level Standard I/O library deals with some more gory details Only the OS deals with the device-specific details