趣味英译文:apue第三版(代码片段)

看,未来 看,未来     2022-12-19     739

关键词:

1.7 Error Handling(错误处理)

When an error occurs in one of the UNIX System functions, a negative value is often returned, and the integer errno is usually set to a value that tells why. For example, the open function returns either a non-negative file descriptor if all is OK or −1 if an error occurs. An error from open has about 15 possible errno values, such as file doesn’t exist, permission problem, and so on. Some functions use a convention other than returning a negative value. For example, most functions that return a pointer to an object return a null pointer to indicate an error.

如果系统函数运行出问题了,会返回一个错误码,绑定着错误信息,还是个负数。就比方说如果我正确打开一个文件,则会返回一个正整数的文件描述符,反之返回-1。关于打开类型的错误有15种,比如说文件不存在、权限问题等。有些函数则使用除返回负值以外的方法,比如大多数返回指向对象的指针的函数都返回空指针以指示错误。


Error Recovery

The errors defined in <errno.h> can be divided into two categories: fatal and nonfatal. A fatal error has no recovery action. The best we can do is print an error message on the user ’s screen or to a log file, and then exit. Nonfatal errors, on the other hand, can sometimes be dealt with more robustly. Most nonfatal errors are temporary, such as a resource shortage, and might not occur when there is less activity on the system.

在<errno.h>中,错误可以被分为两类:完蛋的和不完蛋的。致命的错误是不会有什么前兆现象的,直接完蛋,我们所能做到的最好,就是及时的将错误信息打印在屏幕上,或者写入日志,然后退出。
非致命错误呢,换句话来说,都好办。这种错误,大部分都是一时的,偶尔发生的,就好比资源不足,当进程少的时候就不可能会发生。


Resource-related nonfatal errors include EAGAIN, ENFILE, ENOBUFS, ENOLCK, ENOSPC, EWOULDBLOCK, and sometimes ENOMEM. EBUSY can be treated as nonfatal when it indicates that a shared resource is in use. Sometimes, EINTR can be treated as a nonfatal error when it interrupts a slow system call (more on this in Section 10.5). The typical recovery action for a resource-related nonfatal error is to delay and retry later. This technique can be applied in other circumstances. For example, if an error indicates that a network connection is no longer functioning, it might be possible for the application to delay a short time and then reestablish the connection. Some applications use an exponential backoff algorithm, waiting a longer period of time in each subsequent iteration.

Ultimately, it is up to the application developer to determine the cases where an application can recover from an error. Ifareasonable recovery strategy can be used, we can improve the robustness of our application by avoiding an abnormal exit.

与资源相关的非致命错误包含 EAGAIN、ENFILE、ENOBUFS、ENOLCK、EWOULDBLOCK、有时候也会有ENOMEM。当EBUSY因共享资源被占用而触发时,也可以被视为非致命错误;当一个速度比较慢的系统调用被中断的时候,也会产生一个非致命错误,叫EINTR(不得不说,还真挺多)。一般解决这些问题有个很常见的方法,不过大部分人应该不会喜欢:延迟,稍后重试。。。

这个技术还可以去“祸害”别的地方,比如说当网络不可用的时候,那可能就需要延时一会儿再重试了(考考你:你说为什么不能不延时呢?)有些应用程序使用指数退避算法,在每次后续迭代中等待更长的时间(第一次,等n秒;第二次,等n^m秒,第三次,等n^m^o,哎)。


1.8 User Identification(用户身份识别)

User ID

The user ID from our entry in the password file is a numeric value that identifies us to the system. This user ID is assigned by the system administrator when our login name is assigned, and we cannot change it. The user ID is normally assigned to be unique for every user.

We call the user whose user ID is 0 either root or the superuser. The entry in the password file normally has a login name of root, and we refer to the special privileges of this user as superuser privileges. As we’ll see in Chapter 4, if a process has superuser privileges, most file permission checks are bypassed. Some operating system functions are restricted to the superuser. The superuser has free rein over the system.

用户ID是唯一的,由系统管理员在分配登录名的时候分配,普通用户无权修改。在登录的时候,密码文件会根据用户ID来做验证。

我们将用户ID为0的用户称为root用户(这我还真不知道)。密码文件中的条目通常具有root登录名,我们将此用户的特权称为超级用户特权。我们将在第4章中看到,如果进程具有超级用户权限,那么大多数文件权限检查都会被绕过。某些操作系统功能仅限于超级用户。超级用户可以自由控制系统(一般我们自己玩的时候都是选择超级用户权限,不然还玩个球啊)。


Group ID(组ID)

Our entry in the password file also specifies our numeric group ID. This, too, is assigned by the system administrator when our login name is assigned. Typically, the password file contains multiple entries that specify the same group ID. Groups are normally used to collect users together into projects or departments. This allows the sharing of resources, such as files, among members of the same group. We’ll see in Section 4.5 that we can set the permissions on a file so that all members of a group can access the file, whereas others outside the group cannot.

