發表文章

目前顯示的是 2月, 2008的文章

Linux Process 2 ~ Hash

上一次有提到說,在 Linux Kernel 裡面找尋特定 PID 的 task 不是使用 sequential search,而是使用 hash 的方式,所以現在就來看看它的 hash 到底是怎麼做的! Kernel: 2.6.23 透過 PID 找尋 task 的 API 是下面那一支( kernel/pid.c ): struct task_struct *find_task_by_pid_type(int type, int nr) { return pid_task(find_pid(nr), type); } type? type 是什麼呢?幹嘛要有 type? 先來看看宣告( include/linux/pid.h )吧: enum pid_type { PIDTYPE_PID, // pid PIDTYPE_PGID, // pgid PIDTYPE_SID, // session PIDTYPE_MAX // 用來擔任 enum 的最大值(也許是作為檢查用) }; 後面的註解是我自己加上去的。簡單來說,Linux Kernel 不是單單建立一個 hash table,而是多個,所以要給一個 type 來說明要找哪一個 hash table。接下來就繼續看 pid_task 和 find_pid 到底是幹嘛囉~ struct task_struct * fastcall pid_task(struct pid *pid, enum pid_type type) { struct task_struct *result = NULL; if (pid) { struct hlist_node *first; first = rcu_dereference(pid->tasks[type].first); if (first) result = hlist_entry(first, struct task_struct, pids[(type)].node); } return result; } struct pid * fastcall find_pid(int

好用的 Search Script

這是由我同事所寫的小 script,還蠻好用的。經過作者同意後放入自己的 Blog 中。本 script 可以在程式碼中進行關鍵字的搜索。因為簡單明瞭,所以就不加以解說了。 附帶廣告他的 Blog: http://parrotshen.blogspot.com/ #!/bin/bash if [ $# == 0 ]; then echo "Usage: $0 " echo " : $0 " echo "" exit fi dir=. if [ $# == 2 ]; then dir=$2 fi echo "--------------------------------------------------------------------------------" echo "Processing ..." echo "" #list=`egrep -rwl "$1" $dir | egrep "\.(c|h|cpp)$"` list=`grep -rwl "$1" $dir | grep "\.\(c\|C\|h\|H\|s\|S\|cpp\)$"` for x in $list do echo "[[30;47m$x[[0m" grep -wn "$1" $x echo "" done echo "--------------------------------------------------------------------------------"