Archive for the ‘Linux’ Category

Buffered I/O and non-buffered I/O

星期日, 10月 12th, 2008

实验需要对Flash Disk做无系统缓冲的I/O操作,顺便了解了一下Linux下的I/O.

Linux上的块设备的操作可以分为两类:

  • 第一类是使用C标准库中的fopen/fread/fwrite 系列的函数,我们可以称其为 buffered I/O。

具体的I/O path如下

Application<->Library Buffer<->Operation System Cache<->File System/Volume Manager<->Device

library buffer是标准库提供的用户空间的buffer,可以通过setvbuf改变其大小。

  • 第二类是使用Linux的系统调用的open/read/write 系列的函数,我们可以称其为 non-buffered I/O。

I/O Path

Application<-> Operation System Cache <->File System/Volume Manager<->Device

此外,我们可以通过设置open的O_DIRECT标志来实现Direct I/O(或者叫Raw I/O),即绕过OS Cache,直接读取Device ( that’s what we want^o^ ), 等于将OS cache换成自己管理的cache。不过,Linus在邮件列表中建议不这么做,而是使用posix_fadvice, madvice。[2]中表明Direct I/O比buffered I/O的性能高很多。

在使用O_DIRECT的注意buffer的address必须是block alignment的(i.e. 初始地址必须是boundary), 可以用posix_memalign()函数分配内存以得到这样的buffer。至于为什么要这样,与实现的mmap有关,参见[5].

参考:

  1. Linux: Accessing Files With O_DIRECT http://kerneltrap.org/node/7563
  2. Andrea Arcangeli , O_DIRECT Whitepaper http://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html
  3. A Trip Down the Data Path: I/O and Performance http://articles.directorym.net/_A_Trip_Down_the_Data_Path_IO_and_Performance-a894569.html
  4. Operating Systems System Calls and I/O http://articles.directorym.net/Operating_Systems_System_Calls_and_IO-a894576.html
  5. Linux Device Drivers, 2nd Edition, Chapter 13 mmap and DMA http://www.xml.com/ldd/chapter/book/ch13.html
  6. http://topic.csdn.net/u/20080806/10/cdb1faa1-0146-4e96-8b12-26ba60acdbb5.html
  7. http://lists.alioth.debian.org/pipermail/parted-devel/2007-July/thread.html#1855
  8. Read系统调用剖析, http://www.ibm.com/developerworks/cn/linux/l-cn-read/

Powered by ScribeFire.

ssh代理

星期四, 04月 10th, 2008

有一台远程主机的话,就可以通过ssh作代理了 原理如下

/ foo.com
浏览器ssh server – bar.com
\ example.com

然后在本机运行

$ssh -fND 6666 username@remote.ssh.server

6666是本地端口号,这样在本机就可以将localhost:6666 设为socks5代理

参数的解释可以看这里

Windows下可以用putty的后台命令行程序plink

车东[Blog^2]

$plink -N username@remote.ssh.server -D 127.0.0.1:6666

其中 -N 表示不需要shell

username@remote.ssh.server 替换成 -load sessionname 也可以

如何在Windows下使用密钥方式登录Linux服务器

如果设置需要基于密钥的登录(如果不设置密钥密码,就可以自动登录了),目前成功的方法是:

先在服务器上用服务器上用./ssh-keygen生成密钥对,将公钥 id_rsa.pub >>部署到要登录到的服务器上:/home/username/.ssh/authorized_keys 中.

密钥在Windows客户端下使用:将密钥id_rsa下载到本地,然后用puttygen导入id_rsa 另存转换为putty格式的密钥id_rsa.ppk即可。然后使用

plink -i c:\path\to\id_rsa.ppk username@example.com

登录。


补充:

为避免每次输入密码,可已将生成公钥将其部署到服务器上
$sshgen
它会在本机$HOME/.ssh目录下生成
id_rsa id_rsa.keystore id_rsa.pub
这几个文件,将公钥id_rsa.pub加到远程主机example.com的$HOME/.ssh下的authorized_keys就可以了。
参考 http://rainux.javaeye.com/blog/235777

gvimdiff

星期三, 01月 9th, 2008

gvimdiff file1 file2 [file3 [file4]]

It’s so cool~

增加swap分区[zz]

星期一, 01月 7th, 2008

from http://blog.donews.com/airu/archive/2007/12/28/1240974.aspx

1.首先用命令free查看系统内 Swap 分区大小。
$free -m

引用

total used free shared buffers cached
Mem: 1002 964 38 0 21 410
-/+ buffers/cache: 532 470
Swap: 951 32 929

可以看到 Swap 只有951M,不符合 Oracle-xe-client 的安装要求。

2.创建一个 Swap 文件。
$mkdir swap
$cd swap
$sudo dd if=/dev/zero of=swapfile bs=1024 count=100000

出现下列提示,上面命令中的 count 即代表swap文件大小。

引用
记录了 100000+0 的读入
记录了 100000+0 的写出
102400000 字节 (102 MB) 已复制,0.74704 秒,137 MB/秒

把生成的文件转换成 Swap 文件
$sudo mkswap swapfile

引用
Setting up swapspace version 1, size = 102395 kB
no label, UUID=09fde987-5567-498a-a60b-477e302a988b

3.激活 Swap 文件。
$sudo swapon swapfile

再次查看 free -m 的结果。

引用
total used free shared buffers cached
Mem: 1002 967 34 0 22 410
-/+ buffers/cache: 534 467
Swap: 1053 32 1021

添加成功。

扩展:
如果需要卸载这个 swap 文件,可以进入建立的 swap 文件目录。执行下列命令。
$sudo swapoff swapfile

