【APUE】2、第二章UNIX标准及实现

来源:互联网 发布:苹果手机信号增强软件 编辑:程序博客网 时间:2024/05/21 10:29

第二章 UNIX标准及实现

 

2.1 引言

 

虽然U N I X应用程序在不同的U N I X版本之间进行移植相当容易,但是8 0年代U N I X版本的剧增以及它们之间差别的扩大导致很多大用户(例如美国政府)要求对其进行标准化。

 

 

2.2 UNIX标准化

 

2.2.1 ISO C

 

按照该标准定义的各个头文件,可将该库分成24区。

 

         ISOC头文件依赖于操作系统所配置的C编译器的版本。FreeBSD 8.0 配置了gcc 4.2.1版,Solaris 10配置gcc 3.4.3版(以及Sun Studio 自带的C编译器),Ubuntu 12.04(Linux 3.2.0)配置了gcc 4.6.3版,Mac OS X 10.6.8 配置了gcc 4.0.1 和 4.2.1 版。

 

 

 

 

2.2.2 IEEE POSIX

 

 

 

 

 

这前面四个图总结本书讨论的4UNIX系统实现所包含的头文件

 

 

 

2.3 UNIX系统实现

 

 

 

2.5 限制

 

2.5.1 ISO C 限制

 

 

 

 

 

2.5.2 POSIX限制

 

 

 

 

好的,看了这么多标准来标准去的,到现在我来总结一下,以上标准都是没什么用的!!!!仅供参考。

 

2.5.4 函数sysconf、pathconf、fpathconf

 


 

我们执行相应的awk文件的话

 

awk -f makeconf.awk

 

我们的文件是:

 

#!/usr/bin/awk -fBEGIN{printf("#include \"apue.h\"\n")printf("#include <errno.h>\n")printf("#include <limits.h>\n")printf("\n")printf("static voidpr_sysconf(char *, int);\n")printf("static voidpr_pathconf(char *, char *, int);\n")printf("\n")printf("int\n")printf("main(int argc, char *argv[])\n")printf("{\n")printf("\tif (argc != 2)\n")printf("\t\terr_quit(\"usage: a.out <dirname>\");\n\n")FS="\t+"while (getline <"sysconf.sym" > 0) {printf("#ifdef %s\n", $1)printf("\tprintf(\"%s defined to be %%ld\\n\", (long)%s+0);\n", $1, $1)printf("#else\n")printf("\tprintf(\"no symbol for %s\\n\");\n", $1)printf("#endif\n")printf("#ifdef %s\n", $2)printf("\tpr_sysconf(\"%s =\", %s);\n", $1, $2)printf("#else\n")printf("\tprintf(\"no symbol for %s\\n\");\n", $2)printf("#endif\n")}close("sysconf.sym")while (getline <"pathconf.sym" > 0) {printf("#ifdef %s\n", $1)printf("\tprintf(\"%s defined to be %%ld\\n\", (long)%s+0);\n",$1, $1)printf("#else\n")printf("\tprintf(\"no symbol for %s\\n\");\n", $1)printf("#endif\n")printf("#ifdef %s\n", $2)printf("\tpr_pathconf(\"%s =\", argv[1], %s);\n", $1, $2)printf("#else\n")printf("\tprintf(\"no symbol for %s\\n\");\n", $2)printf("#endif\n")}close("pathconf.sym")exit}END{printf("\texit(0);\n")printf("}\n\n")printf("static void\n")printf("pr_sysconf(char *mesg, int name)\n")printf("{\n")printf("\tlongval;\n\n")printf("\tfputs(mesg, stdout);\n")printf("\terrno = 0;\n")printf("\tif ((val = sysconf(name)) < 0) {\n")printf("\t\tif (errno != 0) {\n")printf("\t\t\tif (errno == EINVAL)\n")printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")printf("\t\t\telse\n")printf("\t\t\t\terr_sys(\"sysconf error\");\n")printf("\t\t} else {\n")printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")printf("\t\t}\n")printf("\t} else {\n")printf("\t\tprintf(\" %%ld\\n\", val);\n")printf("\t}\n")printf("}\n\n")printf("static void\n")printf("pr_pathconf(char *mesg, char *path, int name)\n")printf("{\n")printf("\tlongval;\n")printf("\n")printf("\tfputs(mesg, stdout);\n")printf("\terrno = 0;\n")printf("\tif ((val = pathconf(path, name)) < 0) {\n")printf("\t\tif (errno != 0) {\n")printf("\t\t\tif (errno == EINVAL)\n")printf("\t\t\t\tfputs(\" (not supported)\\n\", stdout);\n")printf("\t\t\telse\n")printf("\t\t\t\terr_sys(\"pathconf error, path = %%s\", path);\n")printf("\t\t} else {\n")printf("\t\t\tfputs(\" (no limit)\\n\", stdout);\n")printf("\t\t}\n")printf("\t} else {\n")printf("\t\tprintf(\" %%ld\\n\", val);\n")printf("\t}\n")printf("}\n")}


  

