為什麼沒有奇數編號的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
  • 閱讀 ( 36 )
  • 分類:網際網路

你可能感興趣的文章

羽狀(pinnate)和掌狀(palmate)的區別

...、羽狀和掌狀是這三種類型。 目錄 1. 概述和主要區別 2. 什麼是羽狀 3. 什麼是掌狀 4. 羽狀和掌狀的相似性 5. 並排比較-羽狀和掌狀的表格形式 6. 摘要 什麼是羽狀(pinnate)? 羽狀脈序是植物最常見的脈序型別之一。一個主脈從葉...

  • 發佈於 2020-10-15 08:28
  • 閲讀 ( 45 )

聯邦稅號(federal tax id)和ein公司(ein)的區別

...個術語嚴格用於申報就業稅。 目錄 1. 概述和主要區別 2. 什麼是聯邦稅務ID或EIN 3. 聯邦稅務ID和EIN的目的是什麼 5. 聯邦稅號和EIN在使用上的差異 6. 摘要 什麼是聯邦稅務id或ein(federal tax id or ein)? 聯邦稅務ID或EIN(僱主識別號)是...

  • 發佈於 2020-10-23 10:32
  • 閲讀 ( 42 )

如何在microsoftword中像pro一樣使用頁首和頁尾

... 什麼是頁首和頁尾(headers and footers)? ...

  • 發佈於 2021-03-11 10:25
  • 閲讀 ( 43 )

基於專案符號的最佳待辦事項列表應用程式

...智慧建議功能。點選燈泡,應用程式建議從前一天開始,什麼是到期或即將到來。您可以訪問Microsoft,在web上執行,並使用iPhone、Android**和所有Windows 10裝置的應用程式。 ...

  • 發佈於 2021-03-11 17:41
  • 閲讀 ( 65 )

如何查詢英特爾處理器代

...器是哪一代。我們將帶您瞭解如何找到這些資訊,並解釋為什麼它會派上用場。 ...

  • 發佈於 2021-03-13 03:08
  • 閲讀 ( 36 )

如何在Windows10工作列中顯示電池百分比

... 就這樣!為什麼我要把它列成一個編號的清單?真的很簡單。單擊工具欄中的電池條圖示將在剩餘時間和剩餘百分比之間切換。 ...

  • 發佈於 2021-03-17 09:28
  • 閲讀 ( 37 )

如何解決windows中cpu使用率高的問題

...00%。同時,你的電腦可能會慢下來。這很煩人,尤其是你什麼都沒做的時候。 ...

  • 發佈於 2021-03-18 13:23
  • 閲讀 ( 55 )

linux中的程序是什麼?

... 什麼是過程(a process)? ...

  • 發佈於 2021-03-27 02:21
  • 閲讀 ( 60 )

如何為microsoft word文件編號

...頁尾或頁首來解決頁碼不一致的問題。並透過確保其頂部沒有寫“與上一節相同”來檢查它是否未連結到上一節。 ...

  • 發佈於 2021-03-30 06:42
  • 閲讀 ( 54 )

如何在linux上執行和控制後臺程序

...的元件,如GNOME或KDE,以及啟動時啟動的系統守護程序。 為什麼幾乎所有的東西都在執行?嗯,Bash內建的cd、pwd和alias在執行時不需要啟動(或“派生”)程序。Bash在終端視窗中執行的bashshell例項中執行這些命令。這些命令之所...

  • 發佈於 2021-04-03 03:30
  • 閲讀 ( 50 )
xxbdsemymd
xxbdsemymd

0 篇文章

作家榜

  1. admin 0 文章
  2. 孫小欽 0 文章
  3. JVhby0 0 文章
  4. fvpvzrr 0 文章
  5. 0sus8kksc 0 文章
  6. zsfn1903 0 文章
  7. w91395898 0 文章
  8. SuperQueen123 0 文章

相關推薦