2008/02/12

Simple UDP server in kernel space with kernel thread

在Fred's blog看到的

Simple UDP Server in Kernel-space

Web Server in kernel-space

在這兩個例子可以看到如何在kernel裡撰寫網路程式以及如何使用kernel thread

2008/02/09

Delete duplicate row without disarranging original order

這是在ptt的linux版看到的討論串,還蠻有趣的,有許多不同的語言作法。

問題
$ cat file.txt
00120
00345
00345
00567
00789
00789

請問如何將重複的列刪除,變成下面這樣:
00120
00345
00567
00789


Linux command (by kenduest)
cat -n test.txt | sort -k 2 -n -u | sort -k 1 -n | cut -f 2
Bash (by FourDollars)
#!/usr/bin/env bash

declare -a db
declare -i index=0

cat file.txt | while read line; do
if ! echo ${db[*]} | grep $line > /dev/null; then
db[$((index++))]="$line"
echo "$line"
fi
done
Perl (by OuTian)
cat test.txt | perl -ne 'print unless($s{$_}++)'
C (by HZYSoft)
/* File: remove_dup.c: Remove duplicated string in a file.
Author: PCMan (C) 2008.02.08
License: GNU GPL V2 */

#include <stdio.h
>
#include <stdlib.h>
#include <string.h>
int main( int argc, char** argv ) {
int i, j, n, buf_size = 1024;
char **strv, line[1024];
FILE *f;
if( ! (f = fopen( argv[1], "r" )) ) return 1;
strv = (char**)malloc(buf_size * sizeof(char*) );
for( n = 0; fgets( line, sizeof(line), f ); ++n ) {
if( n >= buf_size ) {
buf_size += 1024;
strv = (char**)realloc( strv, buf_size );
}
strv[n] = strdup( line );
}
fclose(f);
for( i = 0; i < n; ++i ) {
for( j = i + 1; j < n; ) {
if( 0 == strcmp( strv[i], strv[j] ) ) {
free( strv[j] );
#ifdef KEEP_ORDER /* 如果保持原有順序 */
memcpy( &strv[j], &strv[j+1], sizeof(char*) * (n-j-1) );
#else /* 如果不管順序,這樣速度會快很多 */
strv[j] = strv[n - 1];
#endif
--n;
}
else {
++j;
}
}
}
if( ! (f = fopen( argv[1], "w" )) ) return 1;
for( i = 0; i < n; ++i ) {
fputs( strv[i], f );
free( strv[i] );
}
fclose( f );
return 0;
}

2008/02/07

Difference between __cdecl and __stdcall

在呼叫函式的前,程式必需先將參數放到stack裡,隨後在函式執行完後,這些參數若是由caller負責清除則稱為__cdecl,反之若是callee負責則為__stdcall。

他們有以下的不同點及討論:
  1. __stdcall的呼叫,程式大小會比較小一點。
  2. printf這類不定量的參數的函式,只能使用__cdecl的呼叫

Reference.
  1. __cdecl and __stdcall
  2. Intel x86 Function-call Conventions - Assembly View
  3. Using Win32 calling conventions

Happy lunar new year!?

今年的農曆年過得比以往較不同,一來是放假的時間沒有像快樂的大學生,以及無憂無慮的研一來的長,二來是在過外的老師沒有過中國年,想當然,我們這些研究生也別想過年。過年期間要煩惱的事有一堆,隨便舉就有論文數據有問題、SocketTracker的實驗要做、論文outline、老師叫我寫的paper要改,隨便舉舉就一堆,根本就快樂不起來。

雖然很多事情要煩,但是該訂的電動還是一個也不能少,這次年假的主力放在2個遊戲,一個是Biohazared Umbrella,另一個是瑪力歐的北京奧運的遊戲。Bio那款打到後來很想要有個什麼密技之類的,最後看到有人說只要所有scenario的chapter Hard等級打到Rank S就可以有無限彈,在我的不眠不休的實驗,結論"不可能",在無技可施之下我只好去找別人已經都打到S的存檔,再複製到我的Wii裡,有了無限彈,打起來真的很爽,但是卻好像少了點樂趣。另一款北京奧運的遊戲我也很推薦,家裡有Wii的人可以買來玩。

今年過年大概就在打電動吃東西中渡過。當然,為了初六要跟老師meeting,現在要開始改paper了。好痛苦喔。

(好混的日記)

2007 Turing Award

I think Turing Award is an important award for whom had great contribution towards computer science. However, it seem less people discussed it in Taiwan.

Edmund M. Clarke, E. Allen Emerson, and Joseph Sifakis are the recipients of the 2007 A.M. Turing Award for their work on an automated method for finding design errors in computer hardware and software.

The method, called Model Checking, is the most widely used technique for detecting and diagnosing errors in complex hardware and software design. It has helped to improve the reliability of complex computer chips, systems and networks.


Original article Dr.Dobb 2007 Turing Award Winners Announced