What is cpu affinity
Many times it is good to wait for particular CPUs and get the work done well. Setting Affinity is nothing but scheduling a process on a certain subset of CPUs.
This setting will allow running any of your application process on the selected CPU core. Follow the simple steps. Go to the Task Manager. Click on the Processes tab. Click on Go to details. Detail of the process will be displayed. Select the process for setting CPU affinity.
As I am using the Core 2 Duo processor, there are 4 processors are available. By default, all the processes will be selected. It indicates the process can run on any of the available processors.
Aniruddha Chaudhari. I am complete Python Nut, love Linux and vim as an editor. I keep sharing my coding knowledge and my own experience on CSEstack. Your name can also be listed here.
What is CPU Affinity? Information Title. URL Name. This post discusses CPU affinity and exhibits some examples for its usage. Windows There are various of options to change the affinity of the process. No related lists to display. Follow Following Unfollow. Count the bits from right to left, bit 0 to bit 31 and, thus, processor zero to processor For example:. Because all bits are set, the process can run on any processor.
Get it? What do the next two masks equal in decimal? What is the result of using them as the affinity mask of a process? The first is equal to 2,,, and, because bit 31 is set, binds the process to processor number The second is equal to 3, and it binds the process in question to processor zero and processor one. The Linux CPU affinity interface uses a bitmask like that shown above.
Unfortunately, C does not support binary constants, so you always have to use the decimal or hexadecimal equivalent. You may get a compiler warning for very large decimal constants that set bit 31, but they will work. The first system call is used to set the affinity of a process, and the second system call retrieves it. The second argument is the length in bytes of the CPU affinity bitmask, currently four bytes 32 bits. This number is included in case the kernel ever changes the size of the CPU affinity mask and allows the system calls to be forward-compatible with any changes; breaking syscalls is bad form, after all.
The third argument is a pointer to the bitmask itself. As a convenience, the returned mask is binary ANDed against the mask of all processors in the system. Thus, processors in your system that are not on-line have corresponding bits that are not set. For example, a uniprocessor system always returns 1 for the above call bit 0 is set and no others.
What if you have only one? The system call fails unless at least one processor in the bitmask exists. Using a mask of zero always fails. Likewise, binding to processor seven if you do not have a processor seven will fail. It is possible to retrieve the CPU affinity mask of any process on the system. You can set the affinity of only the processes you own, however.
Of course, root can set any process' affinity. If you are not a programmer, or if you cannot modify the source for whatever reason, you still can bind processes. Listing 1 is the source code for a simple command-line utility to set the CPU affinity mask of any process, given its PID. As we discussed above, you must own the process or be root to do this. As an example, assume we have a dual computer and want to bind our Quake process with PID to processor two. We would enter the following:.
In the previous example, we bound Quake to one of the two processors in our system. To ensure top-notch frame rates, we need to bind all the other processes on the system to the other processor. You can do this by hand or by writing a crafty script, but neither is efficient.
Instead, make use of the fact that CPU affinity is inherited across a fork. All of a process' children receive the same CPU affinity mask as their parent. Then, all we need to do is have init bind itself to one processor. All other processes, by nature of init being the root of the process tree and thus the superparent of all processes, are then likewise bound to the one processor.
The cleanest way to do this type of bind is to hack this feature into init itself and pass in the desired CPU affinity mask using the kernel command line. We can accomplish our goal with a simpler solution, though, without having to modify and recompile init.
0コメント