Monday, January 26, 2009

Setup Wireless

iwl3945

ifconfig wlan0 up
iwconfig wlan0 essid TP-LINK key ***
dhcpcd wlan0

Sunday, January 18, 2009

Debian and KVM

在FreeBSD里折腾了几天,我又装回Debian了。这次是Debian独大,而FreeBSD和Windows则通过虚拟机运行。这样我就可以一边开着FreeBSD测测代码,一边开着Windows写写Word,还能在Debian里上上网。为什么选择Debian呢?桌面环境成熟,网上资料多,有KVM这个硬件级虚拟技术(需要CPU支持)。

Debian安装越来越简化,除了基本系统,手动安装的只有fglrx和alsa-utils。系统时间还是不对,而且安装中的GMT选项也不见了,好在设置起来不麻烦(这里)。

关掉shell下的铃声:
$cat >> .inputrc
set bell-style none

关掉vim里的铃声:
$cat >> .vimrc
set vb
set t_vb=

KVM需要CPU支持Intel VT或者AMD-T技术。执行下面指令来查看你的CPU是否支持:
$egrep '^flags.*(vmx|svm)' /proc/cpuinfo

KVM的安装也很方便,主要参考在这
#aptitude install kvm qemu
加入kvm组:
#adduser username kvm
创建虚拟磁盘:
$qemu-img create -f qcow2 xxx.img 10G
加载ISO进行虚拟系统安装:
$kvm -hda xxx.img -cdrom boot.iso -boot d -m 1024

虚拟系统的网络安装比较烦,请待下回分解。

Friday, January 09, 2009

FreeBSD on T60

这两天尝试了一下在本本上装FreeBSD7.1,基本上照着官网上的Handbook来就行了。相关软件都是通过Ports Collession安装的,比较费时间就是。

Ports
搜索软件:
$cd /usr/ports; make search name=XXX
升级软件用portupdate;
清理Ports使用的磁盘空间用portsclean;
升级Ports:
#portsnap fetch
#portsnap extract
#portsnap update

Xorg
生成默认配置(xorg.conf.new):
#Xorg -configure
只有鼠标部分要改一下以同时支持小红点和USB鼠标:
InputDevice "Mouse0" "CorePointer"
InputDevice "Mouse1" "SendCoreEvents"
Section "InputDevice"
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/psm0"
Option "ZAxisMapping" "4 5 6 7"
EndSection
Section "InputDevice"
Identifier "Mouse1"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/sysmouse"
Option "ZAxisMapping" "4 5 6 7"
EndSection

其中Mouse0对应小红点,Mouse1对应USB鼠标。注意CorePointer只能有一个。如果不知道Device项写啥,可以参考dmesg的输出。改好后复制到/etc/X11/xorg.conf即可。

~/.xinitrc的内容是:
xset b off #关闭X中的beep
scim -d #后台启动scim
exec startxfce4 # 启动Xfce4

中文字体我用的是国乔(kcfonts)和文鼎(arphicttf)。

Scim拼音输入法
~/.profile中添加:
LANG=en_US.UTF-8; export LANG
LC_CTYPE=zh_CN.GBK; export LC_CTYPE
XMODIFIERS=@im=SCIM; export XMODIFIERS
GTK_IM_MODULE=scim; export GTK_IM_MODULE

声音和无线网络模块

手动加载声音模块:
#klbload snd_hda
启动自动加载,/boot/loader.conf添加:
snd_hda_load="YES"

if_wpi_load="YES"
wlan_load="YES"
wlan_amrr_load="YES"
firmware_load="YES"
wpifw_load="YES"
legal.intel_wpi.license_ack=1

扫描无线网络,第一次用:
#ifconfig wpi0 up scan
以后查看无线网络:
#ifconfig wpi0 list scan

网络设置
/etc/rc.conf中添加以下项:
hostname="my.domain.com" # 主机名
ifconfig_em0="DHCP" #em0对应Intel PRO 1000 网卡
#ifconfig_em0="inet 192.168.25.249 netmask 255.255.254.0" # 手动设置ip和子网掩码
ifconfig_wpi0="DHCP" # wpi0对应Intel Pro/Wireless 3945ABG 无线网卡
allscreens_kbdflags="-b off" #关闭控制台的beep
#defaultrouter="192.168.0.1" # 默认网关,使用DHCP时不用设

/etc/resolv.conf中设置DNS:
nameserver 192.168.0.1

/etc/hosts中添加主机名:
::1 localhost localhost.my.domain my.domain.com
127.0.0.1 localhost localhost.my.domain my.domain.com

最后一列就是主机名。

Monday, December 22, 2008

Baby Smallka Happy Birthday

昨天又生日了,谢谢小鱼的蛋糕,还陪我去吃寿司~~

顺便做个总结吧。

7月进公司,培训想走马灯一样,大多都没记住。要说印象深的就是拓展训练最后那个爬墙还有番禺水上乐园。

9月加入蚂蚁工作室,发现除了小xinxin和我,大家都很能吃辣。程序员这边大家的共同点挺多的,比如宅啦。聊聊技术也比较有意思,和他们相比,感觉我学的太浅了。老大很喜欢看书,China-pub,蓝泉都是大把大把的买,有时还每人送几本。

