網頁

2010年7月3日 星期六

Find my way home

Just feel and smile...

2010年6月12日 星期六

口試完了

被電爆...
接下來還有好多研究要補QQ

2010年5月26日 星期三

Virtual File System (VFS)

File System Category
Disk-based file system
journal file system: provide backup file system, more security
FAT -> NTFS
Network file system
NFS, SMB(CIFS), Samba(Unix-based SMB), AFS...
Special file system
/proc, /dev, devfs...


File System Abstraction Layer
/tmp (in-memory file system - RAM disk)

Unix file system
Filesystem are mounted at a specific mount point in a global hierarchy known as a namespace.
mount point(nfs, \tmp)

Each component of a path is called a directory entry(dentry)

inode - file's metadata
superblock - filesystm metadata

Non-Unix filesystem still work in Linux(FAT, NTFS)
special processing done on the fly


VFS Objects

superblock, inode, dentry, file
[              static              ]


sync_fs() best effort
link() - create a inode
dentry cache (d-cache)
inode cache (i-cache)

fs_struct (Process decriptor's fs field)

Nothing is trivial

研究不是一件容易的事
比起coding 研究需要花費更多的心思
除了要設想每個可能被擊破論點
還要小心翼翼地花費時間闡述出來

只不過如果研究方向就是錯誤的話
研究出來的結果就是不可行
那要怎麼去扭轉這樣的結論

真的是不容易......

2010年5月24日 星期一

[OpenCV] Camera Capture

Programmer can extract webcam image throuhg OpenCV easily. 
Following is the example code.

[code]  
    CvCapture *capture;
    IplImage *frame;
    capture =cvCaptureFromCAM(0) ;           //camera init
    cvNamedWindow("Webcam",0);               //highgui window init
    while(TRUE)
    {
        frame = cvQueryFrame(capture);       //extract from CvCapture struct
        cvShowImage("Webcam",frame);         //show frame
        if(cvWaitKey(10)>=0)              //wait for key in
        {
            break;
        }
    }
    cvReleaseCapture(&capture);
    cvDestroyWindow("Webcam");

pkg-config

pkg-config is a useful tool for compiling.
It helps to insert correct compiler options on the command line for gcc/g++ compiler.
example:

            `pkg-config gtk+-2.0 --cflags`                      //include gtk+2.0 lib
            `pkg-config opencv --libs --cflags opencv`    //include opencv lib



[Reference]
pkg-config wiki - http://pkg-config.freedesktop.org/wiki/

[NSLAB] Fun Time



[Reference]
1. wiki
2. youtube

[本報告僅供學術參考]

2010年5月20日 星期四

Ubuntu - ADSL Access

對象: 需帳密的ADSL (浮動IP)
方法:
sudo pppoeconf
進入PPPOE設定程式。
  1. 偵測你哪一張網路卡連接到ISP。他會列出所有偵測到的網路介面並詢問你他是否已找到所有網路介面
  2. 問你是否想用「noauth」、「defaultroute」選項,沒有特殊需求的話,直接答「yes」即可。
  3. 鍵入ADSL用戶名稱
  4. 鍵入ADSL密碼
  5. 問你是否使用 ISP 提供的 DNS 伺服器,沒有特殊需求的話,直接答「yes」即可。
  6. 是否要把 MSS 限制不大於1452位元組,沒有特殊需求的話,直接答「yes」即可。
  7. 問你是否想每次啟動 Ubuntu 時自動連線網路
  8. 最後會問你是否想現在即時連線上網


CSMA - Carrier Sense Multiple Access

It is difficult to detect collision in the wireless environment.
The wireless nodes must detect actively.


Therefore there are two methods to prevent collisions:

1. Before sending data, wait a period of time to listen medium state if no one exploit and then wait a random time to send data (still no one use). Because different random time, it will decrease collision possibility.

2. Before sending data, send RTS(request to send) to target and wait for CTS(clear to send). If receiving CTS, start to send data. (RTS and CTS will clear the channel)
RTS and CTS are small frame and won't cause high transfer cost. (ex. IEEE 802.11)

In other word, RTS/CTS exploit the signals to notify other nodes that there are someone sending data, so others need to wait a second.

2010年5月19日 星期三

Device Driver

Device driver
/dev : a list of device node (sudo file system)
type
major number
minor number

2 special device  nodes
dev - null
dev - zero

dev_t dev =MKDEV(major, minor)
register_chrdev


/proc/devices: list of devices

File Operations
struct file_operations scull_fops
open - initialization

2010年5月13日 星期四

[轉錄] SCREEN

使用 screen

[出自 http://tavi.debian.org.tw/index.php?page=screen ]

在命令列下打 screen 就會進入 screen 的天地:

% screen

注意,所有 screen 的指令都是以 Ctrl 鍵加上 a 鍵開始的,在之後以 C-a 代表,C-a c 代表按住 Ctrl 鍵不放再按 a 鍵,然後放開按 c 鍵;而 C-a C-c 代表按住 Ctrl 鍵不放再按 a 鍵,然後放開後再按住 Ctrl 鍵不放按 c 鍵。

開啟,關閉,切換

C-a c 或 C-a C-c 開啟新的視窗,並同時切換到這個新的視窗

C-a n 或 C-a C-n 或 C-a (space) 切換到下一個視窗(0->1 1->2 ...)

C-a p 或 C-a C-p 切換到上一個視窗(1->0 2->1 ...)

C-a C-a 切換到上一個顯示過的視窗(不是照順序切換)

C-a 0 切換到第 0 個視窗

C-a (1..9) 切換到第 (1..9) 個視窗

C-a w 或 C-a C-w 會列出目前所開啟的視窗

0$ tcsh 1$ tcsh 2*$ tcsh 3$ tcsh

有星號 "*" 的表示目前顯示中的視窗,之後的 tcsh 代表這個視窗的 title ,預設是以開啟時的 shell 名稱

C-a K 關閉所有的視窗並退出 screen

C-a ' 或 C-a " 會出現 "Switch to window:" 字樣,輸入號碼後就可切到該視窗

單一視窗使用中的指令

C-a C 清除目前的視窗內容

C-a d 或 C-a C-d 脫離(detach)目前的 screen ,並放到背景執行(不管開了幾個視窗),下面會再提到

C-a D D 強力脫離,除了放到背景執行外,並自動 logout

C-a C-g 視覺化鈴聲(visual bell)的切換,可以在畫面上顯示訊息代表一般的嗶聲

C-a H 紀錄目前視窗所有顯示過的東西(檔名為 screenlog.n n 為視窗號碼)

C-a i 或 C-a C-i 顯示目前視窗的資訊

(11,24)/(80,24)+1000 +(+)flow -ins -org +wrap +app -log -mon +r G0 [BBBB]

C-a l 或 C-a C-l 重繪視窗,如果有字顯示亂掉了可以用這個指令

C-a m 或 C-a C-m 或 C-a (enter) 可以顯示最近一次顯示過的訊息

C-a t 或 C-a C-t 顯示系統的時間及主機名稱還有負載

C-a v 顯示版本資訊

C-a x 或 C-a C-x 如果你要暫時離開目前位置又不想先 logout ,那可以用這個來鎖住 screen

C-a ? 線上求助畫面

複製/卷軸 模式

按下 C-a [ 就可以進入 複製/卷軸 模式(只有在目前視窗才算,其他視窗照常)基本上跟 vi 很像:

h, j, k, l 分別為向左,向下,向上,向右移動一格(或是一行)

0 移到該行最左邊

^ 與 $ 分別移到該行最左邊及最右邊的非空白字元

w 以字為單位往前移動並移到字首

b 以字為單位往後移動並移到字首

e 以字為單位往前移動並移到字尾

C-b 向上捲一頁

C-f 向下捲一頁

C-u 向上捲半頁

C-u 向下捲半頁

/ 與 ? 向下或向上尋找字串

(space) 第一次按下到第二次按下的區域會被複製到暫存區內

(ESC) 離開 複製/卷軸 模式

接著按下 C-a ] 便可以把剛剛存到暫存區內的內容複製出來。

善用 screen

前面提到當按了 C-a d 脫離了 screen 後,會回到沒有執行 screen 時的狀態,但是同時在之前 screen 裡每個視窗內在跑的 process (不管是前景或背景執行的)仍然繼續執行,即使 logout 也不會有影響。所以你可能接著離開身邊的電腦,然後跑出去喝個水,或者另外找個舒服的地方坐下來再接著剛剛的工作。

好,當你重新登入(login)之後,要怎麼接回剛剛的 screen 呢?重打 screen 只會讓電腦另外又開個 screen 給你,你所要做的事就是加些參數:

screen -ls 顯示目前個人所有的 screen

% screen -ls

There are screens on:

1188.ttyp4.SungSung? (Attached)

76920.ttypq.SungSung? (Detached)

2 Sockets in /tmp/screens/S-AlanSung?.

上面的 Attached 表示你現在連接的 screen ,而 Detached 則表示沒有連接的,最後則是共有多少個 screen 數量

screen -r 就是重新回到(resume)screen 裡,如果你只有開一個 screen ,那只要簡單地打

% screen -r

就沒問題了,但是像上面那個,如果你有了兩個以上的 screen ,這樣就有問題了

% screen -r

There are several screens on:

1188.ttyp4.SungSung? (Detached)

76920.ttypq.SungSung? (Detached)

Type "screen [-d] -r [pid.]tty.host" to resume one of them.

如它的訊息所述,你可以加上它的 process id 或是使用的 tty 加上主機名來決定要接回那一個 screen 。假設在這裡我們要接回 process id 為 1188 的那一個,就打

% screen -r 1188



% screen -r ttyp4



% screen -r ttyp4.SungSung?



% screen -r 1188.ttyp4.SungSung?

都可以

screen -d 假設現在自己的電腦突然當機,或是網路突然斷了一時連不上去,等到好了之後,想要接回來原來的 screen 卻發現

% screen -r

There is a screen on:

1188.ttyp4.SungSung? (Attached)

There is no screen to be resumed.

這時候 -d 這個參數就派上用場了,讓你可以控制某個 screen 脫離(detach),接著再加上 -r 把這個 screen 接手回來

% screen -d

[1188.ttyp4.SungSung? detached.]

% screen -r

或直接

% screen -d -r

screen -wipe 有時候不小心跳電了,這時候重開機後當然也不會有 screen 留著,但是由於 screen 會有紀錄所有的 screen ,因為雖然舊的沒有了,可是還是有紀錄在,因此這個指令就是把廢棄不用的 screen 給"擦拭乾淨"(wipe)

There are screens on:

69103.ttyp5.SungSung? (Removed)

529.ttyp0.SungSung? (Detached)

1 socket wiped out.

1 Socket in /tmp/screens/S-AlanSung?.

會用了上面所介紹的功能,你可以現在進入 bbs ,進入聊天室跟人聊天或是在上面發表文章到一半,發現計中要關門了,你可以從容的 detach ,回家之後再連回來 attach ,繼續剛剛的話題或發表文章,而對方可能根本沒發覺。只要你連線的主機不當機,那電話線或網路再怎麼不穏都不必怕斷線。

如果你需要更多詳細的資料,可以善用線上手冊(manual pages)或是 info 這個指令

% man screen

% info screen

2010年5月12日 星期三

VSPM

Linux Highlight (1)

lsmod (list modules)- observe how many modules included in kernel now
         -- display content: module name, size, and used by
 
insmod (insert modules) - mount modules to the kernel
rmmod (remove modules) - unmount modules from the kernel


TODO

HW/SW Co-Design Note

Synthesis 合成  Allocation + Binding + Scheduling

排程(scheduling)、連結(binding)、與分配(allocation)
                          (運算對資源的指定)    (貼標籤)

Problem : what is the goal? (計算)
Architecture : system arch. (表示component 與其連接)
Specification : Possible Mapping

partitioning - optimization
computation < --------------MAPPING--------------- > component

Bi-partitioning :     SW       +          HW
                         processor +       ASIC

Partitioning (Level of Abstraction)
functional - system level - system parameter unknown
structural - RTL level , netlist level

[Reference]
http://www.nioswiki.com/

2010年5月11日 星期二

AWK

Pattern { Action }
     -> If Pattern is true, do Action.

Pattern will exploit "Relational Expression".


[Relational Expression]
>, <, ==, != ...etc.
~(match)
!~(Not Match)


[EXE]
1. awk -f awkfile [file]
2. $ awk 'shell action' [file]
3. script and chmod +x to instruction

[awk action format]
awk 'pattern' file : display rows which match patterns
awk '{action}' file : execute the action in {} for every row
awk 'pattern{action}' file: execute the action for the rows which match patterns

[argument passing]
awk -v varible 'pattern' file
ex: awk -v avar="$svar" '$0 == avar' file

[Reference]
AWK tutorial

Regular Expression

 regular expression

一個 regular expression是被表示於斜線( / / )之間,並且比對是採用 =~ 為操作子

. # 任何單一字元除了 newline
^ # 一行或一字串的起始 [限制字串出現於行首]
$ # 一行或一字串的尾端 [限制字串出現於行末]
* # 零或多個最後的字元 [重複出現 - 可以沒有]
+ # 一或多個最後的字元 [重複出現 - 至少一個]
? # 零或一個最後的字元 [重複0或1個]

Network Simulation Evaluation (NS2)

Wireless new trace format

[Example]
s -t 0.001978125 -Hs 12 -Hd -2 -Ni 12 -Nx 0.00 -Ny 0.00 -Nz 0.00 -Ne -1.000000 -Nl MAC -Nw --- -Ma 0 -Md c000000 -Ms ffff0008 -Mt message -Is 12.42 -Id -1.42 -It message -Il 30 -If 0 -Ii 2 -Iv 32 




1st field - Packet event
s: send
r: recv
d:drop
f: forward

2nd field - Event time 
-t time

3rd field - Next hop info
-Hs: id for this node
-Hd: id to the next hop towards the destination
  -> -1 broadcast
  -> -2 not deployed yet

4th field - Node property
-Ni:
-Nx -Ny -Nz : node's coordinate
Ne: node energy level
Nl: trace level (ex: AGT..etc)
Nw: reason for the event - 
END” DROP_END_OF_SIMULATIONCOL” DROP_MAC_COLLISIONDUP” DROP_MAC_DUPLICATEERR” DROP_MAC_PACKET_ERRORRET” DROP_MAC_RETRY_COUNT_EXCEEDEDSTA” DROP_MAC_INVALID_STATEBSY” DROP_MAC_BUSYNRTE” DROP_RTR_NO_ROUTE 
LOOP” DROP_RTR_ROUTE_LOOP 
TTL” DROP_RTR_TTL 
TOUT” DROP_RTR_QTIMEOUT 
CBK” DROP_RTR_MAC_CALLBACKIFQ” DROP_IFQ_QFULL  
ARP” DROP_IFQ_ARP_FULL 
OUT” DROP_OUTSIDE_SUBNET

5th field - Packet info at IP level
-Is: source address.port
-Id: dst address.port
-It: packet type
-Il: packet size
-If: flow id
-Ii: unique id
-Iv: time to live

6th field - Packet info at MAC level
-Ma: duration
-Md: destination's addr
-Ms: source's addr
-Mt: ethernet type


2010年5月7日 星期五

[轉錄] struct的初始化,拷貝及指標成員的使用技巧



struct是C中重要的ADT。但是在一般講C的書中,往往只介紹了struct的定義、順序初始化及位域。
本文將筆者曾經用到的、看到的知識點羅列出來,與大家分享。


為了方便後面的介紹,先定義一個struct類型:
struct User
{
int id; //id
char name[100]; //user name
char *home; //home directory
int passwd; //password
};




1初始化
struct數據有3中初始化方法:順序,C風格及C++風格的亂序。


1)順序
這種方法很常見,在一般的介紹C的書中都有介紹。順序初始化的特點是:按照成員定義的順序,從前到後逐個初始化;允許只初始化部分成員;在被初始化的成員之前,不能有未初始化的成員。
eg:
struct User oneUser = {10, "Lucy", "/home/Lucy"};
初始化之後,oneUser各個成員的值為:
oneUser.id = 10;
oneUser.name = "Lucy";
oneUser.home = "/home/Lucy";
oneUser.passwd = 0;


2)亂序(C風格)
順序的缺陷是必須按成員定義的順序逐個初始化,不能間隔。而亂序的方式則很好的解決了這個問題,因為這種方式是按照成員名進行。
eg:
struct User oneUser = {
.name = "Lucy",
.id = 10,
.home = "/home/Lucy"
};


