第十周
2015-11-15 21:55
375 查看
附录A
错误处理
包装函数被封装在一个源文件(csapp.c)中,这个文件被编译和链接到每个程序中。一个独立的头文件(csapp.h)中包含这些包装函数的函数原型。Unix系统中的错误处理
Unix风格的错误处理
通常形式:
if ((pid = wait(NULL))<0){ fprintf(stderr,"wait error: %s\n",strerror(errno)); exit(0); }
strerror函数返回某个errno值的文本描述
Posix风格的错误处理
任何有用的结果都返回在通过引用传递进来的函数参数中
通常形式:
if ((retcode = pthread_create(&tid,NULL,threadd,NULL))!=0){ fprintf(stderr,"pthread_create error: %s\n",strerror(retcode)); exit(0); }
DNS风格的错误处理
gethostbyname和gethostbyaddr函数失败时返回NULL指针,并设置全局变量h_errno。
通常形式:
if ((p = gethostbynae(name)) ==NULL)} fprintf(stderrr,"gethostbyname error: %s\n:",hstrerror(h_errno)); exit(0); }
错误报告函数
#include "csapp.h" void unix_error(char *msg); void posix_error(int code,char *msg); void dns_error(char *msg); void app_error(char *msg);
错误报告函数代码:
void unix_error(char *msg) { fprintf(stderr,"%s:%s\n",msg,strerror(errno)); exit(0); } void posix_error(int code,char *msg) { fprintf(stderr,"%s: %s\n",msg,strerror(code)); exit(0); } void dns_error(char *msg) { fprintf(stderr,"%s: DNS error %d\n",msg,h_errno); exit(0); } void app_error(char *msg) { fprintf(stderr,"%s\n",msg); exit(0); }
错误处理包装函数
Unix风格的错误处理包装函数
void Kill(pid_t pid,int signum) { int rc; if ((rc = kill(pid,signum))<0) unix_error("Kill error"); }
成功返回void,如果错误,打印一条消息然后退出。
Posix风格的错误处理包装函数
void Pthread_detach(pthread_t tid){ int rc; if ((rc = pthread_detach(tid)) != 0) posix_error(rc,"Pthread_detach error"); }
成功时返回void。
DNS风格的错误处理包装函数
struct hostent *Gethostbyname(const char *name) { struct hostent *p; if ((p = gethostbyname(name)) == NULL) dns_error("Gethostbyname error"); return p; }
相关文章推荐
- Python核心编程读笔 12:OOP
- 【Loadrunner】参数化的4种方法
- 蓝懿ios 技术内容交流和学习心得 11.15
- jEasyUI1.3.6版API中文版(Richie696)
- HMM,MEMM,CRF模型
- git 撤销操作
- Partition List
- Activity的生命周期详解+面试 Activity的启动模式
- org.eclipse.equinox.internal.p2.engine.phases.CheckTrust phase问题
- [Django架构流程分析]Django HTTP请求的处理流程
- 第一次spring冲刺第3、4天
- 构建前端工程
- Cookie & Session
- JavaScript _proto_、prototype原型、原型链、constructor构造器、类式继承、原型继承
- MySQL多主一从同步
- 第十章学习总结
- python第三方库的安装=>LTS
- JQ基础
- 信息安全系统设计基础 第10周学习笔记
- 捕获软件异常,再次运行时发送到服务器