如果需要一直保持这个 swap ,可以把它写入 /etc/fstab 文件。

引用
swapfilepath swap swap defaults 0 0

Linux常见任务之查找文件

星期四, 08月 9th, 2007
  • 查找安装的程序

//查找帮助文件
[root@web-node01 ~]# whereis -m php
php: /usr/share/man/man1/php.1
//查找二进制文件,除了帮助文件外全列出来了
[root@web-node01 ~]# whereis -b php
php: /usr/bin/php /etc/php.d /etc/php.ini /usr/include/php
//查找源代码
[root@web-node01 ~]# whereis -s php

e.g.

用grep命令在所有的普通文件中搜索hostname这个词:

# find . -type f -print | xargs grep "hostname"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:

# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。


Del.icio.us : , , ,

lftp参考

星期三, 08月 8th, 2007

lftp from ubuntu中文 http://wiki.ubuntu.org.cn/Lftp

登陆

lftp (ftp://)user:password@site:port

下载

  • 单线程下载单个或多个文件(get,mget,断点续传用-c)

/>get -c ls-lR.txt

/>mget *.txt

  • 多线程下载(pget,缺省5个线程)

/>pget -n 4 ls-lR.txt

上传

  • put,mput

文件夹下载,上传 (mirror)mirror [OPTS] [remote [local]]
-c 续传
-e 这个要小心一些, 比较远端和本地端的档案, 假如远端沒有的, 就将本地端的档案刪除, 也就是将本地端和远端资料同步。
-R 上传整个目录
-n 只下载较新的档案
-r 不用递回到目录中
–parallel=n 同时下载 n 个档案(预设一次只下载一个)

Ubuntu bin cue

星期五, 08月 3rd, 2007
  • .bin/.cue 转换为 .iso 格式
    bchunk image.bin image.cue image.iso
  • Mount ISO Image

        sudo mount -t iso9660 -o iocharset=utf8,loop debianetch.iso /media/isoimage/

What kernel loop module does?

I want to give brief introduction to kernel loop module.Using the
module loop it is possible to mount a filesystem file. squashfs is a
“loop” with (de)compression (Compressed Loopback Device) and it is
possible to mount a compressed filesystem like a block device and
seamlessly decompress its data while accessing it.

  • 要解决Java的中文显示问题,这里给个简单的解决办法。假设中文字体为 VeraSansYuanTi,路径在/usr/share/fonts/VeraSansYuanTi/,JDK路径在 /usr/lib/j2sdk1.5-sun/,那么进行如下操作:

cd /usr/lib/j2sdk1.5-sun/jre/lib/fonts
sudo mkdir fallback
cd fallback
sudo ln -s /usr/share/fonts/VeraSansYuanTi/VeraSansYuanTi-Regular.ttf
sudo mkfontdir
sudo mkfontscale

 

linux下对NTFS分区的写权限

星期五, 07月 27th, 2007

系统不支持对NTFS分区的写。装上NTFS-3G + NTFS-config 。运行NTFS-config搞定。

vim的替换

星期日, 05月 20th, 2007

在写编译程序时遇到了以下问题:已有下列文本

#define AUTO 1
#define BREAK 2
#define CASE 3
...

要写这样的代码:

token_map["auto"]=AUTO;
token_map["break"]=BREAK;
token_map["case"]=CASE;
...

用vim如何实现呢?

首先将其变为

AUTO
BREAK
CASE
...

:%s/#define //g
:%s/ [0-9]*$//g

之后用

:%s/[A-Z]*/token_map["L"]=U\0/g;

Now it’s okay! Enjoy:)

以下是vim参考手册中change列出来的

magic	nomagic	  动作    ~
  &	  &	  替代为完整的匹配				     s/&
 &	   &	  替代为 &
        替代为完整的匹配			 	         s/
     1	  替代为匹配的第一个 () 里面的内容		     s/1
     2   替代为匹配的第二个 () 里面的内容		     s/2
      ..	  ..						     s/3
      9	  替代为匹配的第九个 () 里面的内容		     s/9
  ~	  ~	  替代为前一个 substitute 的替代字符串		     s~
 ~	   ~	  替代为 ~					     s/~
      u	  下一个字符成为大写				     s/u
      U	  其后字符成为大写,直到 E 出现		     s/U
      l	  下一个字符成为小写				     s/l
      L	  其后字符成为小写,直到 E 出现		     s/L
      e	  结束 u、U、l 和 L (注意: 不是 !)	     s/e
      E	  结束 u、U、l 和 L				     s/E
      	  把该行在此位置一分为二
		  ( 以 CTRL-V  方式输入)		     s
      r	  同上						     s/r
      	  插入一个回车 (CTRL-M)
		  ( 以 CTRL-V  方式输入)		     s/
      n	  插入一个  (文件里的 )
		  (此处并不是换行)				     s/n
      b	  插入一个 					     s/b
      t	  插入一个 				     s/t
      	  插入单个反斜杠				     s/
      x	  其中 x 是上面没提到的任何一个字符:
		  保留作将来的扩展

示例:
  :s/a|b/xxxxxx/g             修改 "a b"      为 "xxxaxxx xxxbxxx"
  :s/([abc])([efg])/21/g   修改 "af fa bg" 为 "fa fa gb"
  :s/abcde/abc^Mde/              修改 "abcde"    为 "abc"、"de" (两行)
  :s/$/^M/                      修改 "abcde"    为 "abcde^M"
  :s/w+/u/g                 修改 "bla bla"  为 "Bla Bla"

挂载磁盘

星期六, 05月 19th, 2007

查看分区UUID命令是: ls -l /dev/disk/by-uuid

用pmount自动mount U盘: pmount /dev/sda