3)亂序(C++風格)
C++風格的亂序初始化方式跟C風格的一樣,只是它更常用在C++代碼裡。
eg:
struct User oneUser = {
name:"Lucy",
id:10,
home:"/home/Lucy"
};




不論是哪種方式,都允許只初始化部分成員;未被初始化的成員默認為0(指標類型的成員默認為NULL)。兩種亂序初始化方法,即可以用在C代碼中,也可以用在C++代碼中。




2拷貝
struct有兩種拷貝方式,一是直接賦值(=),另一種是用memcpy等庫函數實行內存拷貝。
eg:
struct Temp a, b;
//Set value to members of b
a = b;
memcpy(&a, &b, sizeof(a));
不管是哪種拷貝方式,都是將以&b開始的,大小為sizeof (struct Temp)的內存區域中的數據,簡單地複製到以&a開始的,同樣大小的內存區域。所以,這兩種方式與按成員賦值是等價的:
a.id = b.id;
a.name = b.name;
a.home = b.home;
a.passwd = b.passwd;
由此,我們不難看出,上面兩種拷貝方式都屬於淺拷貝。
           


3指標成員的兩種使用技巧
1)為多個指標成員同時分配內存
如果一個struct中有多個指標類型的成員,我們通常需要為每個指標逐個成員分配內存空間,並在使用完時釋放它們;這樣頻繁調用malloc/free,難免讓人生厭。如果在分配內存之前,每個指標所指向內存區域的大小是確定的,那麼,我們可以為所有指標一次性分配內存區域;並在使用完後,一次性釋放。
eg:
struct Inode
{
int id;


char *file;
int fie_len;


char *path;
int path_len;


char *user;
int user_len;
};


