Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Chapter - Error Handling and Input and Output, PF, Semester, Engineering

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE) PDF Download

 Introduction

 

The slides cover the following topics:

• Error handling
• I/O
• Man pages

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen

 

Important Dates

 

•Lab 6 (Malloc)
–Due Tuesday, November 19
–Next Monday’s recitation
•Exam 2
–Tuesday, November 12
–Review session next Monday evening
 
 
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
 
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
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Different Error Handling Styles
 
•Unix-Style
 – e.g. kill, signal, fork, etc.
 
•Posix-Style
 – e.g. pthread_create
 
•DNS-Style
–e.g. gethostbyname
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
 
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

 

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
 
Unix-Style Error Handling Cont’d

 

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Posix-Style Error Handling
 
•Return value only indicate success (0) or failure (nonzero)
 
•Useful results returned in function arguments

 

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
 
DNS-Style Error Handling
 
 
•Return a NULL pointer on failure
 
•Set the global h_errno variable

 

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen

 

Example: Wrappers

 

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

•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
 
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
 
Handle Errors Gracefully
 
 
•The wrappers shown above calls exit()
 
Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)
 
•In many situations, we want to handle errors more gracefully.
 
•For example: web server, etc.
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
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 …
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Unix I/O & Standard I/O
 
Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Unix I/O
 
•System calls
 –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);
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
 
Special File Descriptors
 
•0: standard in
 
•1: standard out
 
•2: standard error
 
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Standard I/O
 
 
•The C library I/O routines
 –printf, scanf
 –Formatting
  •printf(“%x ”, 0x15213);
 –Buffering
  •Eventually will call the Unix I/O routines (syscalls)
  •Buffers input to minimize the number of syscalls
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Example: buffered_io.c
 
Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)
 
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
strace
 
strace <program>
 –Runs <program> and prints out info about all the system calls
 
•Let’s run strace on buffered_io

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen

 

File Streams

 

•C Library equivalent of file descriptors
 –FILE*
 –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,…)
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Flushing a File Stream
 
•Force the C library to write any buffered data using Unix I/O
 
int fflush(FILE *stream);
 
fflush(stdout);
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Example: buffered_io_flush.c
 
 
Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
strace, revisited
 
•Let’s run strace  buffered_io_flush

 

Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen

 

Man Pages

 

•Contains detailed description of
 –Commands
 –System calls
 –C library functions
 –etc.
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
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.

 

Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen

 

Man page Sections

 

•Section 1: Commands
–“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), …
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Sections
 
•To specify a man section
 –man n
 –man 3 kill
•Man page for the command “man
 –man man
 
Error Handling and Input and Output----------------------------Next slide-------------------------Shimin Chen
 
Summary
 
•Error handling
 –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
The document Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on Chapter - Error Handling and Input and Output, PF, Semester, Engineering - Computer Science Engineering (CSE)

1. What is error handling in computer programming?
Ans. Error handling in computer programming refers to the process of anticipating, detecting, and resolving errors or exceptions that can occur during the execution of a program. It involves implementing mechanisms to handle unexpected situations, such as input validation errors, system failures, or incorrect data processing. Proper error handling helps ensure that a program can gracefully handle errors and provide appropriate feedback to users.
2. How can error handling be implemented in programming languages?
Ans. Error handling in programming languages can be implemented using various techniques. Some commonly used approaches include: - Using try-catch blocks: This mechanism allows developers to enclose potentially error-prone code within a try block and catch any exceptions that occur during its execution. The catch block is responsible for handling the exception and performing necessary actions. - Using error codes or return values: In this approach, functions or methods return specific error codes or values to indicate the occurrence of an error. The calling code can then check these values and handle the error accordingly. - Using exception classes: Object-oriented languages often provide exception classes that developers can throw when an error occurs. These exceptions can be caught and handled using catch blocks.
3. What is input and output (I/O) in computer programming?
Ans. Input and output (I/O) in computer programming refers to the process of transferring data between a computer system and external devices, such as keyboards, monitors, files, or networks. Input involves receiving data or commands from external sources, while output involves displaying or saving data produced by the program.
4. How can input and output be performed in programming languages?
Ans. Programming languages provide various mechanisms for performing input and output operations. Some common methods include: - Using standard input/output functions: Most programming languages provide standard functions or libraries that allow reading input from the keyboard and displaying output to the console. Examples include scanf and printf in C, input and print functions in Python, and cin and cout in C++. - File I/O operations: Programming languages also offer features to read from and write to files on disk. Developers can use functions or classes provided by the language to open, read, write, and close files. Examples include fopen, fread, fwrite, and fclose in C, file handling in Python, and ifstream and ofstream in C++. - Network I/O: For network communication, programming languages often provide libraries or APIs to establish connections, send and receive data over networks, and handle network-related errors. Examples include socket programming in C and C++, and libraries like urllib in Python for web-related I/O.
5. Why is error handling and proper I/O important in computer programming?
Ans. Error handling and proper input and output (I/O) are crucial in computer programming for the following reasons: - Robustness: Proper error handling helps in creating robust and reliable programs that can handle unexpected situations gracefully. It allows developers to anticipate and handle errors, preventing program crashes or incorrect behavior. - User experience: Effective error handling provides meaningful feedback to users, making programs more user-friendly. It helps users understand and resolve errors, improving their overall experience with the software. - Data integrity: Correct input validation and error handling ensure that the program processes data correctly. This prevents data corruption, security vulnerabilities, or unintended consequences of incorrect input. - Debugging and maintenance: Proper error handling and I/O practices make it easier to debug and maintain code. Error messages and logs provide valuable information for identifying and fixing issues, and well-structured input and output operations improve code readability and maintainability.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

Chapter - Error Handling and Input and Output

,

Free

,

Semester

,

Important questions

,

pdf

,

Exam

,

Sample Paper

,

PF

,

Semester

,

video lectures

,

Previous Year Questions with Solutions

,

Chapter - Error Handling and Input and Output

,

Objective type Questions

,

Summary

,

Engineering - Computer Science Engineering (CSE)

,

MCQs

,

ppt

,

Viva Questions

,

Semester

,

PF

,

Chapter - Error Handling and Input and Output

,

shortcuts and tricks

,

PF

,

practice quizzes

,

Extra Questions

,

study material

,

Engineering - Computer Science Engineering (CSE)

,

Semester Notes

,

past year papers

,

Engineering - Computer Science Engineering (CSE)

;