11月份开始慢慢参与了一些项目相关的工作,按老大的话说就是看书读代码的好日子快过完了,等着被他压榨吧~~

上周算是顺利转正了,感谢从找工以来到现在给过我关心和帮助的所有朋友!

不管是TC还是现在的公司,大多数时候写代码都是很枯燥的体力活,细心和耐心都是不可少的(不知道Google是什么样的,Savior记得告诉我哦)。而剩下那一小部分又往往是你想破头皮也写不出来的。因此总结两点:健康是本钱,有体力才能干活(去健身房不到5次的人);业余多充电,活到老学到老。

Tuesday, October 14, 2008

Diff UNIX Windows, Part 2

平台的差异不仅表现在网络方面:

1)UNIX下create函数创建新文件通常用644作mode。而Windows没有group和other的概念,因此得用600。

2)中的isalpha、isprint、isprint等函数。
以isalpha为例,该函数可以判断给定的字符是否属于alphabet character。如果传给isalpha函数小于-1的char类型实参,程序执行时将出错。这是由isalpha函数中的Assert语句引发的:_ASSERTE((unsigned)(c + 1) <= 256); 该语句的作为用是保证参数的取值范围是0~255。

When used with a debug CRT library, isalpha will display a CRT assert if passed a parameter that is not EOF or in the range of 0 through 0xFF. When used with a release CRT library, isalpha will use the parameter as an index into an array, with undefined results if the parameter is not EOF or in the range of 0 through 0xFF.

------ MSDN


分析可知,出错的原因与两次类型转换有关:

isalpha函数的形参类型是int,而实参是char类型,因此c将首先被向上转型为int,即integral promotion。不同编译器对转型的实现可能不同,即有的将char看作有符号数,有的则看作无符号数。VC编译器属于前者,因此当传入的char类 型实参小于-1时,相应c+1的值小于0。

在Assert语句中,c+1被强制转换为无符号数,由于c+1是负数,因此转型后的值必大于256(负数的符号位为1)。

解决办法是在调用isalpha函数前先将char强制转型为unsigned char。推荐阅读《C Traps and Pitfalls》相关章节。

3)snprintf函数中对NULL结束符的处理。
int _snprintf( char *buffer, size_t count, const char *format [, argument] ... );

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

------ MSDN


int snprintf ( char *str, size_t n, const char *format, ... );

It is similar to sprintf(3), except that n specifies the maximum number of characters to produce. The trailing null character i counted towards this limit, so you should allocate at least n characters for the string str.

------ man snprintf

Friday, September 26, 2008

Diff UNIX Windows, Part 1

前几天尝试着移植CTorrent-dnh到Windows上,主要是想了解一下平台的差异性以及BT的基本原理。后来发现改动量比预计的大,就放弃了。其中还是有不少收获,以后有机会一定要多读代码。

CTorrent
是C++实现的bt客户端,可以在各种UNIX系统上跑。2004年就停止更新了。而CTorrent-dnh作为加强版,修正了一些BUG并且增加了功能。原版结构比较简单,比较适合结合BT协议来阅读。

Windows 的API经常换,光是网络这块就已经有好几套了。而UNIX下最早的select/poll模式却一直用到了现在。虽然epoll,kqueue这些有更 好的性能,但是对于简单的BT客户端,select就够用了,至少CTorrent是这样的。Windows下也支持select模式,但是不遵守规则似 乎也是MS的惯例。下面收集了一些例证:

1)UNIX下SOCKET类型定义为int,而Windows下则是UINT_PTR。这样下面这段UNIX上正常的代码就不起作用了:

SOCKET maxfd = -1;
for (i=0; i maxfd) maxfd = sk[i];

因为UINT_PTR是无符号的,因此-1实际上是最大值。

2)其实不用为上面的情况担心,因为Windows直接忽视select函数的第一个参数:)

3)跨平台移植代码最怕的不是缺了什么函数,而是函数相同却功能却不同。以select函数为例。

Berkeley-derived implementations (and POSIX) have the following two rules regarding select and nonblocking connects:

  1. When the connection completes successfully, the descriptor becomes writable (p. 531 of TCPv2).

  2. When the connection establishment encounters an error, the descriptor becomes both readable and writable (p. 530 of TCPv2).

------ UNIX Networking Programming Volume 1

If a socket is processing a connect call (nonblocking), failure of the connect attempt is indicated in exceptfds (application must then call getsockopt SO_ERROR to determine the error value to describe why the failure occurred).
------ MSDN

应该说Windows的这种改变并非不合理,因为在UNIX下exception fdset名不符实,用处太少,以至于经常被设为NULL而忽略。但这种既不是修BUG又不提高性能的改变却是以牺牲兼容性为代价的。

4)UNIX 下descriptor是通用概念,不仅仅是socket,因此select可以同时管理不同类型的descriptors。而Windows的 select只能接受SOCKET类型。这也是CTorrent-dnh难以移植的原因之一,它的select还管理标准输入输出。

5)UNIX的errno不是线程安全的,因此Windows使用WSAGetLastError函数代替。注意errno仍然存在,但是意义已不同了。

6)getsockopt函数的optval参数在UNIX下是int*类型,WINDOW下则是char*类型。这个比较想不通。

7)Windows下在使用网络API前后必须分别调用WSAStartup和WSACleanup函数。