Archive for January, 2009

wine 了一把《红警2》

January 24th, 2009

昨天和几个哥们出去玩去,本来说打死也不去网吧的结果最后还是跑到网吧去了,到了网吧后我发现我只有《红警》能玩,于是又疯狂了一把。回去后觉得意犹未尽,干脆在linux上装了个wine跑一个来玩,没想到wine 1.1.12对《红警2》支持那么好,图像音效什么的都很棒!就还有两个小问题:1. 速度有点慢;2. 每次鼠标到左上角的时候总是暂时“退出”该窗口,怀疑是个bug……

唉,想当年上高一的时候我对《红警2》那个疯狂啊~!当时我还搞到一张正版的《红警2》,上面录像很全,我认认真真地看了一遍,而且后来硬是把任务一关一关地都过了一遍。哎,往事不堪回首矣~!

reentrant,thread-safe 和 async-signal-safe

January 21st, 2009

先上定义吧,POSIX对它们的定义分别是:

Reentrant Function

A function whose effect, when called by two or more threads, is guaranteed to be as if the threads each executed the function one after another in an undefined order, even if the actual execution is interleaved.

Thread-Safe

A function that may be safely invoked concurrently by multiple threads. Each function defined in the System Interfaces volume of IEEE Std 1003.1-2001 is thread-safe unless explicitly stated otherwise.

Async-Signal-Safe Function

A function that may be invoked, without restriction, from signal-catching functions. No function is async-signal-safe unless explicitly described as such.

可重入我们都清楚,顾名思义,就是可以重新进入,进一步讲就是,用相同的输入,每次调用函数一定会返回相同的结果。这就是可重入。wikipedia上有更严谨的定义:

* Must hold no static (global) non-constant data.
* Must not return the address to static (global) non-constant data.
* Must work only on the data provided to it by the caller.
* Must not rely on locks to singleton resources.
* Must not call non-reentrant computer programs or routines.

然后是线程安全,从定义上看,它仅要求了可以安全地被线程并发执行。这是一个相对较低的要求,因为它内部可以访问全局变量或静态变量,不过需要加锁,也就是说,只要是在线程可控之中的,每次调用它返回不同的结果也没关系。到这里我们可以看出:可重入函数一定是线程安全的,而反之未必。wikipedia上也写道:

Therefore, reentrancy is a more fundamental property than thread-safety and by definition, leads to thread-safety: Every reentrant function is thread-safe, however, not every thread-safe function is reentrant.

例子,有很多,最出名的莫过于strtok(3),我们认识可重入这个概念就是从它开始的,它内部适用了静态变量,显然是不可重入的(它的可重入版是strtok_r(3))。其次应该是malloc(3),嘿嘿,其实也很明显,我就不多说了。但是,strtok(3)不是线程安全的,而malloc(3)是。

还有一个概念我们不常碰到,那就是异步信号安全,它其实也很简单,就是一个函数可以在信号处理函数中被安全地调用。看起来它似乎和线程安全类似,其实不然,我们知道信号是异步产生的,但是,信号处理函数是打断主函数(相对于信号处理函数)后执行,执行完后又返回主函数中去的。也就是说,它不是并发的!

一个函数,它访问了全局变量,那么它就是不可重入的,不过我们可以把它变成线程安全的,加上锁就可以,但是这种方法并不会把它变成异步信号安全的,而几乎可以肯定的是,使用了锁的一定不是信号安全的(除非屏蔽了信号,显然),信号完全可以在锁加上后解开前到来,然后就很可能形成死锁…… 这里有个很好的例子。所以,可重入的函数也一定是异步信号安全的,而反之未必。可以参考IBM上一篇不错的文章

关于异步信号安全的函数列表可以参考man 7 signal;关于线程安全的函数列表可以参考APUE第12.5节 ;关于可重入函数列表,可参考APUE第10.6节。另请参阅

用 sigaction(2) 的原因

January 20th, 2009

Linux man page中提到:

The behavior of signal() varies across Unix versions, and has also varied
historically across different versions of Linux. Avoid its use: use sigaction(2) instead.

The only portable use of signal() is to set a signal’s disposition to SIG_DFL or SIG_IGN. The semantics when using signal() to establish a signal handler vary across systems (and POSIX.1 explicitly permits this variation); do not use it for this purpose.

所以signal(2)的可移植性首先就差了一截子。还有就是,signal(2)也不能像sigaction(2)那样去屏蔽信号,POSIX中提到:

