问题
Is there any way to force a process with specific PID, to be executed and run on only one of the cpu s of a server? I know that there is a command like this
taskset -cp <Cpu_Number> <Pid>
but the above command does not work on my system. So please let me know if there is any other command.
回答1:
There are two ways of assigning cpu core/cores to a running process.
First method:
taskset -cp 0,4 9030
Pretty clear ! assigning cpu cores 0 and 4 to the pid 9030.
Second Method:
taskset -p 0x11 9030
This is a bit more complex. The hexadecimal number that follows -p
is a bitmask. An explanation can be found here, an excerpt of which is given below :
The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system but a mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond to CPUs physically on the system. If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the current system) an error is returned. The masks are typically given in hexadecimal.
Still confused? Look at the image below :
I have added the binaries corresponding to the hexadecimal number and the processors are counted from left starting from zero. In the first example there is a one
in the bitmask corresponding to the zero
th processor, so that processor will be enabled for a process. All the processors which have zero
to their corresponding position in the bitmask will be disabled. In fact this is the reason why it is called a mask.
Having said all these, using taskset to change the processor affinity requires that :
A user must possess CAP_SYS_NICE to change the CPU affinity of a process. Any user can retrieve the affinity mask.
Please check the Capabalities Man Page.
You might be interested to look at this SO Question that deals with CAP_SYS_NICE.
My Resources
Tutorials Point
XModulo
来源:https://stackoverflow.com/questions/33994983/assigning-a-cpu-core-to-a-process-linux