第三篇:errno.h快速入门

    技术2024-03-28  15

     

    简介:

    头文件errno.h定义了一个全局的宏errno,它被展开为一个int类型的“左值”,这意味着宏errno不一定是个对象的标识符,也可以展开为一个由函数返回的可以修改的“左值”,比如int *errno(),这个后面会讲,你可以暂且把它理解为一个全局的int型变量(虽然这样理解是错的,不过方便理解)。

     

    简单来说,errno.h只是为了提供了一种错误报告机制。比如,一个函数调用fopen()发生了错误,它可能就会去修改errno的值,这样外部的代码可以通过判断errno的值来区分fopen()内部执行时是否发生错误,并根据errno值的不同来确定具体的错误类型。先来看一段代码,Demo1:

    #include <stdio.h> #include <errno.h> int main ( int argc, char *argv[] ) { //try to open file "whatever.txt". when you run this demo,make sure the file is NOT existed..。 FILE *fp = fopen("whatever.txt","r"); if(fp ==NULL) { printf("Can not open file/n"); printf("errno value: %d, it means: %s",errno, strerror(errno)); } return 0; }

    程序会输出:

    Can not open file errno value: 2, it means: No such file or directory

    strerror是标准库stdio.h定义的一个函数,它用来返回错误代码所代表的含义。如Demo1所示,我们用fopen(也在stdio.h中定义)打开一个并不存在的文件,因此返回的fp是一个空指针。而在fopen尝试打开文件失败时会修改errno的值,Demo1里fopen失败原因是文件不存在,因此fopen将会把errno指向的值修改为2,通过stderror可以看到错误代码2的意思是“No such file or directory”。

    帅气,看起来用errno来报告错误,既方便,也很简单,但实际应用时远没Demo1那么简单,试下Demo2:

    #include <stdio.h> #include <errno.h> #include <math.h> int main ( int argc, char *argv[] ) { /*try to open file "whatever.txt". when you run this demo,make sure the file is NOT existed...*/ FILE *fp = fopen("whatever.txt","r"); if(fp ==NULL) { printf("Can not open file/n"); int root = sqrt(123568 -123668 ); printf("errno value: %d, it means: %s",errno, strerror(errno)); } return 0; }

    程序会输出:

    Can not open file errno value: 33, it means: Numerical argument out of domain

    这跟我们期望的错误信息完全不一样,因为sqrt函数在得到一个非法的参数时,它把errno的值修改成了33,覆盖了fopen设置的错误代码。所以,使用errno一个比较安全的编码方式是,在下个库函数调用之前就查看它的值,千万不要因为某个库函数看似很简单就假设它不会修改errno的值,那样会死的很惨……如果必须在查看errno之前调用别的库函数,一种安全的方式是先把errno的值保存到一个临时变量里,然后调用那个“必须”调用的库函数,处理完毕后再把errno恢复到之前的值。如Demo3:

    #include <stdio.h> #include <errno.h> #include <math.h> double getSqrt(double value) { //1、save last errno to a temp variable int tmpErrno = errno; //2、set errno to 0 errno = 0; double root = sqrt(123568 -123668 ); printf("I changed errno to '%d' sliently...but it's safe /n",errno); //3.restore errno errno = tmpErrno; return root; } int main ( int argc, char *argv[] ) { FILE *fp = fopen("whatever.txt","r"); if(fp ==NULL) { printf("Can not open file/n"); getSqrt(-1); printf("errno value: %d, it means: %s",errno, strerror(errno)); } return 0; }

    程序会输出:

    Can not open file I changed errno to '33' sliently...but it's safe errno value: 2, it means: No such file or directory

    现在,就像期望的那样输出了。但是……这样真的安全么?!想像一下,如果errno是个全局变量,那多线程环境下岂不完蛋了?!本来线程A把errno设置成2,还没执行到查看错误的语句时,线程B就把errno设置成了33,然后线程A才开始查看errno并输出错误信息,而这时输出的错误就很让人抓狂了!神呀,这破东西多线程没法儿用哇!但是……你多虑了……文章开始说过,宏errno可以被展开为一个“左值”,比如int* getYourErrno(),所以你可以在getYourErrno()里返回一个线程内的局部变量,这样不管哪个线程修改errno都修改的它自己的局部变量,所以我们担心的问题是不存在的。看下errno.h的源码就明白了

    /* Get the error number constants from the system-specific file. This file will test __need_Emath and _ERRNO_H. */ #include <bits/errno.h> #undef __need_Emath #ifdef _ERRNO_H /* 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

    上面的注释说了,如果errno没有定义过就把errno定义为“extern int errno;”,如果这样多线程时是会发生悲剧的,先不着急哭,我们去前面看看它是否被定义过,前面的代码include了一个叫bits/errno.h的头文件,看名字就很“险恶”,进去看看:

    # ifndef __ASSEMBLER__ /* Function to get address of global `errno' variable. */ extern int *__errno_location (void) __THROW __attribute__ ((__const__)); # if !defined _LIBC || defined _LIBC_REENTRANT /* When using threads, errno is a per-thread value. */ # define errno (*__errno_location ()) # endif # endif /* !__ASSEMBLER__ */

    果然来者不善……如果没定义宏__ASSEMBLER__,就会执行中间的代码,里面的代码又说了,如果你没定义_LIBC或者定义了_LIBC_REENTRANT他们就会把errno定义为__errno_location,一看到宏_LIBC_REENTRANT里面有“reentrant(重入)”就知道它不是个好东西……不是,是看到它的名字就知道它是跟多线程有关的,所以如果要在多线程环境下正确的使用errno,你需要确保__ASSEMBLER__没有被定义,而且_LIBC没被定义或者定义了_LIBC_REENTRANT。可以写个程序看下自己开发环境里这几个宏的设置:

    #include <stdio.h> #include <errno.h> int main( void ) { #ifndef __ASSEMBLER__ printf( "__ASSEMBLER__ is NOT defined!/n" ); #else printf( "__ASSEMBLER__ is defined!/n" ); #endif #ifndef __LIBC printf( "__LIBC is NOT defined/n" ); #else printf( "__LIBC is defined!/n" ); #endif #ifndef _LIBC_REENTRANT printf( "_LIBC_REENTRANT is NOT defined/n" ); #else printf( "_LIBC_REENTRANT is defined!/n" ); #endif return 0; }

    我的输出:

    _ASSEMBLER__ is NOT defined! __LIBC is NOT defined! _LIBC_REENTRANT is NOT defined!

    哈!看来我可以在多线程下安全的使用errno,如果你的默认环境不可以就在makefile里定义上_LIBC_REENTRANT吧!ok,有关errno.h的介绍到此就结束,休息,休息一下!

     

    作者:Yao 首发自:http://blog.yaohuiji.com/ 欢迎转载,作者期望转载时带上原文链接,不过这不是必须的。但务必在文章标题处标明【转载】

    最新回复(0)