计算机科学导论奇数编号答案_为什么没有奇数编号的Windows进程ID?

计算机科学导论奇数编号答案

计算机科学导论奇数编号答案

why-are-there-no-odd-numbered-windows-process-ids-00

If you love tinkering with Windows and learning as you go, you may have noticed that Windows process and thread IDs are even-numbered and multiples of four. Why is that? Today’s SuperUser Q&A post has the answers to a curious reader’s questions.

如果您喜欢修改Windows并随手学习,您可能已经注意到Windows进程和线程ID是偶数且是4的倍数。 这是为什么? 今天的“超级用户问答”帖子回答了好奇的读者的问题。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader Peter Hahndorf wants to know why there are no odd-numbered Windows process IDs:

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

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

在Windows中,有很多方法可以查看进程ID。 使用PowerShell:

why-are-there-no-odd-numbered-windows-process-ids-01

I get this result:

我得到这个结果:

why-are-there-no-odd-numbered-windows-process-ids-02

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?

如您所见,所有进程ID都是偶数,不仅如此,它们都是4的倍数。 您可以随心所欲地查找,并且永远不会找到奇数编号的进程ID,至少在基于Windows NT的任何版本上都找不到。 这是什么原因呢?

Why are there no odd-numbered Windows process IDs?

为什么没有奇数Windows进程ID?

答案 (The Answer)

SuperUser contributor DavidPostill has the answer for us:

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

Why are there no odd-numbered Windows process IDs?

为什么没有奇数Windows进程ID?

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.

分配内核句柄的相同代码也用于分配进程和线程ID。 由于内核句柄是四个的倍数,因此进程和线程ID也是如此。

Why are process and thread IDs multiples of four?

为什么进程和线程ID是4的倍数?

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

在基于Windows NT的操作系统上,进程和线程ID总是碰巧是四个的倍数。 这只是巧合吗?

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.

是的,这只是一个巧合,您不应该依赖它,因为它不是编程合同的一部分。 例如对于Windows 95进程和线程ID并不总是四个的倍数。 相比之下,内核句柄始终是4的倍数的原因是该规范的一部分,并且在可预见的将来将得到保证。

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.

进程和线程ID是代码重用的副作用,是四个的倍数。 分配内核句柄的相同代码也用于分配进程和线程ID。 由于内核句柄是4的倍数,因此进程和线程ID也是如此。 这是一个实现细节,因此不要编写依赖它的代码。 我只是告诉你要满足你的好奇心。

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

资料来源: 为什么进程和线程ID是四的倍数?

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.

不太了解的是内核句柄的后两位始终为零。 换句话说,它们的数值始终是四的倍数。 注意,这仅适用于内核句柄。 它不适用于伪句柄或任何其他类型的句柄(用户句柄,GDI句柄,多媒体句柄等)。 您可以将内核句柄传递给CloseHandle函数。

why-are-there-no-odd-numbered-windows-process-ids-03

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.

GetQueuedCompletionStatus函数暗示至少内核句柄的最低位始终为零,这表明您可以设置事件句柄的最低位以禁止完成端口通知。 为了使其正常工作,最低位通常必须为零。

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)

旧的新事物:Windows演变过程中的实用开发,作者: Raymond Chen ( Microsoft首席软件设计工程师 )



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/222451/why-are-there-no-odd-numbered-windows-process-ids/

计算机科学导论奇数编号答案

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值