Liuw’s Thinkpad

想要赢就先学会输,想要成功就先学会失败

Archive for the ‘分享’ Category

近期收集的一些有趣的小東東

without comments

近來比較少寫blog,實在是比較忙。用繁體寫是因為看簡體的字多了眼花,得換換口味了。

72pines的頁面很奇怪,假如我不用搜狗的全網加速功能去開,出來的頁面就沒有樣式了。可能它有一部份的伺服器被教育網墻掉了(?)。

在王亮先生的Emacs定制和擴展中,找到一個“能獲取root權限的輔助腳本”,解決了我一直頭痛的一個問題。原理挺簡單,使用TRAMP來完全的。以前一直以為TRAMP主要是用于遠程編輯文件的,沒有發現這個功能,沒仔細看Manual的後果。


(defun wl-sudo-find-file (file dir)
  (find-file (concat "/sudo:localhost:" (expand-file-name file dir))))

當然,其他的內容也很精彩。

第二個,是從Wowubuntu得來的三手信息。爲什麽說是三手呢,因為它也只是轉載的。主要是講Bash shell的一些奇技淫巧。

最牛B的 Linux Shell 命令 系列连载,有點標題黨了,呵呵。

比較有趣的是活用history功能的條目,比如說sudo執行前一條命令是:


$ sudo !!

兩個嘆號“!!”等價于“!-1”,數字換成其他也是可以的,不過誰記得了那麼多啊。

另外一個有趣的功能就是Bash的替換(原來它也有啊,呵呵):


$ !!:s/foo/bar/

很熟悉的語法,只是從來沒有想到過還有這個功能,sigh。

文章提到上面兩個技巧都是出自The Definitive Guide to Bash Command Line History。

還有一些其他的技巧,不僅僅限於Bash本身,不一而足。由於有的要么知道了,要么對我自己用處不大,這里就不多記了。

Written by liuw

八月 26th, 2010 at 7:00 下午

Posted in UNIX-like, 分享

Tagged with , ,

五笔拆字约定

without comments

1. 凡单笔画与字根相连或带点结构都视为杂全型,如“天”、“千”、“丸”、“太”。

2. 区分汉字结构时,按“能散不连”的原则来进行,如“矢”、“卡”、“严”、“走”都视为上下型。

3. 含两字根且相交者属杂合型,如“乐”、“串”、“电”、“本”。

4. 下含“走之”字为杂合型,如“进”、“过”、“这”、“边”。

5. 属于“连”和“交”的汉字一律视为杂合型。

Written by liuw

八月 8th, 2010 at 9:34 上午

Posted in 分享

Tagged with ,

好文传递:Zero Copy I: User-Mode Perspective

with 2 comments

“零拷贝”这个词也听到不少了,据说是可以明显提高性能。网上找到好文一篇。

Zero Copy I: User-Mode perspective

在一些程序,比如说HTTP服务器,需要向用户发送文件,有一部分是图片、静态网页之类不需要处理就可以直接发送的文件。土一点的做法无非是:


read(file_fd, buf, len);
write(socket_fd, buf, len);

实际上,read(2)这个操作,是首先把文件内容读入到内核缓冲区中去,然后再把文件内容从内核缓冲区拷贝到用户缓冲区;而write(2)这个操作,需要把用户缓冲区的内容拷贝到内核缓冲区。我们不需要对文件内容进行处理的情况下,却多用了一道拷贝工序,直接造成的开销有多了一次的上下文切换,以及浪费了缓冲区的内存。

为什么不把拷贝到用户空间的这一道工序去掉了,内核的两个缓冲区直接拷贝就好了。目前流行的做法是用sendfile(2),在Linux里面的作用是把数据从一个文件描述符直接拷贝到别一个文件描述符。但是sendfile(2)的实现,好像是各个系统都有差异,使用的时候需要注意一下,在使用的时候最好先查一下手册。

但是从内核缓冲区到内核缓冲区的“零拷贝”,还不算真的完全没有拷贝。真正的“零拷贝”需要硬件支持。在具备Gather操作的DMA硬件支持下,内核只需要修改内部文件描述符的指向,然后等待硬件自动执行Gather操作,把数据取走。