在我的Ubuntu中执行的结果是:


#include "apue.h"#include <errno.h>#include <limits.h>static voidpr_sysconf(char *, int);static voidpr_pathconf(char *, char *, int);intmain(int argc, char *argv[]){if (argc != 2)err_quit("usage: a.out <dirname>");exit(0);}static voidpr_sysconf(char *mesg, int name){longval;fputs(mesg, stdout);errno = 0;if ((val = sysconf(name)) < 0) {if (errno != 0) {if (errno == EINVAL)fputs(" (not supported)\n", stdout);elseerr_sys("sysconf error");} else {fputs(" (no limit)\n", stdout);}} else {printf(" %ld\n", val);}}static voidpr_pathconf(char *mesg, char *path, int name){longval;fputs(mesg, stdout);errno = 0;if ((val = pathconf(path, name)) < 0) {if (errno != 0) {if (errno == EINVAL)fputs(" (not supported)\n", stdout);elseerr_sys("pathconf error, path = %s", path);} else {fputs(" (no limit)\n", stdout);}} else {printf(" %ld\n", val);}}



2.5.5 不确定的运行时限制

 

 

 

1、路径名

 

略(由于个人知识有限,展示无法理解,后面章节还会继续探讨)

 

 

2、最大打开文件数

 

 

 

Closei)的意思就是根据这个序列号来关闭相应的文件

 

 

#include "apue.h"#include "error.c"#include <errno.h>#include <limits.h>#ifdef OPEN_MAXstatic long openmax = OPEN_MAX;#elsestatic long openmax = 0;#endif/* * 如果OPEN_MAX是不确定的 */#define OPEN_MAX_GUESS 256long open_max(void){if(openmax == 0){//如果定义为了0,说明没有定义OPEN_MAXerrno = 0;if((openmax = sysconf(_SC_OPEN_MAX)) < 0){if(errno == 0){openmax = OPEN_MAX_GUESS;//那么我们对于不确定的值设定为我们猜的值}//ifelse{err_sys("sysconf error for _SC_OPEN_MAX");}//else}//if}//ifreturn (openmax);}

2.6 选项

 

 

2.8 基本系统数据类型

 

 

 

 

2.10 总结

1、首先不要问我为什么标题不连贯,因为我把书中我不太懂,或者我觉得没必要说明的东西省略了,但是标题还是根据书上来的

2、然后这章讲的是Unix的几个标准的定义,以及一些限制和幻常量

3、最后我想说的是,这章没什么卵用,标准是标准,我也写不出超脱标准的东西,就算写出来了也用不上,再有一个标准也并不完善,乱七八糟的,所以这章纯开阔视野。

 

 

 

 

 

 

 

1 0
原创粉丝点击