I want to be a complete engineer - technical genius and sensitive humanist all in one!
Wednesday, September 2, 2009
ubuntu上使用scim和gajim技巧两则
Gajim 0.12.1 says that he can’t run with root privileges. This is new feature that appeared in Fedora 10. In earlier versions it was normaly running under root. Anyway let’s turn off this feature in case that you need to run Gajim under root.
Open /usr/bin/gajim with your favorite text editor, find 24th line and just comment out this part of code:
if test $(id -u) -eq 0; then
echo "You must not launch Gajim as root, it is INSECURE"
exit 1
fi
After commenting it should look like
#if test $(id -u) -eq 0; then
# echo "You must not launch Gajim as root, it is INSECURE"
# exit 1
#fi
Save file. Thats it.
-------------------------------------------------------------------------------
root 下的中文显示问题解决方法如下
9.04中选择root进入系统,无法显示中文桌面,而且选择中文后也没有作用, 而在/etc/environment中,"LANGUAGE=zh_CN:zh:en_US:en"是不用修改的,改后有没问题还不清楚。
解决方法:
在root下打开隐藏文件.profile。
修改root下隐藏文件.profile,把最后的两行 LANG=C 和LANGUAGE=C改为如下所示后重启。
以下是修改过的。
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n
# Installed by Debian Installer:
# no localization for root because zh_CN.UTF-8
# cannot be properly displayed at the Linux console
LANG=C #修改此行为:LANG="zh_CN.UTF-8"
LANGUAGE=C #修改此行为:LANGUAGE="zh_CN:zh"
然后重启,选择以root进入系统,即可显示中文。
Monday, August 3, 2009
sendfile函数
显示如下:
SENDFILE(2) Linux Programmer’s Manual SENDFILE(2)
NAME
sendfile - transfer data between file descriptors
SYNOPSIS
#include
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
DESCRIPTION
sendfile() copies data between one file descriptor and another.
Because this copying is done within the kernel, sendfile() is more
efficient than the combination of read(2) and write(2), which would
require transferring data to and from user space.
in_fd should be a file descriptor opened for reading and out_fd should
be a descriptor opened for writing.
If offset is not NULL, then it points to a variable holding the file
offset from which sendfile() will start reading data from in_fd. When
sendfile() returns, this variable will be set to the offset of the byte
following the last byte that was read. If offset is not NULL, then
sendfile() does not modify the current file offset of in_fd; otherwise
the current file offset is adjusted to reflect the number of bytes read
from in_fd.
count is the number of bytes to copy between the file descriptors.
Presently (Linux 2.6.9): in_fd, must correspond to a file which sup‐
ports mmap(2)-like operations (i.e., it cannot be a socket); and out_fd
must refer to a socket.
Applications may wish to fall back to read(2)/write(2) in the case
where sendfile() fails with EINVAL or ENOSYS.
RETURN VALUE
If the transfer was successful, the number of bytes written to out_fd
is returned. On error, -1 is returned, and errno is set appropriately.
这些日子编写网络相关的程序,偶尔发现这个api,据说效率奇高。sendfile()在在两个文件描述符之间完成数据拷贝操作,该操作是内核中完成的,所以称为"零拷贝"。sendfile函数比起read和write函数高效的原因在于read和write是要把数据拷贝到用户应用层操作而sendfile不需要。
目前手头时间比较紧迫,抽空出来写个测试程序看看使用这个api与使用其他api到底效率差别有多大。
man手册分类
man手册分为以下几个章节:
(1)standard commands (标准命令)
(2)system calls (系统调用)
(3)library functions (库函数)
(4)special devices (设备说明)
(5)file formats (文件格式)
(6)games and toys (游戏和娱乐)
(7)miscellaneous (杂项)
(8)administrative Commands (管理员命令)
如需要查阅网络编程里面send系统调用的帮助信息,如果直接输入$man send则显示的是send命令的帮助信息,要想正确得到send系统调用的信息,需要$man 2 send 。
NOTE:man是通过手册的章节号来搜索帮助信息的。
Tuesday, July 21, 2009
Install XMMS on Ubuntu
We need to download the required packages to compile XMMS
# sudo apt-get install autotools-dev automake1.9 libtool gettext libasound2-dev libaudiofile-dev \
libgl1-mesa-dev libglib1.2-dev libgtk1.2-dev libesd0-dev libice-dev libmikmod2-dev libogg-dev \
libsm-dev libvorbis-dev libxxf86vm-dev libxml-dev libssl-dev build-essential make
Depending on your internet connection and your machine this may take some minutes.
Second Step:
Prepare the XMMS for compiling
Create a directory in your HOME directory:
# mkdir ~/build
Change the working directory to it:
# cd ~/build
Download XMMS sources:
# wget http://xmms.org/files/1.2.x/xmms-1.2.11.tar.gz
Unpack it:
# tar xvf xmms-1.2.11.tar.gz
Change the working directory to the source directory:
# cd xmms-1.2.11/
Third Step:
Compiling it:
This generates the necessary files, and checks your system:
# ./configure --prefix=/usr
The actually compiling
# make
Fourth Step:
Install it:
# sudo make install
After install we no longer need the source directory:
# cd
# rm -rf ~/build
That's it, now we can run XMMS: press ALT+F2 write xmms there and hit enter and enjoy your music.
NOTE: many people say that XMMS is old, buggy,no UTF-8 support, etc. Yes maybe all is true, but it's an audio player not a media library organizer, so it plays music and you listen, that's all and it does that job.
UPDATE:
Flac Plugin
We need to get the build dependencies
# sudo apt-get build-dep flac
You already know what's this ;)
# mkdir ~/build
# cd ~/build
Get flac's sources
# apt-get source flac
Another well known step:)
# cd flac-1.2.1
# ./configure
# make
It's enough to copy the plugin, not to install the whole flac stuff, this is good if will be flac update, note, an update wont break the plugin.
# cp src/plugin_xmms/.libs/libxmms-flac.so ~/.xmms/Plugins
# cd
# rm -rf ~/build
linux清理内存命令
Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
To free pagecache:
- echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
- echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
- echo 3 > /proc/sys/vm/drop_caches
As this is a non-destructive operation, and dirty objects are not freeable, the user should run “sync” first in order to make sure all cached objects are freed.
This tunable was added in 2.6.16.
the last:echo 0 > /proc/sys/vm/drop_caches
should operator in root !
Thursday, July 16, 2009
自旋锁spinlock
自旋锁在同一时刻只能被最多一个内核任务持有,所以一个时刻只有一个线程允许存在于临界区中。这点可以应用在多处理机器、或运行在单处理器上的抢占式内核中需要的锁定服务。
顺便介绍下信号量的概念,因为其和自旋锁的用法有颇多相似之处。
Linux中的信号量是一种睡眠锁。如果有一个任务试图获得一个已被持有的信号量时,信号量会将其推入等待队列,然后让其睡眠。这时处理器获得自由去执行其它代码。当持有信号量的进程将信号量释放后,在等待队列中的一个任务将被唤醒,从而便可以获得这个信号量。
自旋锁和信号量对比
在很多地方自旋锁和信号量可以选择任何一个使用,但也有一些地方只能选择某一种。下面对比一些两者的用法。
表1-1自旋锁和信号量对比
| 应用场合 | 信号量or自旋锁 |
| 低开销加锁(临界区执行时间较快) | 优先选择自旋锁 |
| 低开销加锁(临界区执行时间较长) | 优先选择信号量 |
| 临界区可能包含引起睡眠的代码 | 不能选自旋锁,可以选择信号量 |
| 临界区位于非进程上下文时,此时不能睡眠 | 优先选择自旋锁,即使选择信号量也只能用down_trylock非阻塞的方式 |
自旋锁与linux内核进程调度关系
以上表第3种情况(其它几种情况比较好理解)为例,如果临界区可能包含引起睡眠的代码则不能使用自旋锁,否则可能引起死锁。
那么为什么信号量保护的代码可以睡眠而自旋锁就不能呢?
先看下自旋锁的实现方法吧,自旋锁的基本形式如下:
spin_lock(&mr_lock);
//临界区
spin_unlock(&mr_lock);
跟踪一下spin_lock(&mr_lock)的实现
#define spin_lock(lock) _spin_lock(lock)
#define _spin_lock(lock) __LOCK(lock)
#define __LOCK(lock) \
do { preempt_disable(); __acquire(lock); (void)(lock); } while (0)
注意到“preempt_disable()”,这个调用的功能是“关抢占”(在spin_unlock中会重新开启抢 占功能)。从中可以看出,使用自旋锁保护的区域是工作在非抢占的状态;即使获取不到锁,在“自旋”状态也是禁止抢占的。了解到这,我想咱们应该能够理解为 何自旋锁保护的代码不能睡眠了。试想一下,如果在自旋锁保护的代码中间睡眠,此时发生进程调度,则可能另外一个进程会再次调用spinlock保护的这段 代码。而我们现在知道了即使在获取不到锁的“自旋”状态,也是禁止抢占的,而“自旋”又是动态的,不会再睡眠了,也就是说在这个处理器上不会再有进程调度 发生了,那么死锁自然就发生了。
咱们可以总结下自旋锁的特点:
● 单处理器非抢占内核下:自旋锁会在编译时被忽略;
● 单处理器抢占内核下:自旋锁仅仅当作一个设置内核抢占的开关;
● 多处理器下:此时才能完全发挥出自旋锁的作用,自旋锁在内核中主要用来防止多处理器中并发访问临界区,防止内核抢占造成的竞争。
linux抢占发生的时间
最后在了解下linux抢占发生的时间,抢占分为用户抢占和内核抢占。
用户抢占在以下情况下产生:
● 从系统调用返回用户空间
● 从中断处理程序返回用户空间
内核抢占会发生在:
● 当从中断处理程序返回内核空间的时候,且当时内核具有可抢占性;
● 当内核代码再一次具有可抢占性的时候。(如:spin_unlock时)
● 如果内核中的任务显式的调用schedule()
● 如果内核中的任务阻塞。
基本的进程调度就是发生在时钟中断后,并且发现进程的时间片已经使用完了,则发生进程抢占。通常我们会利用中断处理程序 返回内核空间的时候可以进行内核抢占这个特性来提高一些I/O操作的实时性,如:当I/O事件发生的是时候,对应的中断处理程序被激活,当它发现有进程在 等待这个I/O事件的时候,它会激活等待进程,并且设置当前正在执行进程的need_resched标志,这样在中断处理程序返回的时候,调度程序被激 活,原来在等待I/O事件的进程(很可能)获得执行权,从而保证了对I/O事件的相对快速响应(毫秒级)。可以看出,在I/O事件发生的时候,I/O事件 的处理进程会抢占当前进程,系统的响应速度与调度时间片的长度无关。
更详细的内容可以参考Robert Love所著的《Linux kernel Development》SE, 经典著作啊,很值得一看!
Wednesday, July 15, 2009
不要脸的中国社会和不要脸的中国人
中国的不要脸首先体现在中国人价值观和是非观的丧失。国人社会价值的衡量标准就是钱的多寡;判断是非的标准就是是否符合经济效益;一个人社会地位的高低取决于其拥有财富的多少,而不管这些财富是如何来的。不要脸的中国人一边不断地诅咒这那些拥有财富的人一边盘算着如何效仿他们以便挤入他们的行列。
在中国最不要脸的组织就是我们的政府,榨取农民几十年的血汗,一旦免除了农民的农业税就把自己装扮成一个施恩者。整天在各种场合大谈关心农民和农民工就是迟迟不动林林种种的不合理体制,甚至不承认这些不合理。“以租待征”怎么就不行了,为什么到了现在还要喝我们农民的血?农民的金融合作就这么危险吗?
其次不要脸的组织是国有垄断企业,凭借着行政授予的垄断权大肆榨取消费者的血汗。更让人气愤的这些人一边喝着我们的血还一边一本正经地挖苦我们太不理解他们的困难。不是某位石油大亨的代言人说了嘛:“中国石油企业的利润率还不到50%”,我不知道这个人是不是白痴,综观发达国家的石油企业,还有谁的利润率能达到10%!无知的言论透露出的是这些部门的猖狂和无耻。有银行系统的代言人说话了:“银行员工的工资之所以高是因为银行属于高危险行业”。奶奶的!不要脸是不?要是这么危险你们怎么不和高空作业的农民工换换,还挤破头地把自己的小子闺女七大姑八大姨家的孩子往“火坑”里推?公路局也跟着不要脸,不就是坐着收费吗,凭什么你们一个月工资就是别人工资的十倍?电信局坐不住了,迫不及待地加入了不要脸的行列,无线上网就这么难吗?电信就是不发展,为什么?控制啊!要说不要脸的可别忘记我们的邮电局,不要脸的商业银行退出农村了,这个不要脸的进来了,光吸收存款不发放贷款,贫穷的中国农民又一次在为中国的城市作贡献了。
还有一个不要脸的组织就是大部分民营企业,资本家的血腥在中国的最近20多年已经把中国人浸透了,黑工厂、黑煤窑、黑砖窑、黑中介,所有的行业都有透着铜臭和血腥的组织。对于民营企业,除了个别的,有几个是干净的?榨取劳动者的血汗,规避法制,偷税漏税,使得朱总理为某个会计学院题词时实在想不起来其他更好的话,没办法才写了个“不做假帐!”这个就不说了,地球人都知道了!以至于美国工会过来为中国工人讨说法,丢人啊,中国政府,中国人!!
要说不要脸的,还有我们自己。胆小怯懦,目送英雄归西;狼狈为奸,冷眼事态炎凉;不负责任,事不关己高高挂起;极度自私,人不为己天诛地灭!
十年文革把中国旧有的价值观念砸的粉碎,改革开放使得国外的思想潮流蜂拥而至。中国是个没有宗教信仰的国家,中华民族是个没有精神寄托的民族。粉碎了旧有的价值体系,新的价值体系根本没有建立起来,每个人都在迷惘,都在彷徨,吃饱了肚子的人精神空虚的如同行尸走肉,于是,李洪志来了,说跟他走,搞什么真善美,于是,邪教诞生了!
他妈的,神奇的国度!神奇的事件!