Written by liuw

五月 18th, 2010 at 8:43 下午

Posted in Tech, UNIX-like, 分享

Tagged with ,

好文传递:How To Become A Hacker

without comments

有个小计划,就是尽量能做到每日抽出一段时间来读点有趣的文章,把链接放在这,然后再记一下自己的想法。

其实前面也有写,比如说公众演说技巧Memory Barriers的一些小结,应该都算是这类文章。但是直到今天,才真正萌发起这样的想法,能否把这个事情做得系统点,坚持下来,对自己也有好处。假如能做到有周期有规律,那自然是最好的,但是目前的情况是比较难实现的了,只能说有看就写了。

今天看的是Eric Steven Raymond写的How To Become A Hacker

简要摘录一下,Hacker应有的态度是:1. 世界上有大量有趣的问题是等待解决的;2. 没有人应该需要解决同样的问题两次;3. 拒绝沉闷的事情;4. 自由;5. 信念不能代替能力。

ESR还提到Hacker的基本技能自然是过硬的编程能力、基本功等等。

个人觉得后面对于社区的讨论也比较有价值。在有了前面的基本能力之后,就必须要融入社区之中,从社区获取,也为社区作贡献。个人觉得,这是Hacker和一般程序员的最大的区别。在我的印象中,Hacker们总是热衷于各种各样的争吵,呵呵。

Written by liuw

五月 16th, 2010 at 1:08 下午

Posted in 分享

Tagged with

关于开发管理的一些思考

with 2 comments

目前我们组内对开发的管理还十分初级。Wiki刚刚开始用,没有用trac、bugzilla之类的工程控制工具,只用了Mercurial进行了版本控制,交流主要靠每周例会。

学生开发组织,我觉得是比较难做到规范化开发的。一则因为外围工具的学习成本高,项目等不及;二则因为学生组织与公司不一样,至少不会因为开发流程不规范而被fire掉。在学校里面,老板要的是效果、演示,不管你用什么方法去做。

在目前开发的过程中,也出现了一些问题。原来使用Mercurial的原因,就是一个DVCS看起来更适合我们项目。组内对于Xen都处于摸索的状态,需要做大量的实验,会产生大量的commit。使用svn的话,必须等到特性稳定之后才可以commit,不合适。

当然,实际使用过程中,我们仍然保持有一个“主”版本库,其中含有稳定的代码。原则上来说,主版本库更新之后,每个人都应该与之保持同步。

现在问题来了,大家都没有及时与主版本进行同步,所以现在diverge得越来越远了。到时再想merge的话,conflict将是一个很头痛的问题。但是用svn的话,这种情况就不会出现了。

当然,只要DVCS的使用者注意同步,这也不会是什么大问题。CVCS的好处是,这个步骤是强制的,不能有意无意的略过,大家必须保持一致。

现在我已经预见到以后merge会遇到的痛苦了。

UPDATE:和小胖聊了一下,发现其实还是工作流的问题。人是活的,工具是死的。工作流做好了,活用branch,CVCS也可以解决“不稳定的新特性commit”的问题。所以最现实的,就是把工作流做好。我们缺的不是工具,而是完整的工作流。

Written by liuw

四月 19th, 2010 at 11:18 上午

Python Daemonize

without comments

Code copied from Xen.


def daemonize():
    # Detach from TTY

    # Become the group leader (already a child process)
    os.setsid()

    # Fork, this allows the group leader to exit,
    # which means the child can never again regain control of the
    # terminal
    if os.fork():
        os._exit(0)

    # Detach from standard file descriptors, and redirect them to
    # /dev/null or the log as appropriate.
    # We open the log file first, so that we can diagnose a failure to do
    # so _before_ we close stderr
    try:
        parent = os.path.dirname(XEND_DEBUG_LOG)
        mkdir.parents(parent, stat.S_IRWXU)
        fd = os.open(XEND_DEBUG_LOG, os.O_WRONLY|os.O_CREAT|os.O_APPEND, 0666)
    except Exception, exn:
        print >>sys.stderr, exn
        print >>sys.stderr, ("Xend failed to open %s. Exiting!" %
                             XEND_DEBUG_LOG)
        sys.exit(1)

    os.close(0)
    os.close(1)
    os.close(2)
    if XEND_DEBUG:
        os.open('/dev/null', os.O_RDONLY)
        os.dup(fd)
        os.dup(fd)
    else:
        os.open('/dev/null', os.O_RDWR)
        os.dup(0)
        os.dup(fd)
    os.close(fd)

    print >>sys.stderr, ("Xend started at %s." %
                         time.asctime(time.localtime()))

