CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

Similar documents
Inter-Process Communication

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Computer Programming: Skills & Concepts (CP) Files in C

Standard C Library Functions

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Lecture 7: file I/O, more Unix

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Lecture 8: Structs & File I/O

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Standard File Pointers

PROGRAMMAZIONE I A.A. 2017/2018

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

CS240: Programming in C

File Access. FILE * fopen(const char *name, const char * mode);

UNIX input and output

Binghamton University. CS-220 Spring Includes & Streams

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Should you know scanf and printf?

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Input / Output Functions

Welcome! COMP s1. Programming Fundamentals

CAAM 420 Notes Chapter 2: The C Programming Language

CS 261 Fall Mike Lam, Professor. Structs and I/O

2009 S2 COMP File Operations

Goals of this Lecture

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSE 12 Spring 2018 Week One, Lecture Two

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Memory Layout, File I/O. Bryce Boe 2013/06/27 CS24, Summer 2013 C

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

CSE 12 Spring 2016 Week One, Lecture Two

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

628 Lecture Notes Week 4

File I/O. Preprocessor Macros

Introduction to Computing Lecture 03: Basic input / output operations

File and Console I/O. CS449 Spring 2016

Naked C Lecture 6. File Operations and System Calls

Continued from previous lecture

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

CSE 303: Concepts and Tools for Software Development

DATA STRUCTURES USING C

Lecture 9: Potpourri: Call by reference vs call by value Enum / struct / union Advanced Unix

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Process Management! Goals of this Lecture!

CSI 402 Lecture 2 Working with Files (Text and Binary)

EE458 - Embedded Systems Lecture 4 Embedded Devel.

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

The Design of C: A Rational Reconstruction: Part 2

Figure 1 Ring Structures

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

C for C++ Programmers

CMPSC 311- Introduction to Systems Programming Module: Input/Output

C Basics And Concepts Input And Output

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

25.2 Opening and Closing a File

CSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Data File and File Handling

UNIT IV-2. The I/O library functions can be classified into two broad categories:

IO = Input & Output 2

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

SWEN-250 Personal SE. Introduction to C

Library Functions. General Questions

Unix Processes. What is a Process?

CS 326: Operating Systems. Process Execution. Lecture 5

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Comp 204: Computer Systems and Their Implementation. Lecture 18: Devices

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

CSE 230 Intermediate Programming in C and C++ Input/Output and Operating System

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

Lecture 14: more class, C++ streams

Lecture 9: File Processing. Quazi Rahman

Stream Model of I/O. Basic I/O in C

CS 11 Ocaml track: lecture 3

W4118: OS Overview. Junfeng Yang

For instance, we can declare an array of five ints like this: int numbers[5];

File IO and command line input CSE 2451

Play with FILE Structure Yet Another Binary Exploitation Technique. Abstract

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Advanced C Programming Topics

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

Computer Programming Unit v

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Transcription:

CS 220: Introduction to Parallel Computing Input/Output II Lecture 8

Today s Agenda Debugging and I/O Buffering I/O Streams Writing files 2/7/18 CS 220: Parallel Computing 2

Today s Agenda Debugging and I/O Buffering I/O Streams Writing files 2/7/18 CS 220: Parallel Computing 3

printf Debugging One of the simplest ways to debug a program is printing information to the terminal For example, HW1: If your min/max functionality isn t working, then you can print the value before and after the change You can also print the values as you loop through the arrays This is fairly effective, and is used in production code when combined with preprocessor directives 2/7/18 CS 220: Parallel Computing 4

