2012年3月8日 星期四

在Linux kernel module 建立proc

轉自:http://kaijie-chang.blogspot.com/2010/12/linux-kernel-module-proc.html

在Linux kernel module 建立proc

建立 Proc的方法很簡單,以下是個建立,讀寫的範例
//include kernel module一些基本的header
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/uaccess.h>

//建立proc_dir struct
static struct proc_dir_entry *dir;
static struct proc_dir_entry *item;
//先implement 對proc讀寫的function
int item_read(char *page, char **start, off_t off, int count, int *eof, void *data )
{
    return sprintf(page,"hello");
}
ssize_t itemt_write( struct file *filp, const char __user *buff, unsigned long len, void *data )
{
char *buffer;
copy_from_user(buffer,buff,len);
printk(KERN_INFO"%s",buffer);
return len;
}
//將讀寫的function註冊到proc_dir struct
int itemt_init(struct proc_dir_entry *entry)
{
entry->read_proc = item_read;
entry->write_proc = item_write;
entry->owner = THIS_MODULE;
return 0;
}
//將proc註冊到系統上
int proc_init(void)
{
entry_dir = proc_mkdir("dir", &proc_root);//把proc目錄建立到/proc 根目錄下
entry_item = create_proc_entry ("item", 0644, entry_dir);//再把item的proc建立在dir下
itemt_init(entry_item);//把proc callback initial好
}
void proc_exit(void)
{
remove_proc_entry("item", entry_item);
remove_proc_entry("dir", &proc_root);

判斷檔案是否存在

使用函數 
    int access(const char *filename, int amode);
apmode為0時是檢查檔案的存在性


access可以檢查檔案其他屬性:
06 檢查讀寫權限
04 檢查讀權限
02 檢查寫權限
01 檢查執行權限
00 檢查檔案存在


#include<stdio.h>
main()
{
    printf("access=%d\n",access("/tmp/file",0));
    //if 0 檔案存在
    //if -1檔案不存在
} 

pthread使用說明

轉自:http://www.cnblogs.com/xianghang123/archive/2011/08/11/2134927.html


linux下C/C++,多線程pthread 


 ·線程創建 


  函數原型:int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg); 


  返回值:若是成功建立線程返回0,否則返回錯誤的編號。 


  形式參數:pthread_t *restrict tidp要創建的線程的線程id指針;const pthread_attr_t *restrict attr創建線程時的線程屬性;void* (start_rtn)(void)返回值是void類型的指針函數;void *restrict arg start_rtn的形參。 


  ·線程掛起:該函數的作用使得當前線程掛起,等待另一個線程返回才繼續執行。也就是說當程序運行到這個地方時,程序會先停止,然後等線程id為thread的這個線程返回,然後程序才會斷續執行。 


  函數原型:int pthread_join( pthread_t thread, void **value_ptr); 


  參數說明如下:thread等待退出線程的線程號;value_ptr退出線程的返回值。 


  ·線程退出 


  函數原型:void pthread_exit(void *rval_ptr); 


  ·獲取當前線程id 


  函數原型:pthread_t pthread_self(void); 


  ·互斥鎖 


  創建pthread_mutex_init;銷毀pthread_mutex_destroy;加鎖pthread_mutex_lock;解鎖pthread_mutex_unlock。 


  ·條件鎖 


  創建pthread_cond_init;銷毀pthread_cond_destroy;觸發pthread_cond_signal;廣播pthread_cond_broadcast S;等待pthread_cond_wait。  


  ·正確處理Linux平台下的線程結束問題 


  在Linux平台下,當處理線程結束時需要注意的一個問題就是如何讓一個線程善始善終,讓其所占資源得到正確釋放。在Linux平台默認情況下,雖然各個線程之間是相互獨立的,一個線程的終止不會去通知或影響其他的線程。但是已經終止的線程的資源並不會隨著線程的終止而得到釋放,我們需要調用pthread_join() 來獲得另一個線程的終止狀態並且釋放該線程所占的資源。








【用C】


/* example.c*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i<3;i++){
sleep(1);
printf("This is a pthread.\n");}
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++){
printf("This is the main process.\n");
sleep(1);
}
pthread_join(id,NULL);
return (0);
}





編譯命令 gcc -o example example.c -lpthread


注:【1】pthread 庫不是 Linux 系統默認的庫,連接時需要使用靜態庫 libpthread.a,所以在使用pthread_create()創建線程,以及調用 pthread_atfork()函數建立fork處理程序時,需要鏈接該庫。


      【2】還要添加頭文件pthread.h


      【3】因為C庫自帶sleep,所以可以不添加unistd.h頭文件





【用C++】


/* example.cpp*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *thread(void *ptr)
{
int i;
for(i=0;i<3;i++){
sleep(1);
printf("This is a pthread.\n");}
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i<3;i++){
printf("This is the main process.\n");
sleep(1);
}
pthread_join(id,NULL);
return (0);
}





編譯鏈接命令 g++ -o example2 example.c -lpthread





【注】 標亮部分,注意C++開發時與C的區別,解釋如下:


我們注意在Posix定義建立線程函數的原型:
extern int pthread_create (pthread_t *__restrict __threadp,
                           __const pthread_attr_t *__restrict __attr,
                           void *(*__start_routine) (void *),
                           void *__restrict __arg) __THROW;
這個call中的第三個參數是載入一個函數,這個函數有一個參數可以傳入,返回一個 通用指針。
我們再來看看原來函數中是怎樣調用這一原型的,基本上就是類似一下的調用方式:
(void *) thread 或是 (void *) &thread


這個表達式的含義:取一個指向函數main_thread的指針,然後將其轉換為一個通用指針。
這就是說顯然上述兩個事情並非同一個事情,故而正確的調用方式是
ret=pthread_create(&id,NULL,thread,NULL);
處理函數的定義如下:
void *thread(void *ptr)
值得注意的是在gcc編譯時不會出錯,但是用g++就會有問題:invalid conversion from `void*' to `void*(*)(void*),究其原因就是C語言編譯器允許隱含性的將一個通用指針轉換為任意類型的指針,而C++不允許。

2012年3月2日 星期五

輸出u32/u64格式

d, lx, ld, lu, 這幾各都是輸出32位元
hd, hx, hu, 這幾各都是輸出16位元
hhd, hhx, hhu, 這幾各都是輸出8位元
lld, ll, llu, llx, 這幾各都是輸出65位元

printf( "%llu ",.....)
%llu   是64位元無符號
%llx   是64位元16進制

2012年3月1日 星期四

Get DHCP Assignment Default Gateway

Shell Code:
    route -n | grep 'UG[ \t]' | awk '{print $2}'


Shell Code:
    route -n | grep '^0\.0\.\0\.0[ \t]\+[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]\+[ \t]\+0\.0\.0\.0[ \t]\+[^ \t]*G[^ \t]*[ \t]' | awk '{print $2}'

Fedora無法開機救援

開機進入GRUB啟動後按 "e",接著在移動到kernel選項在按"e"
進入編輯模式,並輸入"single",跳出後按"b"重新啟動即可進入
編輯模式.

2012年2月29日 星期三

基金配息換算

(金額/淨值)/美金=(單位數*配息)*美金=實際月配息