• 作者:老汪软件
  • 发表时间:2024-01-02 06:00
  • 浏览量:

查看cpu有几个核

使用cat /proc/查看cpu信息,如下两个信息:

也可以使用系统调用获取cpu核心数:

#

int ();/* 返回系统可以使用的核数,但是其值会包括系统中禁用的核的数目,因 此该值并不代表当前系统中可用的核数 */

int ();/* 返回值真正的代表了系统当前可用的核数 */

/* 以下两个函数与上述类似 */

#

int (void);/* 可用核数 */

int (void);/* 真正的反映了当前可用核数 */

使用指令

-> % ps

PID TTY TIME CMD

2683 pts/1 00:00:00 zsh

2726 pts/1 00:00:00

2930 pts/1 00:00:00 ps

-> % -p 2726

pid 2726's mask: 3

显示的十进制数字3转换为2进制为最低两个是1,每个1对应一个cpu,所以进程运行在2个cpu上。

-> % -pc 1 2726

pid 2726's list: 0,1

pid 2726's new list: 1

注意,cpu的标号是从0开始的,所以cpu1表示第二个cpu(第一个cpu的标号是0)。

至此,就把应用程序绑定到了cpu1上运行,查看如下:

-> % -p 2726

pid 2726's mask: 2

#启动时绑定到第二个cpu

-> % -c 1 ./&

[1] 3011

#查看确认绑定情况

-> % -p 3011

pid 3011's mask: 2

使用系统调用

可以将某个进程绑定到一个特定的CPU。

# /* See (7) */

#

/* 设置进程号为pid的进程运行在mask所设定的CPU上

* 第二个参数是mask所指定的数的长度

* 通常设定为()

* 如果pid的值为0,则表示指定的是当前进程

*/

int (pid_t pid, , *mask);

int (pid_t pid, , *mask);/* 获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中 */

#include
#include
#include
#include
#include
#define __USE_GNU
#include
#include
#include
#include
#define THREAD_MAX_NUM 200  //1个CPU内的最多进程数
int num=0;  //cpu中核数
void* threadFun(void* arg)  //arg  传递线程标号(自己定义)
{
         cpu_set_t mask;  //CPU核的集合
         cpu_set_t get;   //获取在集合中的CPU
         int *a = (int *)arg; 
         int i;
         printf("the thread is:%d\n",*a);  //显示是第几个线程
         CPU_ZERO(&mask);    //置空
         CPU_SET(*a,&mask);   //设置亲和力值
         if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力
         {
                   printf("warning: could not set CPU affinity, continuing...\n");
         }
           CPU_ZERO(&get);
           if (sched_getaffinity(0, sizeof(get), &get) == -1)//获取线程CPU亲和力
           {
                    printf("warning: cound not get thread affinity, continuing...\n");
           }
           for (i = 0; i < num; i++)
           {
                    if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力
                    {
                             printf("this thread %d is running processor : %d\n", i,i);
                    }
           }
         return NULL;
}
int main(int argc, char* argv[])
{
         int tid[THREAD_MAX_NUM];
         int i;
         pthread_t thread[THREAD_MAX_NUM];
         num = sysconf(_SC_NPROCESSORS_CONF);  //获取核数
         if (num > THREAD_MAX_NUM) {
            printf("num of cores[%d] is bigger than THREAD_MAX_NUM[%d]!\n", num, THREAD_MAX_NUM);
            return -1;
         }
         printf("system has %i processor(s). \n", num);
         for(i=0;i

-> % ./a.out

has 2 (s).

the is:0

the is:1

this 0 is : 0

this 1 is : 1

绑定线程到cpu核上运行

# /* See (7) */

#

int np( , , const *);

int np( , , *);

and link with -.

#define _GNU_SOURCE
#include 
#include 
#include 
#include 
#define handle_error_en(en, msg) \
        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
    int s, j;
    cpu_set_t cpuset;
    pthread_t thread;
    thread = pthread_self();
    /* Set affinity mask to include CPUs 0 to 7 */
    CPU_ZERO(&cpuset);
    for (j = 0; j < 8; j++)
        CPU_SET(j, &cpuset);
    s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
        handle_error_en(s, "pthread_setaffinity_np");
    /* Check the actual affinity mask assigned to the thread */
    s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
        handle_error_en(s, "pthread_getaffinity_np");
    printf("Set returned by pthread_getaffinity_np() contained:\n");
    for (j = 0; j < CPU_SETSIZE; j++)
        if (CPU_ISSET(j, &cpuset))
            printf("    CPU %d\n", j);
    exit(EXIT_SUCCESS);
}

-> % ./a.out

Set by np() :

CPU 0

CPU 1

扩展