为什么没有奇数编号的windows进程id?

如果您喜欢修补Windows并边走边学习,那么您可能已经注意到Windows进程和线程id是偶数和4的倍数。为什么?今天的超级用户问答帖子回答了一位好奇的读者的问题。...

为什么没有奇数编号的windows进程id?

如果您喜欢修补Windows并边走边学习,那么您可能已经注意到Windows进程和线程id是偶数和4的倍数。为什么?今天的超级用户问答帖子回答了一位好奇的读者的问题。

今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。

问题

超级用户读者Peter Hahndorf想知道为什么没有奇数编号的Windows进程ID:

There are many ways to look at the process IDs in Windows. Using PowerShell:

007Ys3FFgy1gpedrcik0nj30hd010wec

I get this result:

007Ys3FFgy1gpedrd71gnj30hd0c5jrl

As you can see, all the process IDs are even-numbered, not only that, they are all multiples of four. You can look as hard as you want and you will never find an odd-numbered process ID, at least not on any version that is Windows NT-based. What is the reason for this?

为什么没有奇数编号的Windows进程id?

答案

超级用户贡献者DavidPostill为我们提供了答案:

Why are there no odd-numbered Windows process IDs?

The same code that allocates kernel handles is also used to allocate process and thread IDs. Since kernel handles are a multiple of four, so are process and thread IDs.

Why are process and thread IDs multiples of four?

On Windows NT-based operating systems, process and thread IDs always happen to be a multiple of four. Is this just a coincidence?

Yes, it is just a coincidence, and you should not rely on it since it is not part of the programming contract. For example, Windows 95 process and thread IDs were not always multiples of four. By comparison, the reason that kernel handles are always a multiple of four is part of the specification and will be guaranteed for the foreseeable future.

Process and thread IDs are multiples of four as a side-effect of code reuse. The same code that allocates kernel handles is also used to allocate process and thread IDs. Since kernel handles are multiples of four, so are process and thread IDs. This is an implementation detail, so do not write code that relies on it. I am just telling you to satisfy your curiosity.

Source: Why are process and thread IDs multiples of four?

Why are kernel handles always a multiple of four?

Something that is not very well known is that the bottom two bits of kernel handles are always zero; in other words, their numeric value is always a multiple of four. Note that this applies only to kernel handles; it does not apply to pseudo-handles or to any other type of handle (USER handles, GDI handles, multimedia handles, etc.). Kernel handles are things you can pass to the CloseHandle function.

007Ys3FFgy1gpedrea0xlj30hd04bwem

That at least the bottom bit of kernel handles are always zero is implied by the GetQueuedCompletionStatus function, which indicates that you can set the bottom bit of the event handle to suppress completion port notification. In order for this to work, the bottom bit must normally be zero.

This information is not useful for most application writers, which should continue to treat handles as opaque values. The people who would be interested in tag bits are those who are implementing low-level class libraries or are wrapping kernel objects inside a larger framework.

Source: Why are kernel handles always a multiple of four?

Further Reading

The Old New Thing: Practical Development Throughout the Evolution of Windows by Raymond Chen (Principal Software Design Engineer at Microsoft)


有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。

  • 发表于 2021-04-10 09:14
  • 阅读 ( 220 )
  • 分类:互联网

你可能感兴趣的文章

可提高windows管理技能的powershell cmdlet

...的数据呢?Get ChildItem有一些内置的筛选,但大多数命令没有。在这些情况下,使用Where对象。 ...

  • 发布于 2021-03-13 13:26
  • 阅读 ( 294 )

linux中杀死无响应程序的7种方法

...应用程序也可能挂起。与其等待它们崩溃,不如杀死这些没有响应的程序。事实上,有很多方法可以杀死Linux程序,你可能会发现自己被宠坏了! ...

  • 发布于 2021-03-18 00:56
  • 阅读 ( 368 )

如何解决windows中cpu使用率高的问题

...SE.exe文件过程。如果它的CPU使用率高于百分之几,并且你没有运行任何会影响它的程序,那么它就不能正常工作。 ...

  • 发布于 2021-03-18 13:23
  • 阅读 ( 246 )

linux中的进程是什么?

对于没有计算机科学背景的人来说,过程这个词通常是陌生的。然而,在讨论Linux编程时经常使用它,进程对于系统管理工作是必不可少的。 ...

  • 发布于 2021-03-27 02:21
  • 阅读 ( 240 )

如何为microsoft word文档编号

...页脚或页眉来解决页码不一致的问题。并通过确保其顶部没有写“与上一节相同”来检查它是否未链接到上一节。 ...

  • 发布于 2021-03-30 06:42
  • 阅读 ( 242 )

如何在linux上杀死僵尸进程

...目,其中包含有关它的一些信息。 每个进程表结构中都没有太多内容。它们包含进程ID、一些其他数据项和指向该进程的进程控制块(PCB)的指针。 PCB保存了Linux需要为每个进程查找或设置的许多细节。PCB也会随着进程的创建、...

  • 发布于 2021-04-01 07:47
  • 阅读 ( 237 )

如何在linux上运行和控制后台进程

...的组件,如GNOME或KDE,以及启动时启动的系统守护进程。 为什么几乎所有的东西都在运行?嗯,Bash内置的cd、pwd和alias在运行时不需要启动(或“派生”)进程。Bash在终端窗口中运行的bashshell实例中执行这些命令。这些命令之所...

  • 发布于 2021-04-03 03:30
  • 阅读 ( 229 )

如何使用linux lsof命令

...之间的桥梁。除了挂载它的所有者(在本例中是GNOME),没有人甚至连根用户都不能访问这些文件系统。您可以忽略此警告。 lsof的输出很宽。最左边的列是: 最右边的列是: 列的lsof 并非所有列都适用于所有类型的打开文件...

  • 发布于 2021-04-03 10:30
  • 阅读 ( 163 )

linux下如何确定当前用户帐号

...教程将向您展示如何使用一些最快速、最简单的方法。 为什么需要查找当前用户的身份?在许多情况下,计算机的所有者是唯一的用户,而且,在不太现实的情况下,他们可能了解自己。也许,但人们创建额外的用户帐户以允...

  • 发布于 2021-04-03 16:59
  • 阅读 ( 141 )

windows任务管理器:完整指南

...序,然后单击“结束任务”将其关闭。如果一个应用程序没有响应,也就是说,如果它被冻结了,而您不能以通常的方式关闭它,那么这是很有用的。 也可以在此窗口中右键单击应用程序以访问更多选项: 切换到:切换到应用...

  • 发布于 2021-04-03 17:57
  • 阅读 ( 208 )
xxbdsemymd
xxbdsemymd

0 篇文章

相关推荐