[技術] 使用Linux的mmap心得

Written on 11:40 上午 by Yu Lai

最近的工作有遇到一個用mmap解決的issue,
讓我了解到mmap原來也可以這樣使用.
以下是使用的心得, 寫下來分享一下.

一般我們在Linux中使用mmap不外乎是用來做IPC的實做,
或是配合driver的file node來操控外部的device,
或者是特殊的需求-加速存取or動態載入.

但我這次遇到的問題居然是空間的需求. 在採用Linux來實做
Embedded System時, 往往會使用Ramdisk或ramfs等技術
來implement出file system來. 而這類的file system有個特點-
它佔memory. 所以在存取較大的檔案時, e.g. image raw file.
若採用傳統malloc出空間再讀進此空間進行操作時, e.g. MD5
checksum, 勢必會遇到記憶不夠的情況.
(PS. 還意外知道原來Linux有oom-killer這東東 XD)

此時我採用了mmap來將原本就在memory裡的file另外對映到
其他記憶體空間中, 把這個當成pointer來操作, 而不用額外花費
真實的memory空間. 以下是相關的source code:

int fd;
struct stat fstat;

fd = open(TMPMD5, O_RDONLY);
stat(TMPMD5, &fstat);
image = mmap(NULL, fstat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if(image == MAP_FAILED) {
printf("Error.\n");
close(fd);
return -1;
}

[技術] Linux在x86與non-x86開機程序比較

Written on 1:28 上午 by Yu Lai

感謝jserv在blog中分享, 在IBM developerWorks上的好文章: [Migrating from x86 to PowerPC, Part 2: Anatomy of the Linux boot],探討 GNU/Linux 在不同硬體架構下的開機程序差異,這篇文章有兩張很好的附圖可作為比較:

典型 x86 Linux 啟動程序:



典型 ARM / PowerPC Linux 啟動程序:


[技術] 各家Database分頁實作

Written on 11:25 上午 by Yu Lai

Ref: http://blog.blueshop.com.tw/gpx1981/archive/2007/08/24/52129.aspx

DB2
1.1. SELECT * FROM STUDENT ORDER BY SCORE DESC fetch first 10 rows only
1.2. select * from ( SELECT rownumber() over(ORDER BY SCORE DESC) as row_,
* FROM STUDENT ORDER BY SCORE DESC ) as temp_ where row_ <= 10
2. select * from ( SELECT rownumber() over(ORDER BY SCORE DESC) as row_,
* FROM STUDENT ORDER BY SCORE DESC ) as temp_ where row_ between 11
and 20

Firebird
1. SELECT first 10 * FROM student ORDER BY score DESC
2. SELECT first 10 SKIP 10 * FROM student ORDER BY score DESC
 
HypersonicSQL(HSQL)
1. SELECT TOP 10 * FROM student ORDER BY score DESC
2. SELECT LIMIT 10 10 FROM student ORDER BY score DESC
 
Interbase
1. SELECT * FROM student ORDER BY score DESC ROWS 10
2. SELECT * FROM student ORDER BY score DESC ROWS 10 TO 10
 
MySQL
1. SELECT * FROM student ORDER BY score DESC LIMIT 10
2. SELECT * FROM student ORDER BY score DESC LIMIT 10, 10
 
Oracle
1. select * from ( SELECT * FROM STUDENT ORDER BY SCORE DESC ) where
rownum <= 10
2. select * from ( select err.*, rownum rownum_ from ( SELECT * FROM
errormsg order by seqno) err where rownum <= 20) where rownum_ > 10
 
PostgreSQL
1. SELECT * FROM student ORDER BY score DESC limit 10
2. SELECT * FROM student ORDER BY score DESC limit 10 OFFSET 10
 
SQLServer
1. SELECT top 10 * FROM STUDENT ORDER BY SCORE DESC
 
paged queries not supported
FrontBase, Informix, Ingres, Mckoi, NoArgSQL, Pointbase, Progress, SAPDB,
StandardSQL, Sysbase