首页 > 系统服务 > 详细

Linux下的“句柄”(文件句柄,窗口句柄)

时间:2019-01-22 10:04:24      阅读:287      评论:0      收藏:0      [点我收藏+]

在windows中,句柄是一个32位的整数,是内存中维护的一个对象的地址列表的整数索引,这些对象包括:窗口(window)、块(module)、任务(task)、实例 (instance)、文件(file)、内存块(block of memory)、菜单(menu)、控制(control)、字体(font)、资源(resource),包括图标(icon),光标 (cursor),字符串(string)等、GDI对象(GDI object),包括位图(bitmap),画刷(brush),元文件(metafile),调色板(palette),画笔(pen),区域(region),以及设备描述表(device context)。

在Linux中,每一个进程都由task_struct 数据结构来定义,即PCB,进程通过PCB中的文件描述符表找到文件描述符fd所指向的文件指针filp,文件描述符表是一个指针数组,每一个元素都指向一个内核的打开文件对象,而fd,就是这个表的下标。当用户打开一个文件时,内核会在内部生成一个打开文件对象,并在这个表里找到一个空项,让这一项指向生成的打开文件对象,并返回这一项的下标作为fd,Linux中的文件描述符类似于Windows下文件句柄的概念,但区别是Windows的文件句柄是一个全局的概念,而Linux下文件句柄的作用域只在本进程空间,其中0(标准输入)、1(标准输出)、2(标准错误)是每一个进程中相同的文件描述符,由操作系统规定好,文件描述符所指向元素的文件指针为struct file结构体,在系统中是一个全局的指针。

技术分享图片

在linux的X Window桌面环境下,Window 类似于Windows下的窗口句柄,X11/X.h中定义如下:

typedef unsigned long int XID;
typedef XID Window;
typedef XID Font;
typedef XID Pixmap;
typedef XID Drawable;
typedef XID Cursor;
typedef XID Colormap;
typedef XID GContext;
typedef XID KeySym;

 同时,X的官方解释(参考https://www.x.org/wiki/guide/concepts/#index10h4)如下:

XIDs

Many resources managed by the server are assigned a 32-bit identification number, called an XID, from a server-wide namespace. Each client is assigned a range of identifiers when it first connects to the X server, and whenever it sends a request to create a new Window, Pixmap, Cursor or other XID-labeled resource, the client (usually transparently in Xlib or xcb libraries) picks an unused XID from it‘s range and includes it in the request to the server to identify the object created by this request. This allows further requests operating on the new resource to be sent to the server without having to wait for it to process the creation request and return an identifier assignment. Since the namespace is global to the Xserver, clients can reference XID‘s from other clients in some contexts, such as moving a window belonging to another client.

大概意思是:

X Server通过一个32比特的标识号标识资源。每一个客户端在第一次连接到X Server时,会被赋值一个标识范围,无论是向X Server请求创建一个Window、Pixmap、或者其他XID标记的资源,客户端(通常对于Xlib或xcb库来说是透明的)从此范围选择一个未使用的XID,同时在向X Server发送请求时包含此XID以识别此次请求创建的资源。这种方式允许同时向X Server请求多个新的资源,而不需要等待每次资源创建成功并返回XID。因为对于X Server来说,域名是全局的,所以在同一个上下文中,不同的客户端之间可以相互引用XID,例如在一个客户端中移动属于另一个客户端的窗口。

在Xorg中,客户端资源(包括XID)使用一个数组保存(dix/resource.c):

static ClientResourceRec clientTable[MAXCLIENTS];

当客户端向X Server请求资源时,会查询数组中相关的资源。

 

Linux下的“句柄”(文件句柄,窗口句柄)

原文:https://www.cnblogs.com/lizhenneng/p/10290127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!