Recitation 9: Error Handling, I/O, Man Andrew Faulring 15213 Section A 4 November 2002.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
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.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
CSE 451 Section 4 Project 2 Design Considerations.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
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.
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.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
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.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
File IO and command line input CSE 2451 Rong Shi.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
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.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
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.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
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.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
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 -
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.
Error handling I/O Man pages
CS/COE 0449 (term 2174) Jarrett Billingsley
Robust I/O package Chapter 11 practice problems
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
CSC215 Lecture Input and Output.
Programming in C Input / Output.
Lecture 5: Process Creation
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
LINUX System Programming with Processes (additional)
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Recitation 9 Greg Reshko
Process Programming Interface
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
Programming in C Input / Output.
Module 12 Input and Output
Standard I/O Library Implementation
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Intro to the Shell with Fork, Exec, Wait
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002

Logistics Office hours –NSH 2504 –Permanently moving to Tuesday 2–3 Exam 2 on Tuesday Lab 6 (Malloc) due on Tuesday Lab 7 (Proxy) due on Thursday

Logistics Lab 6 (Malloc) –Due Tuesday, 19 November –Coverage Lecture on Tuesday –10.9 Dynamic Memory Allocation Next Monday’s recitation Exam 2 –Tuesday, 12 November –Review session next Monday evening

Coverage of Exam 2 Similar to last year’s exam 2 –Performance optimization –Cache simulation –Cache miss rate –Cache miss analysis –Processes –Signals –Virtual memory Come to the special recitation –Mon. 11/11 evening –Location TBD

Today’s Plan Error handling I/O –Unix I/O –Standard I/O Linux man pages

Error Handling Should always check return code of system calls –Not only for 5 points in your lab! –There are subtle ways that things can go wrong –Use the status info kernel provides us Approach in this class: Wrappers Different error handling styles –Unix-Style –Posix-Style –DNS-Style

Unix-Style Error Handling Special return value when encounter error (always –1) Set global variable errno to an error code –Indicates the cause of the error Use strerror function for text description of errno void unix_error(char *msg) { fprintf(stderr, “%s: %s\n“,msg, strerror(errno)); exit(0); } … if ((pid = wait(NULL)) < 0) unix_error(“Error in wait”);

Posix-Style Error Handling Return value only indicate success (0) or failure (nonzero) Useful results returned in function arguments void posix_error(int code, char *msg) { fprintf(stderr, “%s: %s\n“, msg, strerror(code)); exit(0); } … if ((retcode = pthread_create(…)) != 0) posix_error(retcode, “Error in pthread”);

DNS-Style Error Handling Return a NULL pointer on failure Set the global h_errno variable void dns_error(char *msg) { fprintf(stderr, “%s: DNS error %d\n“, msg, h_errno); exit(0); } … if ((p = gethostbyname(name)) == NULL) dns_error(“Error in gethostbyname”);

Appendix B: csapp.h and csapp.c Unix-Style, for kill function Behaves exactly like the base function if no error Prints informative message and terminates the process void Kill (pid_t pid, int signum) { int rc; if((rc = kill(pid, signum)) <0) unix_error(“Kill error”); } Example: Wrappers

Not All Errors Are Fatal Wrappers are not always the correct path. –Treats all errors as fatal errors –Terminate the program with exit() –Sometimes an error is not fatal void sigchld_handler(int signum) { pid_t pid; while((pid = waitpid(-1, NULL, 0)) > 0) printf(“Reaped %d\n”, (int)pid); if(errno != ECHILD) unix_error(“waitpid error”); }

I/O Full coverage in Lecture 24 on 14 November. Chapter 11 in textbook. But folks were having some issues with the Shell lab And these issues will pop up with the Malloc lab …

Unix I/O Why need I/O? –copy data between main memory and external devices All devices modeled as files in Unix –Disk drives, terminals, networks –A Unix file is a sequence of bytes –Input/Output performed by reading and writing files

Unix I/O Why Kernel I/O –System level I/O functions provided by the kernel –Understand system concepts (process, VM, etc.) –When impossible/inappropriate to use standard library I/O –Reading and writing raw byte arrays How kernel I/O works –Apps only keep track of file descriptor returned from kernel –Kernel maintains all file information

Functions – Open int open(const char *pathname, int flags); –Return value is a small integer – file descriptor Special file descriptors –Defined in –Default open with each shell-created process –Standard Input (descriptor = 0) –Standard Output (descriptor = 1) –Standard Error (descriptor = 2)

Functions – Read and Write ssize_t read(int fd, void *buf, size_t count); –Copy count >0 bytes from a file (fd) to memory (buf) –From current position k (maintained by kernel) –Trigger EOF when k is greater than file size –No “EOF character” ssize_t write(int fd, void *buf, size_t count); –Copy count >0 bytes from memory (buf) to a file (fd)

Functions – Close int close(int fd); –Kernel frees data structures created by file open (if any) –Restores file descriptor to a pool of available descriptors –What if a process terminates? –Kernel closes all open files and free memory (for this proc)

Standard I/O Higher level I/O functions –fopen,fclose, fread,fwrite, fgets,fputs, –scanf and printf : formated I/O –Eventually calls the kernel I/O routines Models an open file as a stream –A pointer to FILE –Abstraction for file descriptor and for a stream buffer Why use stream buffer? –Reduce expensive Unix system calls!

File Streams C Library equivalent of file descriptors –FILE* –stdin, stdout, stderr –FILE *fopen (const char *path, const char *mode); –FILE *fdopen (int fildes, const char *mode); fprintf, fscanf, … –Take an extra first argument: FILE* int printf(const char *format,...); int fprintf(FILE *stream, const char *format,...); –printf(arg1,arg2,…) = fprintf(stdout,arg1,arg2,…) –scanf(arg1,arg2,…) = fscanf(stdin,arg1,arg2,…)

Example: buffered_io.c #include int main(void) { printf("1"); printf("5"); printf("2"); printf("1"); printf("3"); return 0; }

Use strace to check strace –Runs and prints out info about all the system calls Let’s run strace on buffered_io unix> strace./buffered_io … write(1, "15213", ) = 5 …

Flushing a File Stream Force the C library to write any buffered data int fflush(FILE *stream); fflush(stdout);

Example: buffered_io_flush.c #include int main(void) { printf("1"); printf("5"); fflush(stdout); printf("2"); printf("1"); printf("3"); return 0; }

strace, revisited Let’s run strace buffered_io_flush unix> strace./buffered_io_flush … write(1, "15", 215) = 2 write(1, "213", 3213) = 3 …

Man pages unix> man kill KILL(1) Linux Programmer's Manual KILL(1) NAME kill - terminate a process SYNOPSIS kill [ -s signal | -p ] [ -a ] pid... kill -l [ signal ] DESCRIPTION kill sends the specified signal to the specified process. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, if may be necessary to use the KILL (9) signal, since this signal cannot be caught. Most modern shells have a builtin kill function.

Man page Sections Section 1: Commands –“Stuff that you could run from a Unix prompt” –cp(1), bash(1), kill(1), … Section 2: System Calls –“Talking with the Kernel” –kill(2), open(2), … Section 3: Library Calls –“The C Library” –printf(3), scanf(3), fflush(3), …

Sections To specify a man section –man n … –man 2 kill

Summary Error handling –You should always check error codes –Wrappers can help in most cases I/O –Be sure to call fflush with debugging code Man pages –Information grouped into sections