《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.
《Unix编程艺术》读书笔记(1)的更多相关文章
- 【读书笔记】《Computer Organization and Design: The Hardware/Software Interface》(1)
笔记前言: <Computer Organization and Design: The Hardware/Software Interface>,中文译名,<计算机组成与设计:硬件 ...
- 算法设计手冊(第2版)读书笔记, Springer - The Algorithm Design Manual, 2ed Steven S.Skiena 2008
The Algorithm Design Manual, 2ed 跳转至: 导航. 搜索 Springer - The Algorithm Design Manual, 2ed Steven S.Sk ...
- [读书笔记]设计原本[The Design of Design]
第1章 设计之命题 1.设计首先诞生于脑海里,再慢慢逐步成形(实现) 2.好的设计具有概念完整性:统一.经济.清晰.优雅.利落.漂亮... 第2章 工程师怎样进行设计思维——理性模型 1.有序模型的有 ...
- 【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句&倒装句(Reduced Clauses、Inverted Sentences) 【完结】
[英语魔法俱乐部——读书笔记] 3 高级句型-简化从句&倒装句(Reduced Clauses.Inverted Sentences):(3.1)从属从句简化的通则.(3.2)形容词从句简化. ...
- 【英语魔法俱乐部——读书笔记】 2 中级句型-复句&合句(Complex Sentences、Compound Sentences)
[英语魔法俱乐部——读书笔记] 2 中级句型-复句&合句(Complex Sentences.Compound Sentences):(2.1)名词从句.(2.2)副词从句.(2.3)关系从句 ...
- Computer architecture Computer organization
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCEComputer architectureNINTH EDITION C ...
- 图解TCP/IP读书笔记(一)
图解TCP/IP读书笔记(一) 第一章 网络基础知识 本学期的信安概论课程中有大量的网络知识,其中TCP/IP占了相当大的比重,让我对上学期没有好好学习计算机网络这门课程深感后悔.在老师的推荐下开始阅 ...
- 《Linux/Unix系统编程手册》读书笔记8 (文件I/O缓冲)
<Linux/Unix系统编程手册>读书笔记 目录 第13章 这章主要将了关于文件I/O的缓冲. 系统I/O调用(即内核)和C语言标准库I/O函数(即stdio函数)在对磁盘进行操作的时候 ...
- 《Linux/Unix系统编程手册》读书笔记7 (/proc文件的简介和运用)
<Linux/Unix系统编程手册>读书笔记 目录 第11章 这章主要讲了关于Linux和UNIX的系统资源的限制. 关于限制都存在一个最小值,这些最小值为<limits.h> ...
随机推荐
- Javascript和jquery事件--事件冒泡和事件捕获
jQuery 是一个 JavaScript 库,jQuery 极大地简化了 JavaScript 编程,在有关jq的描述中,jq是兼容现有的主流浏览器,比如谷歌.火狐,safari等(当然是指较新的版 ...
- 【MinGW】【C语言环境搭建】
问题 安装MinGW配置环境变量后终端输入gcc -v出错 解决 Win10下环境变量最后不用加分号
- 洛谷 P3385 【模板】负环
P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M ...
- iOS开发--漫谈内存管理(一)
1.MRC与ARC 苹果提供两种内存管理机制:一种是MRC(manual reference count),即手动引用计数:还有一种是ARC(auto reference count).即自己主动引用 ...
- 论Node在构建超媒体API中的作用
论Node在构建超媒体API中的作用 作者:chszs,转载需注明. 博客主页:http://blog.csdn.net/chszs 超媒体即Hypermedia,是一种採用非线性网状结构对块状多媒体 ...
- *android抓包工具——fiddler抓包分析api接口
本文地址:http://blog.csdn.net/u011733020 首先,写这个仅仅是为了学习.不是要做什么违法的事情,假设有问题,有关部门 请联系我,立刻删除. 不要查我水表. 正题:这里介绍 ...
- thinkphp5 left join
thinkphp5 left join 一.总结 1.作用:left join就是即使不匹配也返回左表中的数据 2.join使用通式:object join ( mixed join [, mixed ...
- BZOJ1492: [NOI2007]货币兑换Cash(CDQ分治,斜率优化动态规划)
Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个 ...
- Trie图(模板)
Trie图(蒟蒻听说AC自动机能做的题Trie图都能做,而且AC自动机可能被卡,就没学过AC自动机),最近想捡一捡,好久之前做的了. Trie图,就是一个在Trie树上建的图 大概描述一下 比如说有 ...
- 编程之路-client学习知识点纲要(Web/iOS/Android/WP)
Advanced:高级内容 Architect:架构设计 Core:框架底层原理分析 Language:框架经常使用语言 Objective-C Dart Swift Java Network:网络 ...