See more about Process Group from Wiki.

A note from ‘man 2 setsid’

A child created via fork(2) inherits its parent’s session ID. The ssession ID is preserved across an execve(2).

A process group leader is a process with process group ID equal to its PID. In order to be sure that setsid() will succeed, fork(2) and _exit(2), and have the child do setsid().

Written by liuw

三月 24th, 2010 at 10:34 下午

Python @staticmethod vs @classmethod

with one comment

There are two predefined decorators in Python: @staticmethod and @classmethod.

I’m somewhat confused. For someone who has a shallow Java background, static method and class method are just the same.

See this for short.

And see Python PEP and manuals for details.

Written by liuw

三月 14th, 2010 at 12:21 上午

Borg Pattern in Python

without comments

During the search about Python singleton pattern, I discovered [ActiveState recipe 66531].

In this Borg Pattern, instances’ internal dictionary points to the same place, so all instances share the same state. Also, [Alex Martelli], the author of this recipe, has a interesting comment: Borg Pattern != Singleton Pattern. I admit that I confuse Borg Pattern with Singleton Pattern at first.

It’s NOT a singleton object… it doesn’t NEED to be! This is a completely and deeply different pattern from Singleton, and thus I find the latest comment totally off-base. Singleton focuses (wrongly) on object IDENTITY: that’s what the Gof4 focuses on, that’s what “EffC++” focuses on, etc, etc. But we don’t really care about identity most of the time, but about state and behavior.

The “Borg” design pattern IS quite possible in C++ (just a bit more laborious, but not much) and was indeed once written up in C++ Report (but I forget the name it was given then and can’t find my old issue): you get as many objects as you want, with separate identities but all sharing state and behavior. That’s the POINT! Just about all the practical USEFULNESS of Singleton without the troublesome identity-issue that Singleton tends to give.

Here’s the code, quite simple, cool.


class Borg:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state

Written by liuw

三月 13th, 2010 at 11:35 下午

Posted in Programming, 分享

Tagged with , ,

Python Singleton Pattern Implementation

without comments

There are three ways to get this job done.

1 Module Implementation

The easiest implementation is to use a module scope variable. Module is persistent and shared by all references through its life time, no matter how it is renamed or modified.

See this [stackoverflow post on Python singleton]. Xen Xend makes use of this Python feature, which exposes a moudule function as interface to singleton class.

2 Class Implementation

The second implemention is class implementation, it’s not so convenient because Python does not support static variable itself. It certainly requires some tricks to get this job done.

According to this [ActiveState recipe 52558], it’s all about create exactly one instance of some class. But this is not so convient cause it uses automatic delegation.

3 Function Argument Default Value Implementation

Also another trick. The default value of a function argument is initialized only once and persistent. See the following example.


def f(x, l=[]):
    l.append(x)
    return l

This is least recommended.

Written by liuw

三月 13th, 2010 at 11:27 下午

How to Copy to / Paste from Clipboard for Emacs

without comments

It feels easy to copy/paste within Emacs, but sometimes we need to transfer data between Emacs and other programs. Many window systems have some sort of “clipboard” to do simple transfers.

Emacs can also make use of system clipboard. See following commands.


clipboard-kill-region
clipboard-kill-ring-save
clipboard-yank

I’ve been looking for them for years, gee.

Written by liuw

二月 25th, 2010 at 11:21 下午

Posted in Programming, 分享

Tagged with , , ,