APUE——unix出错处理

1. errno原理

当UNIX函数出错时(系统调用),常常会返回一个负值,而且整型变量errno通常被设置为含有附加信息的一个值。

文件<errno.h>中定义了符合errno以及可以赋予它的各种常量,这些常量都以字符E开头。另外,UNIX系统手册第2部分的第1页intro(2)列出了所有这些出错常量。在Linux中,出错常量在errno(3)手册页中列出(可以使用命令man 3 errno查看)。

  1. errno被重新定义为线程私有数据。这样,一个线程做了设置errno的操作并不会影响进程中其他线程的errno的值。
  2. 如果没有出错,则其值不会被一个例程清除。因此,仅当函数的返回值指明出错时,才检验其值。
  3. 任一函数都不会将errno值设置为0,在<errno.h>中定义的所有常量都不为0。
extern int errno;

将errno设置为线程局部变量是个不错的主意,事实上,GCC中就是这么干的。他保证了线程之间的错误原因不会互相串改,当你在一个线程中串行执行一系列过程,那么得到的errno仍然是正确的。
多线程的时候,为了保证线程安全,在单个线程中,errno通过指针获取当前线程中的错误信息

extern int *__errno_location(void);
#define errno (*__errno_locatioin())

2. errno的获取函数

2.1 strerror

输入为errno的整形值,为errno或者是具体的枚举的key

char *strerror(int errnum)
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main ()
{
   FILE *fp;

   fp = fopen("file.txt","r");
   if( fp == NULL ) 
   {
      printf("Error: %s\n", strerror(errno));
   }
   
  return(0);
}
Error: No such file or directory

2.2 perror

C 库函数 void perror(const char *str) 把一个描述性错误消息输出到标准错误 stderr。首先输出字符串 str,后跟一个冒号,然后是一个空格。

void perror(const char *str)

#include <pthread.h>
#include "apue.h"
#include <errno.h>

int main()
{
    FILE* fp;

    fp = fopen("filename.txt","r");
    if(fp == NULL)
        perror("Error: ");
    printf("the error is : %s\n",strerror(errno));
    for(int i = 0;i<=256;i++)
        printf("errno: %2d\t%s\n",i,strerror(i));
	return 0;
}
Error: : No such file or directory
the error is : No such file or directory
errno:  0       Success
errno:  1       Operation not permitted
errno:  2       No such file or directory
errno:  3       No such process
errno:  4       Interrupted system call
errno:  5       Input/output error
errno:  6       No such device or address
errno:  7       Argument list too long
errno:  8       Exec format error
errno:  9       Bad file descriptor
errno: 10       No child processes
等133

猜你喜欢

转载自blog.csdn.net/weixin_44537992/article/details/106119994
今日推荐