Phiên Hỏi & Đáp hôm nay đến với chúng tôi theo lịch sự của SuperUser - một phân khu của Stack Exchange, một nhóm các trang web Hỏi & Đáp do cộng đồng điều hành.
Câu hỏi
Người đọc siêu người dùng Peter Hahndorf muốn biết tại sao không có ID tiến trình Windows được đánh số lẻ:
There are many ways to look at the process IDs in Windows. Using PowerShell:
Tại sao không có ID quy trình Windows số lẻ?
Câu trả lời
Người đóng góp SuperUser DavidPostill có câu trả lời cho chúng tôi:
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.
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)
Có cái gì để thêm vào lời giải thích? Tắt âm thanh trong các ý kiến. Bạn muốn đọc thêm câu trả lời từ những người dùng Stack Exchange có hiểu biết công nghệ khác? Xem toàn bộ chuỗi thảo luận tại đây.