如果您喜歡修補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:
I get this result:
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.
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使用者那裡瞭解更多答案嗎?在這裡檢視完整的討論主題。
...、羽狀和掌狀是這三種類型。 目錄 1. 概述和主要區別 2. 什麼是羽狀 3. 什麼是掌狀 4. 羽狀和掌狀的相似性 5. 並排比較-羽狀和掌狀的表格形式 6. 摘要 什麼是羽狀(pinnate)? 羽狀脈序是植物最常見的脈序型別之一。一個主脈從葉...
...個術語嚴格用於申報就業稅。 目錄 1. 概述和主要區別 2. 什麼是聯邦稅務ID或EIN 3. 聯邦稅務ID和EIN的目的是什麼 5. 聯邦稅號和EIN在使用上的差異 6. 摘要 什麼是聯邦稅務id或ein(federal tax id or ein)? 聯邦稅務ID或EIN(僱主識別號)是...
... 什麼是頁首和頁尾(headers and footers)? ...
...智慧建議功能。點選燈泡,應用程式建議從前一天開始,什麼是到期或即將到來。您可以訪問Microsoft,在web上執行,並使用iPhone、Android**和所有Windows 10裝置的應用程式。 ...
... 就這樣!為什麼我要把它列成一個編號的清單?真的很簡單。單擊工具欄中的電池條圖示將在剩餘時間和剩餘百分比之間切換。 ...
...頁尾或頁首來解決頁碼不一致的問題。並透過確保其頂部沒有寫“與上一節相同”來檢查它是否未連結到上一節。 ...
...的元件,如GNOME或KDE,以及啟動時啟動的系統守護程序。 為什麼幾乎所有的東西都在執行?嗯,Bash內建的cd、pwd和alias在執行時不需要啟動(或“派生”)程序。Bash在終端視窗中執行的bashshell例項中執行這些命令。這些命令之所...