《Unix编程艺术》读书笔记(1)

《Unix编程艺术》读书笔记(1)



这两天开始阅读该书,下面是自己的体会,以及原文的摘录,虽然有些东西还无法完全吃透。

写优雅的代码来提高软件系统的透明性:(P134)

Elegance is a combination of power and simplicity. Elegant code does much with little. Elegant code is not only correct but visibly, transparently correct. It does not merely communicate an algorithm to a computer, but also conveys insight and assurance to the mind of a human that reads it. By seeking elegance in our code, we build better code. Learning to write transparent code is a first, long step toward learning how to write elegant code — and taking care to make code discoverable helps us learn how to make it transparent. Elegant code is both transparent and discoverable.

调试工具是通往代码内部的窗口,能够发现大多数的bug:(P139)

The lesson is this: Don’t let your debugging tools be mere afterthoughts or treat them as throwaways. They are your windows into the code; don’t just knock crude holes in the walls, finish and glaze them. If you plan to keep the code maintained, you’re always going to need to let light into it.

GCC编译器是一个关于透明性的很好的实例,它能通过命令行清晰的揭示出预处理器,汇编器,连接器等各个组件的具体工作流,中间结果很有用:P(140)

the driver program has monitoring switches that merely (but sufficiently) expose the textual data flows among the components.

要追求代码的透明,就是不要在具体操作的代码之上叠放太多的抽象层,保持薄胶合层:P149

If you want transparent code, the most effective route is simply not to layer too much abstraction over what you are manipulating with the code.

批评了过度的面向对象,既要模块化编程,也要保持着地,而不是站的离基本组件太高(不同的语言应该有所侧重??):P150

Unix programmers are the original zealots about modularity, but tend to go about it in a quieter way. Keeping glue layers thin is part of it; more generally, our tradition teaches us to build lower, hugging the ground with algorithms and structures that are designed to be simple and transparent.

透明性和可显性,与模块性一样,是软件系统的设计特性,而不是代码的编码风格,难有特定的硬性规定,需要思考:P150

  • What is the maximum static depth of your procedure-call hierarchy? That is, leaving out recursions, how many levels of call might a human have to model mentally to understand the operation of the code? Hint: If it’s more than four, beware.
  • Does the code have invariant properties that are both strong and visible? Invariant properties help human beings reason about code and detect problem cases.
  • Are the function calls in your APIs individually orthogonal, or do they have too many magic flags and mode bits that have a single call doing multiple tasks? Avoiding mode flags entirely can lead to a cluttered API with too many nigh-identical functions, but the obverse error (lots of easily-forgotten and confusable mode flags) is even more common.
  • Are there a handful of prominent data structures or a single global scoreboard that captures the high-level state of the system? Is this state easy to visualize and inspect, or is it diffused among many individual global variables or objects that are hard to find?
  • Is there a clean, one-to-one mapping between data structures or classes in your program and the entities in the world that they represent?
  • Is it easy to find the portion of the code responsible for any given function? How much attention have you paid to the readability not just of individual functions and modules but of the whole codebase?
  • Does the code proliferate special cases or avoid them? Every special case could interact with every other special case; all those potential collisions are bugs waiting to happen. But even more importantly, special cases make the code harder to understand.
  • How many magic numbers (unexplained constants) does the code have in it? Is it easy to discover the implementation’s limits (such as critical buffer sizes) by inspection?

隐藏细节和无法访问细节有着重要区别,优秀的程序员应该给程序设定调试标志和探测开关,看开源代码,这一点很常见,debug选项,可以透视程序的运行状态。P151

Programs that cannot reveal what they are doing make troubleshooting far more difficult. Thus, experienced Unix users actually take the presence of debugging and instrumentation switches as a good sign, and their absence as possibly a bad one. Absence suggests an inexperienced or careless developer; presence suggests one with enough wisdom to follow the Rule of Transparency.

将软件系统划分为协作进程,虽全局复杂度降低,但是代价是需要合适的进程间通信协议,接口部分都是bug的聚集地,还需要为通信各方设计状态机,真正重要的不是协议语法而是协议逻辑(模型的正确性)。P159

The real challenge is not protocol syntax but protocol logic—designing a protocol that is both sufficiently expressive and deadlock-free. Almost as importantly, the protocol has to be seen to be expressive and deadlock-free; human beings attempting to model the behavior of the communicating programs in their heads and verify its correctness must be able to do so.

