1 Standard I/O FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write- only)

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

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Lecture 2: OS Programming Interface T.Yang, CS
1 Standard I/O FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write- only)
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
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?
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
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
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.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
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 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
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.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
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.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Shell (Addendum). 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.
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 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
Manipulating Files Refresher. The touch Command touch is used to create a new, empty file. If the file already exists, touch updates the time and date.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
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.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
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.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
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.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
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.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 4: Managing File System State Dr. Xiao Qin Auburn University.
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.
By C. Shing ITEC Dept Radford University
Week 3 Redirection, Pipes, and Background
Operating Systems Moti Geva
CIRC Summer School 2017 Baowei Liu
File redirection ls > out
Objective Explain basic fuzzing with concrete coding example
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
2/25/08 Frans Kaashoek MIT OS abstractions 2/25/08 Frans Kaashoek MIT
Andy Wang Operating Systems COP 4610 / CGS 5765
Programming Assignment # 2 – Supplementary Discussion
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
IPC Prof. Ikjun Yeom TA – Hoyoun
Weeks 9-10 IO System Calls Standard IO (stdin, stdout) and Pipes
Lecture 4 Redirecting standard I/O & Pipes
Malware and Software Vulnerability Analysis Fuzzing Test Example Cliff Zou University of Central Florida.
FILE I/O File Descriptors I/O Efficiency File Sharing
Block I/O fread and fwrite functions are the most efficient way to read or write large amounts of data. fread() – reads a specified number of bytes from.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Presented by, Mr. Satish Pise
Presentation transcript:

1 Standard I/O FILE * stdin: standard input (read-only) FILE * stdout: standard output (write-only) FILE * stderr: standard error output (write- only) File descriptor: stdin: 0 stdout: 1 stderr: 2

2 Standard I/O Example #include int main() { char buf[1024]; int offset, read_num=0, write_num=0; read_num = read(0, buf, sizeof(buf)); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); //write_num = fwrite(buf, 1, read_num, stdout); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; } read(): return # of bytes read, 0 indicates end of file write(): return # of bytes write fwrite(): calls write()

3 Shell Redirection Replace stdin/stdout/stderr by another file Usage:./a.out < input.txt./a.out > output.txt./a.out output.txt Question: how to achieve this?

4 Redirect Standard Input (I) #include int main() { char buf[1024]; int offset, read_num=0, write_num=0; close(0); open(“input.txt", O_RDONLY); read_num = read(0, buf, sizeof(buf)); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; }

5 Redirect Standard Input (II) #include int main() { char buf[1024]; int offset, read_num=0, write_num=0; int input_fd = open("tmp.txt", O_RDONLY); dup2(input_fd, 0); read_num = read(0, buf, sizeof(buf)); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; }

6 Redirect Standard Output #include int main() { char buf[1024]; int offset, read_num=0, write_num=0; int input_fd = open("tmp.txt", O_RDONLY); dup2(input_fd, 0); read_num = read(0, buf, sizeof(buf)); close(1); open("temp.txt", O_WRONLY | O_TRUNC | O_CREAT,0666); while(read_num > 0){ offset = 0; do { write_num = write(1, buf+offset, read_num); read_num -= write_num; offset += write_num; } while(read_num > 0); read_num = read(0, buf, sizeof(buf)); } return 0; }