密码文件中的条目还指定了数字组ID。这也是在分配登录名时由系统管理员分配的。通常,密码文件包含多个指定相同组ID的条目。组通常用于将用户收集到项目或部门中。这允许在同一组的成员之间共享资源,例如文件。我们将在第4.5节中看到,我们可以设置文件的权限,以便组中的所有成员都可以访问该文件,而组外的其他成员则不能。

(对于组ID的研究不多,或者说只是听说过,嗯)


趣味英译文:apue第三版(代码片段)

1.9SignalsSignalsareatechniqueusedtonotifyaprocessthatsomeconditionhasoccurred.Forexample,ifaprocessdividesbyzero,thesignalwhosenameisSIGFPE(floating-pointexception)issenttotheprocess.Theprocesshasthr 查看详情

趣味英译文:apue第三版(代码片段)

1.7ErrorHandling(错误处理)WhenanerroroccursinoneoftheUNIXSystemfunctions,anegativevalueisoftenreturned,andtheintegererrnoisusuallysettoavaluethattellswhy.Forexample,theopenfunctionreturnseit 查看详情

趣味英译文:apue第三版(代码片段)

1.7ErrorHandling(错误处理)WhenanerroroccursinoneoftheUNIXSystemfunctions,anegativevalueisoftenreturned,andtheintegererrnoisusuallysettoavaluethattellswhy.Forexample,theopenfunctionreturnseit 查看详情

趣味英译文:apue第三版(代码片段)

1.7ErrorHandling(错误处理)WhenanerroroccursinoneoftheUNIXSystemfunctions,anegativevalueisoftenreturned,andtheintegererrnoisusuallysettoavaluethattellswhy.Forexample,theopenfunctionreturnseit 查看详情

每天看点英文文档:apue第三版(代码片段)

1.6ProgramsandProcesses(程序与进程)ProgramAprogramisanexecutablefileresidingondiskinadirectory.Aprogramisreadintomemoryandisexecutedbythekernelasaresultofoneofthesevenexecfunctions.We’llcover 查看详情

每天看点英文文档:apue第三版(代码片段)

1.6ProgramsandProcesses(程序与进程)ProgramAprogramisanexecutablefileresidingondiskinadirectory.Aprogramisreadintomemoryandisexecutedbythekernelasaresultofoneofthesevenexecfunctions.We’llcover 查看详情

每天看点英文文档:apue第三版(代码片段)

1.6ProgramsandProcesses(程序与进程)ProgramAprogramisanexecutablefileresidingondiskinadirectory.Aprogramisreadintomemoryandisexecutedbythekernelasaresultofoneofthesevenexecfunctions.We’llcover 查看详情

每天看点英文文档:apue第三版(代码片段)

1.5InputandOutput(输入输出流)FileDescriptorsFiledescriptorsarenormallysmallnon-negativeintegersthatthekernelusestoidentifythefilesaccessedbyaprocess.Wheneveritopensanexistingfileorcreatesanew 查看详情

每天看点英文文档:apue第三版(代码片段)

1.5InputandOutput(输入输出流)FileDescriptorsFiledescriptorsarenormallysmallnon-negativeintegersthatthekernelusestoidentifythefilesaccessedbyaprocess.Wheneveritopensanexistingfileorcreatesanew 查看详情

每天看点英文文档:apue第三版(代码片段)

1.5InputandOutput(输入输出流)FileDescriptorsFiledescriptorsarenormallysmallnon-negativeintegersthatthekernelusestoidentifythefilesaccessedbyaprocess.Wheneveritopensanexistingfileorcreatesanew 查看详情

每天看点英文文档:apue第三版(代码片段)

1.4FilesandDirectories(文件和目录)FileSystem(文件系统)TheUNIXfilesystemisahierarchicalarrangement(层次排列)ofdirectoriesandfiles.Everythingstartsinthedirectorycalledroot,w 查看详情

每天看点英文文档:apue第三版(代码片段)

1.4FilesandDirectories(文件和目录)FileSystem(文件系统)TheUNIXfilesystemisahierarchicalarrangement(层次排列)ofdirectoriesandfiles.Everythingstartsinthedirectorycalledroot,w 查看详情

每天看点英文文档:apue第三版(代码片段)

1.4FilesandDirectories(文件和目录)FileSystem(文件系统)TheUNIXfilesystemisahierarchicalarrangement(层次排列)ofdirectoriesandfiles.Everythingstartsinthedirectorycalledroot,w 查看详情

apue第三版习题10.5(代码片段)

这是书本上的答案:See‘‘ImplementingSoftwareTimers’’byDonLibes(CUsersJournal,vol.8,no.11,Nov.1990)foranexample.Acopyofthispaperisavailableonlineathttp://www.kohala.com/start/libes.timers.txt.  我参考上面提到 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情

每天看点英文文档:apue第三版(代码片段)

文章目录前言1、UNIXSystemOverview:Unix系统简介1.1Introduction1.2UNIXArchitecture(UNIX体系结构)1.3LoggingInLoginNameShells前言这本书好厚啊,一千多页。为什么会想看一本英文著作呢?我想,不久后的将来,我会感... 查看详情