Smarter printf Debugging From stackoverflow (https://stackoverflow.com/questions/5765175/macroto-turn-off-printf-statements): #define LOG(fmt,...) \ do { \ if (DEBUG) fprintf(stderr, fmt, VA_ARGS ); \ } while (0) What is the benefit of this approach? 2/7/18 CS 220: Parallel Computing 5

Demo: Preprocessor Debug 2/7/18 CS 220: Parallel Computing 6

printf Debugging: Problems Let s say we re working on a bug and want to determine what s wrong printf to the rescue! Unfortunately, sometimes printing to the terminal can be misleading The printf() may execute, but the program crashes before any output is displayed This occurs due to Input/Output (I/O) Buffering 2/7/18 CS 220: Parallel Computing 7

I/O Buffering (1/2) Input/output operations are expensive: they have high latency Printing to the terminal is outputting data to the standard output stream Writing to disk or controlling an external hardware device are also I/O operations These devices generally operate on buffers Example: our serial console has a 2-byte buffer; we fill up the buffer before asking it to print the text 2/7/18 CS 220: Parallel Computing 8

I/O Buffering (2/2) You may have used buffered streams in Java to get better performance You might have also noticed what happens when you forget to close a stream Buffered I/O collects multiple I/O operations, combines them, and then executes them Important: matching up with hardware capabilities 2/7/18 CS 220: Parallel Computing 9

Why is I/O Buffering Used? (1/2) Printing to your terminal accesses a virtual device, a pseudoterminal Back in the days of mainframes, everybody had a physical terminal on their desk Accessing a device, virtual or otherwise, requires a system call Privileged operations executed by the OS that interact with the hardware These take time 2/7/18 CS 220: Parallel Computing 10

Why is I/O Buffering Used? (2/2) I/O buffering batches the writes into groups to reduce the amount of system calls Rather than printing: H, e, l, l, o It s faster to just print: Hello Unfortunately, if your program crashes before the buffer is flushed, everything is lost 2/7/18 CS 220: Parallel Computing 11

Sizing our Buffers Large buffers are expensive See: high-end networking hardware If they re too large, we re just wasting space Small buffers may increase latency More I/O operations More round trips More context switching at the OS level 2/7/18 CS 220: Parallel Computing 12

Flushing the Output Stream Sometimes when debugging your program crashes before the buffer gets cleared Data is lost To make the print operation happen *now*, we need to flush the output stream: fflush(stdout); 2/7/18 CS 220: Parallel Computing 13

Why not always call fflush()? Flushing the buffer when it s not full or at inopportune times for the OS incurs more latency Performing the print operation takes several steps, and that takes time We can compare the performance of two C programs, one that flushes I/O and one that does not Example on schedule: flush.c 2/7/18 CS 220: Parallel Computing 14

Demo: fflush 2/7/18 CS 220: Parallel Computing 15

Another Tip I/O buffers are often flushed when a newline (\n) is encountered If you make sure to add a newline character to your print statements, they generally will be flushed to the display and useful for debugging Not a guarantee though the only way to know for sure is to call fflush each time! 2/7/18 CS 220: Parallel Computing 16

Today s Agenda Debugging and I/O Buffering I/O Streams Writing files 2/7/18 CS 220: Parallel Computing 17

Input/Output Streams I mentioned the standard output stream earlier stdout Each program gets allocated three streams by default: stdout (standard output) stderr (standard error) stdin (standard input) These streams have different functions 2/7/18 CS 220: Parallel Computing 18

stdout When you call printf, you are writing to stdout This stream is designed for general program output; for example, if you type ls then the list of files should display on stdout You can pipe stdout to other programs: ls -l / grep 'bin' or redirect to a file: ls -l / > list_of_files.txt 2/7/18 CS 220: Parallel Computing 19

stderr The standard error stream is used for diagnostic information You may recall our LOG macro was printing to stderr This way, program output can still be passed to other programs/files but we ll still see diagnostics printed to the terminal Helps us know when something went wrong Unlike stdout, stderr is not buffered 2/7/18 CS 220: Parallel Computing 20

stdin The final stream, stdin, is how we provide program input (via scanf, for example) This can be entered by the user, or we can pipe input directly into a program: ls -l / grep 'bin' Writes file list to stdout Reads file list from stdin, Writes matching files to stdout 2/7/18 CS 220: Parallel Computing 21

Today s Agenda Debugging and I/O Buffering I/O Streams Writing files 2/7/18 CS 220: Parallel Computing 22

fprintf As we saw earlier, we can print to stderr with fprintf File printf stderr is actually a file in fact, on Unix systems most devices are represented as files Try ls /dev to view the devices on your machine (includes macos) So if we want to write data to a file, just pass it in: fprintf(file, "My name is: %s", "Bob"); 2/7/18 CS 220: Parallel Computing 23

puts & fputs If you don t need formatting functionality, you can use puts to put a string to your terminal fputs is similar, but lets you specify a file: FILE *file = fopen("my_file.txt", "w"); fputs("hi there, file!\n", file); Note: we need to fopen the file with w mode. Why does the file arg come after? Well, it is C 2/7/18 CS 220: Parallel Computing 24

sprintf One last thing we can do: print to a buffer char buffer[100]; sprintf(buffer,"hi, my favorite number is %d!", 42); printf("buffer contents ->%s\n", buffer); 2/7/18 CS 220: Parallel Computing 25