虽然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
NAMEsignal - ANSI C signal handlingSYNOPSIS#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.cT1$ ./fooT2$ ps -ef | grep foo | grep -v grep
veli      9239  2293  0 21:06 pts/7    00:00:00 ./fooT2$ kill -SIGUSR1 9239The 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的区别与内涵意思相关推荐

  1. handle与handler的区别

    handle既是名词也是动词,而handler只是个名词.在计算机编程术语里handle作为名词时是对可进行管理的资源对象的抽象,handle指向某个类别的资源对象,如文件句柄,进程ID都可以用han ...

  2. 闲话handle和handler

    虽然handle和handler只有一个字符之差,但在计算机的世界里,含义却大相径庭. 1. 先说说handle 北京话说"一边儿玩儿去,玩勺子把儿去","勺子把儿&qu ...

  3. handle和handler的理解

    维基百科对handler的解释: Handler, an asynchronous callback (computer programming) subroutine in computing .. ...

  4. android 倒计时handle,android -handler 实现倒计时

    实现倒计时想到了三个方案 1.countDownTimer sdk较高版本有bug 计时不精准 2.timer 和timer task的方式 但是在timertask不可以直接更新页面,还是需要用ha ...

  5. mvvm模式和mvc的区别_Android 开发中的架构模式 -- MVC / MVP / MVVM

    预备知识 了解 Android 基本开发 看完本文可以达到什么程度 了解如何分析一个架构模式 掌握 MVC,MVP,MVVM 架构定义和实现 更多面试内容,面试专题,flutter视频 全套,音视频从 ...

  6. Martini 中的 Handler

    为什么80%的码农都做不了架构师?>>>    前文参见 Martini 的工作方式 Handler 在Martini中是这样定义的 <!-- lang: cpp --> ...

  7. html中inline函数,开窗函数和窗口函数区别 inline函数和一般的函数有什么不同

    sql over开窗函数 和group by的区别 / 蓝讯如果有多个聚合函数,但是分组依据不同,此时只能使用开窗函数. 而GROUP BY要求聚合函数的分组依据一致. SQL Server中的开窗函 ...

  8. android模拟多任务键,模拟Android Handler机制——单线程处理多任务

    消息类(Message): package p; public class Message { private Runnable runnable; private long start; priva ...

  9. Android中handler的使用及原理---学习笔记

    Handler类的基本介绍以及使用: Android中UI操作是线程不安全的操作,如果有多个线程并发操作UI组件,就会出现线程安全问题,所以Android中制定了一个规则:在Android中只允许主线 ...

最新文章

  1. SAP RETAIL MM42维护商品采购信息记录数据的缺陷
  2. HTML基础-第二讲
  3. python唯一映射类型_Python基础:04映射类型
  4. 富士康筹划在越南建造2.7亿美元新工厂,扩大生产线!
  5. Spark RDD Cache Checkpoint
  6. 第 15 篇:优化博客功能的细节,提升使用体验—— HelloDjango 系列教程
  7. 光缆成端接头的含义是指
  8. 微信小程序开发制作 | 第1期:下载微信小程序开发工具
  9. ORCAD元件的批量替换与更新
  10. 腾讯云通信IM集成踩坑记
  11. 图解多线程设计模式pdf_图解Java多线程设计模式 PDF 全书扫描版
  12. Fashion MNIST进行分类
  13. 3 计算机网络的主要功能,计算机网络的功能主要有哪些?
  14. unity5-GI是什么?
  15. 情人节单身的你,是否用一张智能名片,进行表白
  16. 思科无线认证服务器,思科服务器认证配置
  17. Android的消息循环机制:Handler
  18. 计算机等级考试光敏电阻,光敏电阻的基础知识介绍
  19. FITC/ICG/TMR/HRP/Cy3荧光标记阿法替尼/阿西替尼/克里唑蒂尼/奥拉帕尼/Lenvatinib/多韦替尼 等药物
  20. python爬虫(13)爬取百度贴吧帖子

热门文章

  1. Python-pandas-Excel序列填充
  2. 用Modelsim仿真时一直弹窗,最后会闪退
  3. 交朋友这事,我觉得算是我们能自己控制的改变命运的方法之一
  4. 如何低成本实现酒店地图导航室内导航
  5. flowable 实现逐级审批功能
  6. csstable跨列居中_CSS进阶11-表格table
  7. 【华为OD机试真题 JAVA】英文输入法
  8. PMP备考战术:题海战术
  9. linux服务器怎么连接无线网卡,在linux上怎么安装无线网卡驱动?_网站服务器运行维护,linux,无线网卡...
  10. jQueryDOM节点操作总结