指定进程运行的CPU

1、taskset
-p, –pid
operate on an existing PID and not launch a new task
-c, –cpu-list
specify a numerical list of processors instead of a bitmask. The list may contain multiple items, separated by comma, and ranges. For example, 0,5,7,9-11.
taskset -p pid   显示进程号为pid的进行运行的cpu。显示结果为和cpu affinity有关的二进制bitmask。从地位到高位,每一个1对应一个cpu
taskset -pc 3 pid  制定进程运行在特定cpu上,这里是第4个cpu
taskset -c 1 ./redis-server 进程启动时指定cpu
2、sched_setaffinity系统调用
int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) 将某个进程绑定到特定的CPU。定义在sched.h头文件中。成功返回0,错误返回-1。 pid为0,表示当前调用进行。
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) 查看一个进程的CPU亲和度掩码
cpu affinity bitmask会从父进程传递给其派生出的子进程。
POSIX API环境下使用int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
const cpu_set_t *cpuset);
int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
cpu_set_t *cpuset);

Leave a Reply

Your email address will not be published. Required fields are marked *