简单的说,errno在标准C中是一个整型变量,在errno.h中声明,C标准库中实现。多线程技术中,为了使errno线程安全,使用宏定义替代了简单的extern int errno声明。man errno, 再看看C99标准文档,就明白了。
---选自/usr/include/bits/errno.h------
//当使用多线程时,errno是个宏定义
# if !defined _LIBC || defined _LIBC_REENTRANT
/* When using threads, errno is a per-thread value. */
# define errno (*__errno_location ())
# endif
# endif /* !__ASSEMBLER__ */
#endif /* _ERRNO_H */
----选择/usr/include/errno.h-------
//标准的errno定义,在errno.h中声明,libc标准库中实现定义。
/* Declare the `errno' variable, unless it's defined as a macro by
bits/errno.h. This is the case in GNU, where it is a per-thread
variable. This redeclaration using the macro still works, but it
will be a function declaration without a prototype and may trigger
a -Wstrict-prototypes warning. */
#ifndef errno
extern int errno;
#endif
以下是C99标准对errno.h的说明:(从PDF中拷的,格式混乱,凑合看一下....)
7.5 Errors<errno.h>1The header <errno.h> de?nes several macros, all relating to the reporting of errorconditions.2The macros areEDOMEILSEQERANGEwhich expand to integer constant expressions with typeint,distinct positive values, andwhich are suitable for use in#ifpreprocessing directives; anderrnowhich expands to a modi?able lvalue169)that has typeint,the value of which is set to apositive error number by several library functions. It is unspeci?ed whethererrnois amacro or an identi?er declared with external linkage. If a macro de?nition is suppressedin order to access an actual object, or a program de?nes an identi?er with the nameerrno,the behavior is unde?ned.3The value of errno is zero at program startup, but is never set to zero by any libraryfunction.170)The value of errno may be set to nonzero by a library function callwhether or not there is an error, provided the use of errno is not documented in thedescription of the function in this International Standard.4Additional macro de?nitions, beginning with E and a digit or E and an uppercaseletter,171)may also be speci?ed by the implementation.
The integer errno is set by system calls (and some library functions) to indicate what went wrong. Its value
is significant only when the call returned an error (usually -1), and a library function that does succeed is
allowed to change errno.
Sometimes, when -1 is also a legal return value one has to zero errno before the call in order to detect possi-
ble errors.
errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly
declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in
any other thread.
Valid error numbers are all non-zero; errno is never set to zero by any library function. All the error names
specified by POSIX.1 must have distinct values.
POSIX.1 (2001 edition) lists the following symbolic error names. Of these, EDOM and ERANGE are in the ISO C
standard. ISO C Amendment 1 defines the additional error number EILSEQ for coding errors in multibyte or wide
characters.