Error handling I/O Man pages

Slides:



Advertisements
Similar presentations
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Advertisements

CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
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?
1 System Calls and Standard I/O Professor Jennifer Rexford
CSE 451 Section 4 Project 2 Design Considerations.
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.
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.
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.
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()
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.
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.
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
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.
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.
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.
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: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
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.
CS/COE 0449 (term 2174) Jarrett Billingsley
Operating Systems Moti Geva
Robust I/O package Chapter 11 practice problems
Precept 14 : Ish dup() & Signal Handling
Using Processes.
CSC215 Lecture Input and Output.
Programming in C Input / Output.
CSE 333 – Section 3 POSIX I/O Functions.
Lecture 5: Process Creation
Programming in C Input / Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
File I/O We are used to reading from and writing to the terminal:
LINUX System Programming with Processes (additional)
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
Recitation 8 Processes, Signals, Tshlab
Input/Output and the Operating Systems
Recitation 9 Greg Reshko
CSE 333 – Section 3 POSIX I/O Functions.
Recitation 9: Processes, Signals, TSHLab
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Process Programming Interface
I/O and Process Management
CSE 333 – Section 3 POSIX I/O Functions.
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
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O.
Intro to the Shell with Fork, Exec, Wait
Recitation 9: Processes, Signals, TSHLab
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Error handling I/O Man pages 15213 Recitation Section C Shimin Chen Nov. 4, 2002 Outline Error handling I/O Man pages

Important Dates Lab 6 (Malloc) Exam 2 Due Tuesday, November 19 Next Monday’s recitation Exam 2 Tuesday, November 12 Review session next Monday evening

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 Appendix B

Different Error Handling Styles Unix-Style e.g. kill, signal, fork, etc. Posix-Style e.g. pthread_create DNS-Style e.g. gethostbyname

Unix-Style Error Handling Special return value when encounter error (always –1) Global variable errno set to an error code Use strerror function for text description of errno Or use perror 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”);

Unix-Style Error Handling Cont’d if ((pid = wait(NULL)) < 0) { perror(“Error in wait”); exit (0); }

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”);

Example: Wrappers Appendix B: csapp.h and csapp.c void Kill (pid_t pid, int signum) { int rc; if((rc = kill(pid, signum)) <0) unix_error(“Kill error”); } 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

Handle Errors Gracefully The wrappers shown above calls exit() In many situations, we want to handle errors more gracefully. For example: web server, etc. void sigchld_handler(int signum) { pid_t pid; while((pid = waitpid(…)) > 0) printf(“Reaped %d\n”, (int)pid); if(errno != ECHILD) unix_error(“waitpid error”); }

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

Standard C Library I/O: printf, fopen, fclose, fread, fwrite, etc. Unix I/O & Standard I/O Standard C Library I/O: printf, fopen, fclose, fread, fwrite, etc. Unix I/O: open, close, read, write OS Kernel

Unix I/O System calls Functions Reading and writing raw byte arrays File descriptors (small integers) Functions int open(const char *pathname, int flags); ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); int close(int fd); Reading and writing raw byte arrays. No formatting.

Special File Descriptors 0: standard in 1: standard out 2: standard error

Standard I/O The C library I/O routines printf, scanf Formatting printf(“%x\n”, 0x15213); Buffering Eventually will call the Unix I/O routines (syscalls) Buffers input to minimize the number of syscalls

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

strace strace <program> Let’s run strace on buffered_io Runs <program> and prints out info about all the system calls Let’s run strace on buffered_io Notice that we had five calls to printf, but only one write. unix> strace ./buffered_io … write(1, "15213", 515213) = 5 …

File Streams C Library equivalent of file descriptors stdin, stdout, stderr FILE *fopen (const char *path, 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,…)

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

Example: buffered_io_flush.c #include <stdio.h> 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 …

Contains detailed description of Man Pages Contains detailed description of Commands System calls C library functions etc.

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 Section 2: System Calls “Stuff that you could run from a Unix prompt” ls(1), cp(1), bash(1), kill(1), … Section 2: System Calls “Talking with the Kernel” kill(2), open(2), fork(2), … Section 3: Library Calls “The C Library” printf(3), scanf(3), fflush(3), …

Sections To specify a man section Man page for the command “man” man n … man 3 kill Man page for the command “man” man man

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