1、void CPU_CLR (int cpu, cpu_set_t *set)这个宏将 cpu 从 CPU 集 set 中删除。int CPU_ISSET (int cpu, const cpu_set_t *set)如果 cpu 是 CPU 集 set 的一员,这个宏就返回一个非零值(true),否则就返回零(false)。Example:我是在一个虚拟机上运行的程序,机器CPU是双核的,我设置虚拟机模拟四核。在linux上执行top指令看结果,点击“1”查看每个CPU核的运行情况。/* Short test program to test sched_setaffinity* (which
2、sets the affinity of processes to processors).* Compile: gcc sched_setaffinity_test.c * -o sched_setaffinity_test.o -lm* Usage: ./sched_setaffinity_test.o* Open a top-window at the same time and see all the work* being done on CPU 0 first and after a short wait on CPU 1,2,3.* Repeat with different n
3、umbers to make sure, it is not a* coincidence.*/#include math.hsched.hdouble waste_time(long n) double res = 0; long i = 0; while(i n * 200000) i+; res += sqrt(i); return res;int main(int argc, char *argv) unsigned long mask = 1; /* 二进制1,processor 0 */ /* bind process to processor 0 */ if (sched_set
4、affinity(0, sizeof(mask), &mask) 0) perror(sched_setaffinity); /* waste some time so the work is visible with */ printf (result: %fn, waste_time (2000); mask = 2; /*二进制10, process switches to processor 1 now */ /* waste some more time to see the processor switch */ mask = 4; /* 二进制100,processor 2 */
5、 /* bind process to processor 2 */ mask = 8; /* 二进制1000,process switches to processor 3 now */ 2、运行结果图2-1:图中显示编译及执行一个使用“CPU亲和力”的程序。图2-2 可以看见CPU0已在执行程序(CPU达到100.0%?有必要吗),第一段程序的结果还未出来。图2-3 可以看见CPU1已在执行程序,第一段程序的结果已经显示(图片中显示了上一次执行的结果,因为截图比较慢,执行了3次-),CPU1执行的结果还未显示出来。图2-4:可以看见CPU2已在执行程序,第二段程序(CPU1执行)的结果已经
6、显示,CPU2执行的程序结果还未出来。图2-5::CPU3再执行程序。图2-6:结果已经显示,CPU恢复到执行之前的状态二、多进程(在两个核上各自运行一个进程)1、代码如下/* Short test program to test parallel_setaffinity_test.c gcc parallel_setaffinity_test.c * -o parallel_setaffinity_test.o -lm ./parallel_setaffinity_test.o* being done on CPU 0 first and after a short wait on CPU
7、1. #includesys/types.hsys/sysinfo.hunistd.hstring.h #include double waste_time(long n) double res = 0; long i = 0; while(i i+; res += sqrt(i); return res; int main() char buf100; pid_t cld_pid; int fd; int status; unsigned long mask = 0; strcpy(buf,This is parent process write!n if(cld_pid=fork()=0)
8、 strcpy(buf,This is child process write! printf(This is child process!My PID(child) is %dn,getpid();,getppid(); mask = 1; /* bind process to processor 0 */ if (sched_setaffinity(0, sizeof(mask), & perror( /* waste some time so the work is visible with printf ( %f.n write(fd,buf,strlen(buf); close(fd
9、); exit(0); elseThis is parent process!My PID(parent) is %d.nMy child PID is %d.n,cld_pid); mask = 2; /* waste some more time to see the processor switch */ wait(&status); return 0;图3-1:图中显示编译及执行一个使用“CPU亲和力”的多进程程序,双核(虚拟四核太慢)。图3-2:,父子进程并发执行,父进程在CPU1上运行,子进程运行在CPU0,可以看出两个处理器目前都在运行程序。图3-3:结果已经显示,CPU0和CP
10、U1都恢复到执行之前的状态附录1、Processor affinityis a modification of the native central queuescheduling algorithmin asymmetric multiprocessingoperating system. Each task (be it process or thread) in the queue has a tag indicating its preferred / kinprocessor. At allocation time, each task is allocated to its kin
11、 processor in preference to others.Processor affinity takes advantage of the fact that some remnants of a process may remain in one processors state (in particular, in itscache) from the last time the process ran. Scheduling it to run on the same processor the next time could result in the processs
12、running more efficiently by reducing performance-degrading situations such as cache misses. A practical example might be running multiple instances of an application which does not use multiple threads, such as some graphics-rendering software. Overall system efficiency increases.Scheduling algorith
13、m implementations vary in adherence to processor affinity. Under certain circumstances some implementations will allow a task to change to another processor if this is deemed to be most efficient. This may be the case if two processor-intensive tasks (A & B) have affinity to one processor while anot
14、her processor lies unused. Many algorithms would shift task B to the second processor in order to maximize processor use. Task B would then acquire affinity with the second processor while task A would continue to have affinity with the original processor.Processor affinity can effectively reduce ca
15、che problems but it does not curb the persistentload-balancingproblem.1Further, processor affinity becomes more complicated in systems with non-uniform architectures. For example, a system with two dual-corehyper-threadedCPUs presents a challenge to a scheduling algorithm. There is complete affinity
16、 between two virtual CPUs implemented on the same core via hyper-threading, partial affinity between two cores on the same physical chip (as the cores share some, but not all, cache), and no affinity between separate physical chips.As other resources are also shared, processor affinity alone cannot
17、be used as the basis for CPU dispatching. If a process has recently run on one virtual hyper-threaded CPU in a given core, and that virtual CPU is currently busy but its partner is not, cache affinity would suggest that the process should be dispatched to the idle partner. However, the two virtual C
18、PUs compete for essentially all computing, cache, and memory resources. It would typically be more efficient in this case to dispatch the process to a different core or CPU if one is available. This would likely incur a penalty when process repopulates the cache, but overall performance would likely
19、 be higher as the process would not have to compete for resources within the CPU.OnLinuxthe CPU affinity of a process might be altered with the taskset(1) program.2SGIsystems,dplacebinds a process to a set of CPUs.3NetBSD5.0,FreeBSD7.2 and later versions can use pthread_setaffinity_np and pthread_ge
20、taffinity_np.4InNetBSD, the psrset utility5to set a threads affinity to a certain CPU set. InFreeBSD, cpuset6utility is used to create CPU sets and to assign processes to these sets. OnWindows NT, thread and process CPU affinities can be set separately by using SetThreadAffinityMask7and SetProcessAf
21、finityMask8API calls or via the Task Manager interface (for process affinity only).Mac OS Xexposes anaffinity APIthat provides hints to the kernel how to schedule threads according to affinity sets.2、sched_setaffinity, sched_getaffinity - set and get a processs CPU affinity maskSYNOPSISDESCRIPTIONsc
22、hed_setaffinity sets the CPU affinity mask of the process denoted by pid. If pid is zero, then the current process is used.The affinity mask is represented by the bitmask stored in mask. The least significant bit corresponds to the first logical processor number on the system, while the most signifi
23、cant bit corresponds to the last logical processor number on the system. A set bit corresponds to a legally schedulable CPU while an unset bit corresponds to an illegally schedulable CPU. In other words, a process is bound to and will only run on processors whose corresponding bit is set. Usually, a
24、ll bits in the mask are set.The argument len is the length of the data pointed to by mask. Normally this is the size of a word on the system. For compatibility with future versions of the Linux kernel, since this size can change, the bitmask supplied must be at least as large as the affinity mask stored in the kernel.The function