这里翻译的不好理解,应该理解为popen只能为shellout搭建输入或则输出管道,但是却不能构建双向管道,因为pipe是单向的。P168

Unix’s popen(3) call can set up either an input pipe or an output pipe for a shellout, but not both for a slave process — this seems intended to encourage simpler programming.

信号IPC的一种常用场景是pidfile,进程A把自身的PID写入到一个普通的文件中,在以后的某个时候进程B可以据此文件对A进行控制,如kill它。P171

A technique often used with signal IPC is the so-called pidfile. Programs that will need to be signaled will write a small file to a known location (often in /var/run or the invoking user’s home directory) containing their process ID or PID. Other programs can read that file to discover that PID. The pidfile may also function as an implicit lock file in cases where no more than one instance of the daemon should be running simultaneously.

SIGHUP信号通常作为守护进程重新载入配置文件的信号。P172

Many well-known system daemons accept SIGHUP (originally the signal sent to programs on a serial-line drop, such as was produced by hanging up a modem connection) as a signal to reinitialize (that is, reload their configuration files); examples include Apache and the Linux implementations of bootpd(8), gated(8), inetd(8), mountd(8), named(8), nfsd(8), and ypbind(8).

虽然文本流没有经典的RPC性能高,但是系统简单,易于理解,XML-RPC/json-rpc综合了二者的优点。P179

Even if text streams were less efficient than RPC, the performance loss would be marginal and linear, the kind better addressed by upgrading your hardware than by expending development time or adding architectural complexity. Anything you might lose in performance by using text streams, you gain back in the ability to design systems that are simpler — easier to monitor, to model, and to understand.

线程共享地址空间,暴漏了彼此太多的内部状态,所以产生了竞争问题。而进程有独立的地址空间,良好的封装,通过明确的IPC进行通信。而且,在多线程中的时序依赖也是一个很难的问题。P180

Threads are a fertile source of bugs because they can too easily know too much about each others’ internal states. There is no automatic encapsulation, as there would be between processes with separate address spaces that must do explicit IPC to communicate. Thus, threaded programs suffer from not just ordinary contention problems, but from entire new categories of timing-dependent bugs that are excruciatingly difficult to even reproduce, let alone fix.

时间: 2024-10-24 06:38:45

《Unix编程艺术》读书笔记(1)的相关文章

《Programming in Lua 3》读书笔记(二十二)

日期:2014.8.6 PartⅣ The C API 26 Extending Your Application 使用Lua很重要的一点是用来做配置语言.配合主语言做一些功能的配置. 26.1 The Basics 有的时候程序需要配置一些功能信息,很多时候可能有许多别的方法比用lua做配置要更简单:如使用环境变量或者读取文件,读取文件涉及到文件的解析.如果使用Lua进行配置的话,相当于用lua文件替代了要读取的如csv.txt文件等. 使用Lua进行配置的时候,就需要使用Lua API去控制

《Programming in Lua 3》读书笔记(二十一)

日期:2014.8.1 PartⅣ The C API 25 An Overview of the C API Lua是一种嵌入式语言.这就意味着Lua不是单独存在的,而是可以通过一系列的标准库将lua的特性嵌入至其他应用模块中. Lua以Lua interpreter(lua的解释器?)来解决了其不是独立程序,我们直到现在却又能独立使用Lua的问题.这个解释器是一个小型的程序(不超过500行代码),使用lua的标准库来实现独立解释程序,这个程序将处理与用户的交互等操作交给lua的标准库,这些库

读书笔记:《重来REWORK》

读书笔记:<重来REWORK> <重来Rework--更为简单有效的商业思维>这本书是看了别人的书单而购买的,初 拿到这本书翻看时,感觉有两点与平常的书不同,一是每个小节非常短,通常是一页,最多二页,二是附有大量的插图,原以为这些插图可能是用来凑页数的,但细 看一些插图,虽然都是用手工画的,但却能用形象的办法表示出一小节的观点,很象是出自做软件原型的人员之手. 这本书包含了80多个观点,特别适合指导小型软件公司的创业,感觉对我做的软件项目也有帮助. 卸负篇TAKEDOWNS 忘了“

【游戏设计模式】之四 《游戏编程模式》读书笔记:全书内容梗概总结

