闲话handle和handler
虽然handle和handler只有一个字符之差,但在计算机的世界里,含义却大相径庭。
1. 先说说handle
北京话说"一边儿玩儿去,玩勺子把儿去","勺子把儿"说的就是handle。而将handle译成"句柄"绝对是一个相当文雅相当阳春白雪的翻译,因为太文绉绉啦,很多文化底蕴不够的码农就看不大懂了或者望而生畏。为了弄明白为什么这么整,我费了点儿周折。 句柄者,弯杆杆儿,弯把手儿也。注意: "句"为"勾"的通假字,句柄应该读作gou柄才是。《说文解字》对"句"的解释是"句, 曲也"。《说文解字注》(作者:清代学者段玉裁,简称"段注")里是这么说的"凡曲折之物,侈为倨,敛为句。考工记多言倨句。" 因此,如果将handle翻译成大白话"弯把手儿",进不得教科书,也写不进那些晦涩乏味的计算机图书。那么,程序员当如何理解handle呢?简单来说,handle就是一个"带把儿"的物件的那个"把儿"。
1.1 handle的本来含义
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】)

o 带handle的平底锅

1.2 handle在计算机世界里的含义
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
#include <stdio.h>
#include <signal.h>
#include <unistd.h> unsigned int g_flag = ; static void foo_handler(int signum)
{
printf("signal %d is caught, %s is called\n", signum, __func__);
g_flag++;
} int main(int argc, char *argv[])
{
signal(SIGUSR1, foo_handler); while (!g_flag)
sleep();
printf("good bye\n"); return ;
}
o 编译并测试
T1$ gcc -g -Wall -m32 -o foo foo.c T1$ ./foo T2$ ps -ef | grep foo | grep -v grep
veli : pts/ :: ./foo T2$ kill -SIGUSR1 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. 总结
- A handle is an abstract reference to a resource. Handle是对某个资源的抽象引用。
- A handler is an asynchronous callback subroutine. Handler则是一个异步的回调函数(子程序)。
附注: 《柯林斯高阶英语学习词典》对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.
闲话handle和handler的更多相关文章
- Jquery报错:Uncaught TypeError: ((m.event.special[e.origType] || (intermediate value)).handle || e.handler).apply is not a function
页面中出现了Jquery报错:Uncaught TypeError: ((m.event.special[e.origType] || (intermediate value)).handle || ...
- jquery , find the event handler,找到jquery中的event handler
找到 dispatch: function (e) { e = b.event.fix(e); var n, r, i, s, o, u = [], a = d.call(arguments), f ...
- Go Web:Handler
Multiplexer根据URL将请求路由给指定的Handler.Handler用于处理请求并给予响应.更严格地说,用来读取请求体.并将请求对应的响应字段(respones header)写入Resp ...
- Android: ListView数据的分批加载 以及 Handle 总结
这是效果图: activity_main.xml 01 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...
- Android中的Handler及它所引出的Looper、MessageQueue、Message
0.引入 0.1.线程间通信的目的 首先,线程间通信要交流些什么呢? 解答这个问题要从为什么要有多线程开始,需要多线程的原因大概有这些 最早也最基本:有的任务需要大量的时间,但其实并不占用计算资源,比 ...
- I/O Handler的管理(3)
另外连接地址:http://blogs.readthedocs.org/zh_CN/latest/Handler_mgr.html 本章目录 I/O Handler的管理 IO句柄与Select_Re ...
- Golang 接口型函数和http.Handler接口
一.接口型函数 参考Golang必备技巧:接口型函数 1.原始接口实现 type Handler interface { Do(k, v interface{}) } func Each(m map[ ...
- Handle详解
首先通过一个函数启动一个服务器,只提供一个方法并返回Hello World!,当你在浏览器输入http://127.0.0.1:8080,就会看到Hello World. 对于http.ListenA ...
- JavaScript事件详解-jQuery的事件实现(三)
正文 本文所涉及到的jQuery版本是3.1.1,可以在压缩包中找到event模块.该篇算是阅读笔记,jQuery代码太长.... Dean Edward的addEvent.js 相对于zepto的e ...
随机推荐
- 为Visual Studio添加一个“编码的UI测试生成器”的快捷方式
在添加CodedUI测试用例时,经常需要查看捕获控件的属性.按照常规的方式,只有在添加一个全新的CodedUI编码测试时才能查看捕获控件的属性,这样很不方便. 下面介绍在Visual Studio工具 ...
- API Test Postman接口测试之高级篇1
API Test Postman接口测试之高级篇1 一.postman中的请求参数简介: 1.请求参数简介: 点击params下面会出现key,value等信息,这里填写的会自动追加在url地址后面 ...
- 如何处理加括号的四则混合运算表达式——基于二叉树的实现(Eclipse平台 Java版)
记得上<数据结构>课程时,利用栈的特性解决过四则混合运算表达式.而如今在编写小型关系数据库的时候,编译部分要处理where后面的逻辑表达式——检查语法正确与否的同时,还要将信息传给下一个接 ...
- Java50道经典习题-程序14 求日期
题目:输入某年某月某日,判断这一天是这一年的第几天?分析:(1)以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 (2)特殊情况,闰年2月份的天数是29天,否则是28天 impo ...
- 利用C# CefSharp Python采集某网站简历并自动发送邀请短信
以往爬虫没怎么研究过,最近有个需求,要从某网站采集敏感信息,稍稍考虑了一下,决定利用C# Winform和Python一起来解决这个事件. 整个解决方案不复杂:C#编写WinForm窗体,进行数据分析 ...
- Code Chef January Challenge 2019题解
传送门 \(div2\)那几道题不来做了太水了-- \(DPAIRS\) 一个显然合法的方案:\(A\)最小的和\(B\)所有连,\(A\)剩下的和\(B\)最大的连 算了咕上瘾了,咕咕咕 const ...
- 洛谷P5279 [ZJOI2019]麻将(乱搞+概率期望)
题面 传送门 题解 看着题解里一堆巨巨熟练地用着专业用语本萌新表示啥都看不懂啊--顺便\(orz\)余奶奶 我们先考虑给你一堆牌,如何判断能否胡牌 我们按花色大小排序,设\(dp_{0/1,i,j,k ...
- ObjectARX动态添加AutoCAD传统下拉菜单入门篇(一)
ObjectARX动态添加传统下拉菜单入门篇 图文by edata , 转载注明出处 http://www.cnblogs.com/edata AutoCAD 添加传统下拉菜单有很多种方式,比较典型 ...
- 代码审计就该这么来3 beescms getshell
本文作者:i春秋作家——索马里的海贼 前言上一回(http://bbs.ichunqiu.com/thread-13714-1-1.html)说到快速漏洞挖掘中的几个重点关注对象,命令执行,文件操作, ...
- Elasticsearch 5 Ik+pinyin分词配置详解
版权声明:本文为博主原创文章,地址:http://blog.csdn.net/napoay,转载请留言. 一.拼音分词的应用 拼音分词在日常生活中其实很常见,也许你每天都在用.打开淘宝看一看吧,输入拼 ...