When a Unix function fails, a negative number is often returned, and the integer errno is usually set to a value containing additional information. For example, if the open function is successful, 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, and so on ). Some functions do not return negative numbers, but use another convention. For example, if you return a majority of functions pointing to object pointers, a null pointer is returned when an error occurs.
The file <errno. h> defines the symbol errno and various constants that can be assigned to it. These constants start with the character E.
POSIX and Iso c define errno as such a symbol, which is extended into a modifiable left INTEGER (lvalue), which can be an integer containing an error number, or a function that returns an error number pointer. The previous definition is:
Extern int errno
However, in a multi-threaded environment, multiple threads share the process address space. Each thread has its own local errno to prevent one thread from interfering with another thread. For example, Linux supports multi-threaded access to errno, which is defined:
Extern int * _ errno_location (void );
# Define errno (* _ errno_location ())
Therefore, in Linux, errno is thread-safe.