本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接:http://blog.csdn.net/poem_qianmo/article/details/53240330 作者:毛星云(浅墨)    微博:http://weibo.com/u/1723155442 本文的Github版本:QianMo/Reading-Notes/<游戏编程模式>读书笔记 这是一篇超过万字读书笔记,总结了<游戏编程模式>一书中所有章节与内容的知识梗概. 我们知道,游戏行业其实一直很缺一本系

读书笔记-2015年第1本:《暗时间》

作者所说的暗时间,其实在生活中,我自己也有领悟到,领悟的时候很感慨,然而过不了几天就把这个领悟给忘得一干二净,于是又是过着重蹈覆辙的生活.现在给我的提醒就是把领悟到的东西记录下来,每天空出一些时间静下心专门来思考这些领悟到的东西.现在有幸看到自己曾经遗忘的宝贵的生活哲理,好好思考,好好领悟. 在这本书中,作者推荐了大量有价值的学习资料以及学习方法,有时间时都可以去研究学习. 我们可以看出,作者很有学问,但也可以想一下为何如此有学问?“看上去好像很高端的样子”,实际上,也就是作者曾经在一年内看过很

【python下使用OpenCV实现计算机视觉读书笔记3】读写视频文件

Lua可以调用C函数的能力将极大的提高Lua的可扩展性和可用性. 对于有些和操作系统相关的功能,或者是对效率要求较高的模块,我们完全可以通过C函数来实现,之后再通过Lua调用指定的C函数. 对于那些可被Lua调用的C函数而言,其接口必须遵循Lua要求的形式,即typedef int (*lua_CFunction)(lua_State* L). 简单说明一下,该函数类型仅仅包含一个表示Lua环境的指针作为其唯一的参数,实现者可以通过该指针进一步获取Lua代码中实际传入的参数.返回值是整型,表示该

《程序员的呐喊》读书笔记(下)

接着<程序员的呐喊>读书笔记(上),继续分享下篇,这次干货比较多哦,有静动态类型的优缺点.强弱类型系统的对抗.设计模式.程序员的数学.编译器的重要性以及保守派自由派的较量,一时消化不了的建议保存以便read it later. 静态类型和动态类型的优缺点 静态类型的优点下面列出了静态类型的主要优点:(1)静态类型可以在程序运行之前,依赖其与生俱来的限制来及早发现一些类型错误.(或是在插入/更新记录,解析XML文档等情况下进行检测.)(2)静态类型有更多机会(或者说更容易)优化性能.例如只要数据

代码的未来读书笔记&lt;二&gt;

代码的未来读书笔记<二> 3.1语言的设计 对Ruby JavaScript Java Go 从服务端客户端以及静态动态这2个角度进行了对比. 这四种语言由于不同的设计方针,产生了不同的设计风格. Header 客户端 服务端 动态 Html5 Ruby 静态 Java Go 静态动态 静态:无需实际运行,仅根据程序代码就能确定结果. 动态:只有到了运行时才能确定结果.不过无论任何程序,或多或少都包含的动态的特性. 动态运行模式 运行中的程序能识别自身,并对自身进行操作.对程序自身进行操作的编

读书笔记:技术的本质-技术是什么,它是如何进化的 (布莱恩?阿瑟)

读书笔记算不算原创? - page 30 然而,作为人类,我们实际上不应该和技术如此紧密地结合,而是应该和其他什么东西融合得更为紧密,那就是自然.在最深的层次上,人的存在应该和自然,和我们最初的环境,以及最初使我们成为人的那些条件相融合. ========== - page 36 如今机器被用来生产机器了,同时它又变成了以后同类机器的父母. ========== - page 43 技术的建构不仅来自已有技术的组合,还来自于对自然现象的捕捉和征服.在 ========== - page 44 技

读书笔记:技术的本质-技术是什么,它是怎样进化的 (布莱恩?阿瑟)

读书笔记算不算原创? - page 30 然而.作为人类,我们实际上不应该和技术如此紧密地结合,而是应该和其它什么东西融合得更为紧密,那就是自然. 在最深的层次上.人的存在应该和自然,和我们最初的环境,以及最初使我们成为人的那些条件相融合. ========== - page 36 现在机器被用来生产机器了,同一时候它又变成了以后同类机器的父母. ========== - page 43 技术的建构不仅来自已有技术的组合.还来自于对自然现象的捕捉和征服.在 ========== - page 4