struct Inode data = {
.file_len = X,
.path_len = Y,
.user_len = X
};


//Allocate memory
data.file = (char *)malloc(data.file_len + data.path_len, data.user_len);
data.path = data.file + data.file_len;
data.user = data.path + data .path_len;




//User
       ...




//Free memory
free(data.file);




2)變長數組的另類實現
將下面的定義
struct File
{
TypeA dataA;
          ......
char *data;
TypeN dataN;
};
改成:
struct File
{
TypeA dataA;
          ......      
TypeN dataN;
char data[0];
};
即將指標成員換成大小為0的一維數組,作為struct的最後一個成員(數據結構的可變部分必須作為最後一個成員),有兩個優點:
(1)在緊鄰struct處為data分配內存區域,這樣在分配內存後無須為data賦值;
(2)利用數組的特性,以指標的方式通過越界訪問data數組外的內存區域。
eg:
struct File *pVar = (struct File *)malloc(sizeof(struct File) + DATA_LEN);
strncpy(pVar->data, "Source data", DATA_LEN);
       

2010年4月24日 星期六

[轉錄] c++ typedef



當你在宣告一個變數時是這樣的:
int  ImVar;//<-----------------------1
當你在宣告一個函式時卻是這樣:
int  ImFun(...);//---------------------2
變數宣告時名稱在最後面,而函式名稱卻在中間,
你會不會覺得這很奇怪?
本來用一個小括號括起來的參數定義就是函式名稱的附屬品
你可以當它是函式名稱的一部份。沒有了它函式名稱就不完整了。
(注意在C++中不同參數的同名函式在編譯器的內部函式名稱是不同的)

