雖然handle和handler只有一個字符之差,但在計算機的世界裏,含義卻截然不同。程序員
1. 先說說handle編程
北京話說"一邊兒玩兒去,玩勺子把兒去","勺子把兒"說的就是handle。而將handle譯成"句柄"絕對是一個至關文雅至關陽春白雪的翻譯,由於太文縐縐啦,不少文化底蘊不夠的碼農就看不大懂了或者望而生畏。爲了弄明白爲何這麼整,我費了點兒周折。 句柄者,彎杆杆兒,彎把手兒也。注意: "句"爲"勾"的通假字,句柄應該讀做gou柄纔是。《說文解字》對"句"的解釋是"句, 曲也"。《說文解字注》(做者:清代學者段玉裁,簡稱"段注")裏是這麼說的"凡曲折之物,侈爲倨,斂爲句。考工記多言倨句。" 所以,若是將handle翻譯成大白話"彎把手兒",進不得教科書,也寫不進那些晦澀乏味的計算機圖書。那麼,程序員當如何理解handle呢?簡單來講,handle就是一個"帶把兒"的物件的那個"把兒"。windows
1.1 handle的原本含義app
A handle is a part of, or attachment to, an object that can be moved or used by hand.
例如:異步
o 帶橡膠handle的現代拔釘錘(A modern claw hammer with rubber handle 【圖片來源: https://en.wikipedia.org/wiki/File:Claw-hammer.jpg】)socket
o 帶handle的平底鍋async
1.2 handle在計算機世界裏的含義ide
A handle is an abstract reference to a resource.
+1: A handle is a unique identifier for an object managed by Windows.
+2: A handle can be anything from an integer index to a pointer to a resource
in kernel space. The idea is that they provide an abstraction of a resource,
so you don't need to know much about the resource itself to use it.
注: +1 is from "what-is-a-windows-handle"; +2 is from "what-is-a-handle-in-c"函數
例如: (在Unix/Linux系統中) 【鑑於我的對windows瞭解甚少,故不談windows】學習
進程號pid就是一個handle,
文件描述符(fd)也是一個handle,
系統調用號(syscall num)仍然是一個handle,
... 不勝枚舉。
在操做系統中,一切對用戶來講是透明(注:這裏的"透明"指的是"看不見摸不着就如空氣同樣"而不是"盡收眼底毫無祕密可言")的可是操做系統內核看得懂的無符號整數(unsigned int)均可以被看做是handle。
在操做系統設計與實現中,聯繫內核態和用戶態,靠的就是一個個無符號整數。由於用數字來作通訊密碼(好比:操做碼,錯誤碼等)實在是太方便了。並且,一個unsigned int佔4個字節,能夠表徵的通訊密碼總數爲2^32(=4G, 約40億)。 若是不用無符號整數來作通訊密碼,而是採用可讀性很好的明文(字符串"string")來作通訊,那是何等的情何以堪?! 由於,計算機作字符串比較的代價要遠遠大於無符號整數的比較。
好啦,扯遠了,一句話,下次看到"句柄",不用懼怕啦。由於它就是handle, 說白了就是跟一個黑盒子進行通訊的密碼。一旦通訊密碼傳給了黑盒子,黑盒子具體怎麼操做,對持有handle的用戶來講,徹底不用關心。"不看過程,只看結果"就得了。 古人云"微曲爲倨,甚曲爲句",將"handle"翻譯成"句柄",仍是有必定道理的,由於用戶程序拿到的handle,一般並不可以徑直通向真實的內核資源,而是須要"繞個彎兒",也就是被內核映射成一個指向內核資源的首地址的pointer纔可以訪問真實的內核資源。
2. 什麼是handler
在編程中使用過信號(signal)的朋友必定跟handler不會陌生。 例如:
$ man -s2 signal NAME signal - ANSI C signal handling SYNOPSIS #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); ...
hanlder就是一個回調函數(callback)。當某個事件到達時,事先註冊的handler會被接收到事件的主體所調用。 示例代碼:
o foo.c
1 #include <stdio.h> 2 #include <signal.h> 3 #include <unistd.h> 4 5 unsigned int g_flag = 0; 6 7 static void foo_handler(int signum) 8 { 9 printf("signal %d is caught, %s is called\n", signum, __func__); 10 g_flag++; 11 } 12 13 int main(int argc, char *argv[]) 14 { 15 signal(SIGUSR1, foo_handler); 16 17 while (!g_flag) 18 sleep(10); 19 printf("good bye\n"); 20 21 return 0; 22 }
o 編譯並測試
T1$ gcc -g -Wall -m32 -o foo foo.c T1$ ./foo T2$ ps -ef | grep foo | grep -v grep veli 9239 2293 0 21:06 pts/7 00:00:00 ./foo T2$ kill -SIGUSR1 9239 The output from T1 looks like: T1$ ./foo signal 10 is caught, foo_handler is called good bye
維基百科對handler的解釋是這樣的,
Handler, an asynchronous callback (computer programming) subroutine in computing ... Event handler, a routine for processing a programming event Interrupt handler, a routine for processing CPU interrupts Signal handler, a routine for handling signals sent to a process Exception handler, a routine for handling software exceptions
而維基百科對handle的解釋是這樣的,
In computer programming, a handle is an abstract reference to a resource.
Handles are used when application software references blocks of memory or
objects managed by another system, such as a database or an operating system.
A resource handle can be an opaque identifier, in which case it is often an
integer number (often an array index in an array or "table" that is used to
manage that type of resource), or it can be a pointer that allows access to
further information.
Common resource handles are file descriptors, network sockets,
database connections, process identifiers (PIDs), and job IDs.
Process IDs and job IDs are explicitly visible integers, while file descriptors
and sockets (which are often implemented as a form of file descriptor) are
represented as integers, but are typically considered opaque. In traditional
implementations, file descriptors are indices into a (per-process) file
descriptor table, thence a (system-wide) file table.
3. 總結
附註: 《柯林斯高階英語學習詞典》對handle和handler的解釋(供參考並幫助理解其在計算機世界裏的含義)
A handle is the part of an object such as a tool, bag, or cup that you hold in order to be able to pick up and use the object.
A handler is someone whose job is to deal with a particular type of object.