When a signal is caught by a signal-catching function installed by
sigaction(), a new signal mask is calculated and installed for the
duration of the signal-catching function (or until a call to either
sigprocmask() or sigsuspend() is made). This mask is formed by taking
the union of the current signal mask and the value of the sa_mask for
the signal being delivered unless SA_NODEFER or
SA_RESETHAND is set, And then including the signal being
delivered. If and when the user’s signal handler returns normally, the
original signal mask is restored.

sparse 的一个 bug

January 19th, 2009

今天用sparse的时候遇到一个bug:

error: unable to open ’stddef.h’

简单地搜了搜,发现debian上已经有人报告,最新的sparse的git树里已经解决了这个问题,可是Fedora还没有。于是只好自己编译,git-clone之后 make &&  make install PREFIX=/usr 就可以了。希望Fedora也尽快更新sparse。

Linux分区个数限制

January 18th, 2009

在linux上,对于IDE硬盘来说,分区个数限制在63个;而对于SCSI硬盘限制在15个。

我们可以直接在linux内核源代码中找到证据:

include/linux/ide.h

#define PARTN_BITS      6       /* number of minor dev bits for partitions */

次设备号也就决定了可挂载的IDE设备不会超过63个(2^6-1,之所以减一是因为hdX本身也会占用一个)。

SCSI也差不多,见 drivers/scsi/sd.c

#if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
#define SD_MINORS       16
#else
#define SD_MINORS       0
#endif

在内核文档中也可以看到,见Documentation/devices.txt :

For partitions, add to the whole disk device number:
0 = /dev/hd?          Whole disk
1 = /dev/hd?1         First partition
2 = /dev/hd?2         Second partition

63 = /dev/hd?63        63rd partition

8 block       SCSI disk devices (0-15)

Partitions are handled in the same way as for IDE
disks (see major number 3) except that the limit on
partitions is 15.

IDE, ATA, SCSI 等等

January 17th, 2009

关于硬盘的术语有一大堆,而且和很多别的计算机术语一样,界定不清,含义模糊。在这里简单澄清一下,但我不排除你看了之后会更糊涂。:^)

IDE:Integrated Drive Electronics 的缩写。最早的硬盘接口,后来被 ATA 标准所取代,准确地讲应该是 ATA-1。所以有时也被称为 IDE/ATA,可参见其历史介绍。后来,几乎和ATA-1同时出现的 EIDE(Enhanced IDE )又被规范化为 ATA-2。

ATA:Advanced Technology Attachment 的缩写,存储设备(并非仅限硬盘)的接口标准。有时也被称为 IDE。IDE 在 ATA 标准出现之前就有了,所以估计这个也是因为历史原因。不过根据wikipedia记载,这两个术语还是有些许不同之处的:

The terms “integrated drive electronics” (IDE), “enhanced IDE” and “EIDE” have come to be used interchangeably with ATA (now Parallel ATA). However the terms “IDE” and “EIDE” are at best imprecise. Every ATA drive is an “integrated drive electronics” drive, but not every “integrated drive electronics” drive is an ATA drive, as the term also correctly describes SCSI drives: They have the drive controllers on board and present the drive to the host as an array of blocks.

后来就都是 ATA 了。又因为 SATA(Serial ATA,即 ATA-7)的出现,所以老的 ATA 又都被称为 PATA(Parallel ATA)了。

SCSI:Small Computer System Interface 的缩写,另一个完全不同的标准,定义了内部的命令,协议和接口。SCSI更快,当然也更贵,多用在中,高性能服务器上,不过apple的mac也是最早使用它的计算机之一。新的SCSI标准,Serial Attached SCSI又被简称为SAS

SCSI 和前面的总起来并列为三个标准:PATA,SATA,SCSI。其中SATA应该是会继续流行的PC机硬盘接口。

HDD:Hard Disk Drive 的缩写,就是我们通常所说的硬盘,磁盘。

SSD:Solid State Drive 的缩写,通常我们称为固态硬盘。SSD耗电少,抗震性能好,但比起HDD来要贵一些,而且容量通常也不像HDD那么大。

RAID:Redundant Array of Independent Disks 的缩写,就是我们通常所说的磁盘阵列。RAID是对多块硬盘的组合,需要硬件支持(貌似也有从文件系统层支持RAID的,btrfs?)。根据不同的组合方式,RAID又分为RAID0到RAID7,详细参考这里

参考:

http://www.91wp.com/Article/jswz/200704/350.html

http://www.pcbuyerbeware.co.uk/IDE.htm

http://www.pcguide.com/ref/hdd/if/ide/over.htm

http://en.wikipedia.org/wiki/RAID

http://en.wikipedia.org/wiki/SCSI

http://en.wikipedia.org/wiki/AT_Attachment

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。
葡驻京办ICP备07006283号