typedef int INT;//<------------------3
typedef int *PINT;//<--------------4
typedef int (*PINT);//<--------------5
3式是定義一個int的型態,名為INT
4式是定義一個int的指標型態,名為PINT
5式是定義一個指向int的指標型態,名為PINT
4式和5式的結果是等效的。

現在我們嘗試為函式定義型態:
typedef int IntFun(...);//<------------6
先注意到有關2式的說明,就不應再對為何函式名稱後還有(...)
6式定義一個型態(或返回)int函式,名稱為IntFun。
我們知道,函式名本身俱有隱性指標的性質,所以IntFun和 *IntFun是
等效的。
那麼可以明白的定義IntFun為指標嗎,應該可以的!直觀的感覺是套入
4式:
typedef int * IntFun(...);//<------------7
問題出來了,任何一個編譯器都會把7式解讀為:
定義一個型態(或返回)int*函式,名稱為IntFun。
這不是我們要的,那要如何指定指標('*')給IntFun而不是int呢?
答案就是括號,也就是套入5式而不是4式:
typedef int (*IntFun)(...);//<------------8
這就是原提問要的解答了,唯要注意的是
對型態的定義來說 4式和5式是等效的,
但對函式的定義來說6式和8式才是等效的;
那麼使用6式或8式效好?
一般都使用8弍,它有較好的可讀性,隱式指標總是令人較為困感的。
而且也不敢保證所有的編譯器都可以接受6式的敘述。





P.S. C/C++ tutorial http://140.127.40.1/~jwu/c/cpg.htm