Random Tech Thoughts

The title above is not random

多线程程序的 IO

最近碰到一个跟多线程程序 IO 有关的 bug。看来即使 stdio 函数是线程安全的,在处理多线程 IO 的时候还是得当心。

程序有两个线程,一个线程用 fprintf 输出大量日志,另一个线程在特定条件下会调用 exit 退出。调试半天以后发现在问题出在 exit 时,日志文件会出错。(比如少掉一个换行,出现重复的 log entry 或者丢掉一些 log 等等。)

原以为 exit 时,所有输出都会被 flush 出去。但回头想想,如果 fprintf 正在执行,而整个进程退出了,那缓存着的日志自然不能保证完全写出。(不知道为啥一开始调试的时候没想到。。。)

如果执行 exit 之前由该线程调用 fflush 还是会出同样的问题,因为另外一个线程可能在 fflush 执行完,而 exit 执行前输出日志。

最后的解决办法是在遇到特定退出条件时设置标志变量,输出日志的线程看到这个标志设置后调用 fflush 并且不再输出日志。

Macbook Pro 上 SD 卡只读的问题

以前工作地好好的,今天突然发现 SD 卡在我的 mbp (OS 10.6.5) 的内置读卡器上只读,同学的机器上 (OS X 10.6.4) 却是可写的。

用 fat32 的优盘排除了文件系统的问题,借了别人的外置读卡器来,结果还是只读。。。

Google 下,结果很给力。这个帖子有个看起来不太很靠谱但真的能行的解决办法,把 SD 卡的 lock 开关扳到 lock 和 unlock 中间的位置,然后一切都正常了。。。

从在外置读卡器症状相同来看,应该是软件而不是内置读卡器的问题。正在龟速下载升级包,更新到 10.6.6 之后再看看是不是就正常了。

Update on 2011.1.11:

升级到 10.6.6,问题没有解决…… 10.6.4 的时候还是正常的,不过现在不能确定是软件还是硬件的问题,等同学的机器升级以后再测试下。

Use Dnsmasq to Setup a Local DNS Server

In a previous article, I introduced how to use PowerDNS to setup a local DNS server for my lab. But it turns out that Dnsmasq is a better choice for the task. It’s much much easier to setup since it’s designed for this, the web page of Dnsmasq states that:

It is designed to provide DNS and, optionally, DHCP, to a small network … It can serve the names of local machines which are not in the global DNS … Dnsmasq is targeted at home networks using NAT … but would be a good choice for any smallish network (up to 1000 clients is known to work) where low resource use and ease of configuration are important.

Kindle 3 Wi-Fi Connection Problem

Seems that many people have problems with Kindle wi-fi connection on some routers. As this replay in a thread on amazon’s customer discussion forum suggests, kindle 3 verifies internet connection before considering the Wi-Fi connection successful.

PowerDNS Setup on Debian Lenny Using Bind or Sqlite3 Backend

Update on May 4th, 2011: Sebastian Andrzej Siewior writes a C program that can make incremental SQL updates for sqlite based on a bind zone file. This would be useful if you are managing a large domain using sqlite.

As the number of computers in my lab grows, it’s better to use a local DNS server. I’ve done this with bind when I’m an undergraduate student, but I forget almost all about bind’s configuration. PowerDNS seems to be promising: good performance, flexible, easy to use. It has a built-in web server monitor, web based zone management tool Poweradmin. So I decide to use it this time.

Update: When troubleshooting the problem in the final note, I find that dnsmasq is more suitable for my purpose. It reads /etc/resolv.conf and /etc/hosts for it’s DNS configuration. I will write more about this in another article.

Glibc Stdio Functions Are Thread-safe

根据 man flockfile,stdio 的函数是线程安全的。FILE 里其实包含有 lock,stdio 函数会执行加锁操作。

需要的时候用户可以使用不加锁的 stdio 函数(例如 fwrite_unlocked,参考 man unlocked_stdio),自己调用 flockfile 等函数来执行加锁解锁操作。

以前我还自己写测试来判断 glibc 的 stdio 函数是否线程安全,今天才看到 man page 里的说明。

PS: 今天还碰到个 uint16_t 和 int 做比较出错的 bug。。。我用宏定义了一个常数 -1,C 可以通过 -1UL 来指定 unsigned long int 1,但是没有办法指定为 uint16_t,最后只好换成用 const uint16_t 定义常量就搞定了。

const 的限制在于不能用它定义的其他常数的算数运算来定义其他的 const,这点限制不太方便。

Git Http/ssh/git Protocol Through HTTP Proxy

My university network only gives us access to CERNET. This causes trouble when I need to access resources outside CERNET. Luckily, many applications support HTTP proxy, and git works with proxy very well.

Using http protocol to access a git repository is the most simple way. Set an environment variable is enough. “export http_proxy=proxyhost:proxyport”. If your proxy requires authentication, “export http_proxy=http://username:passwd@proxyhost:proxyport” should work. (I didn’t tested the later one. man libcurl-tutorial if you want to see the magic behind this syntax.)

For ssh/git protocol, we need to create an http tunnel using either corkscrew/socat/connect-proxy (I used corkscrew). Note that the proxy server needs to allow connect to the destination port. (If your proxy server only allows connect to 80/443 port, this will not work.)

To use git protocol, you can find instructions here.

To use ssh protocol, ssh magic is needed. Take a look here.

推荐 Flos Http Proxy

发现 ccproxy 不支持 ipv6 的二级代理,搜了下发现了这个 flos http proxy。

很简单小巧的程序,配置起来比 ccproxy 也简单很多,win7 也能用。

用户认证方面比较弱,只能配置一个用户名密码,不过对我来说够用了。

没找到官方网站,google 下找个能下载的就行,我用的版本是 1.0a7。

给个下载的链接

Release of COREMU

PPI 半年了,COREMU 终于在 sourceforge 上发布了。COREMU 的主要工作是王肇国(我本科同学,现在也在 PPI)完成的,其他开发人员可以在 PPI 的 COREMU 项目主页上找到。

简单说 COREMU 把 Qemu 并行化了。原先的 Qemu 只能使用一个物理 CPU,COREMU 则是使用多个物理 CPU 来模拟虚拟 CPU。

目前 COREMU 只能在 x86_64 Linux 系统上运行,支持模拟 x86_64 和 ARM Cortex A9 MPCore。在我们的测试用的 4 核系统上能模拟出 255 核,并且成功运行 Linux。

在这里宣传一下。半年来一直与 bug 做斗争,现在终于发布了,希望 COREMU 能够发挥些作用。有什么问题的话欢迎发邮件联系 :) cyfdecyf at gmail dot com

RDC 远程 64bit Win 7 没声音的解决方法

找的有点辛苦。。。装个 hotfix 就可以了,注意不同版本的系统要下不同的 hotfix。

话说微软的 hotfix 要用起来还真麻烦,邮件申请,还要用密码解压。。。好在很快能搞定

另外装了这个 hotfix 以后 CoRD 还是没声音,只好用 RDC 了。