Lien000034
August 24, 2014
1. errno variable
The file <errno. h> defines the symbol errno and various constants that can be assigned to it. These constants start with the character E. For example, if errno is equal to the constant eacces, a permission error occurs (for example, you do not have sufficient permissions to open the required file ).
When a Unix function fails, a negative value is often returned, and the integer variable errno is set to each constant containing additional information. For example, if the open function is successfully executed, a non-negative file descriptor is returned. If an error occurs,-1 is returned. When an open error occurs, there are about 15 different errno values (file does not exist, permission issues, etc ).
Errno should know two rules.
Rule 1: If no error occurs, the errno value is not cleared by a routine.
Therefore, the value of errno is verified only when the return value of the function indicates an error.
Rule 2: no function will set the errno value to 0, and all constants defined in <errno. h> are not 0.
The error mark errno is an integer value, which is insufficient for user prompts. The C standard defines two functions to help print error information.
# Include <string. h>
Char * strerror (INT errnum );
Returned value: pointer to the message string
This function maps errnum (usually the errno value) to an error message string and returns a pointer to this string.
Based on the current value of errno, The perror function generates an error message for a standard error and returns it.
# Include <stdio. h>
Void perror (const char * MSG );
It first outputs the string directed by MSG, followed by a colon, a space, followed by an error message corresponding to the errno value, and finally a line break.
Example:
The following code shows how to use these two error functions.
#include <string.h>#include <stdio.h>#include <errno.h>#include <stdlib.h>intmain(int argc, char *argv[]){ fprintf(stderr, "EACCES: %s\n", strerror(EACCES)); errno = ENOENT; perror(argv[0]); exit(0);}
Compile the program, generate errno_demo, and then execute it.
lienhua34:demo$ gcc -o errno_demo errno_demo.clienhua34:demo$ ./errno_demoEACCES: Permission denied./errno_demo: No such file or directory
2. Print all error messages
The C standard library defines the total number of errors that sys_nerr uses to record. The following program prints all the information cyclically.
#include <string.h>#include <stdio.h>#include <errno.h>#include <stdlib.h>intmain(int argc, char *argv[]){ int idx = 0; for (idx = 0; idx < sys_nerr; idx++) { printf("Error #%3d: %s\n", idx, strerror(idx)); } exit(0);}
Compile the program, generate print_err, and then execute it.
lienhua34:demo$ gcc -o print_err print_err.clienhua34:demo$ ./print_errError # 0: SuccessError # 1: Operation not permittedError # 2: No such file or directoryError # 3: No such processError # 4: Interrupted system call...Error #133: Unknown error 133Error #134: Unknown error 134lienhua34:demo$
3. multi-thread Extension
In a thread-supporting environment, multiple threads share the process address space. Each thread has its own local errno to prevent one thread from interfering with another thread.
The strerror () function is not thread-safe. This function stores the string corresponding to errnum in a static buffer and returns the pointer to the buffer. When another thread calls strerror (), it will reset the content of the static buffer.
4. Error Recovery
Errors defined in <errno. h> can be divided into two types: fatal and non-fatal. A fatal error cannot be recovered. You can print at most one error message on the user's screen, or write an error message to the log file, and then terminate the operation. Non-fatal errors can be properly handled.
Unix programming learning notes (1): -- error handling errno