黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

進(jìn)程線程與cpu綁定

系統(tǒng) 2229 0

CPU Affinity


CPU親合力就是指在Linux系統(tǒng)中能夠?qū)⒁粋€(gè)或多個(gè)進(jìn)程綁定到一個(gè)或多個(gè)處理器上運(yùn)行.
一個(gè)進(jìn)程的CPU親合力掩碼決定了該進(jìn)程將在哪個(gè)或哪幾個(gè)CPU上運(yùn)行.在一個(gè)多處理器系統(tǒng)中,設(shè)置CPU親合力的掩碼可能會(huì)獲得更好的性能.
一個(gè)CPU的親合力掩碼用一個(gè)cpu_set_t結(jié)構(gòu)體來(lái)表示一個(gè)CPU集合,下面的幾個(gè)宏分別對(duì)這個(gè)掩碼集進(jìn)行操作:
CPU_ZERO() 清空一個(gè)集合
CPU_SET()與CPU_CLR()分別對(duì)將一個(gè)給定的CPU號(hào)加到一個(gè)集合或者從一個(gè)集合中去掉.
CPU_ISSET()檢查一個(gè)CPU號(hào)是否在這個(gè)集合中.
其實(shí)這幾個(gè)的用法與select()函數(shù)那幾個(gè)調(diào)用差不多.
下面兩個(gè)函數(shù)就是最主要的了:
sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)
該函數(shù)設(shè)置進(jìn)程為pid的這個(gè)進(jìn)程,讓它運(yùn)行在mask所設(shè)定的CPU上.如果pid的值為0,則表示指定的是當(dāng)前進(jìn)程,使當(dāng)前進(jìn)程運(yùn)行在mask所設(shè)定的那些CPU上.第二個(gè)參數(shù)cpusetsize是


mask所指定的數(shù)的長(zhǎng)度.通常設(shè)定為sizeof(cpu_set_t).如果當(dāng)前pid所指定的CPU此時(shí)沒(méi)有運(yùn)行在mask所指定的任意一個(gè)CPU上,則該指定的進(jìn)程會(huì)從其它CPU上遷移到mask的指定的


一個(gè)CPU上運(yùn)行.
sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)

該函數(shù)獲得pid所指示的進(jìn)程的CPU位掩碼,并將該掩碼返回到mask所指向的結(jié)構(gòu)中.即獲得指定pid當(dāng)前可以運(yùn)行在哪些CPU上.同樣,如果pid的值為0.也表示的是當(dāng)前進(jìn)程.


?

    # define __CPU_SETSIZE  1024

# define __NCPUBITS     (8 * sizeof (__cpu_mask))



typedef unsigned long int __cpu_mask;



# define __CPUELT(cpu)  ((cpu) / __NCPUBITS)

# define __CPUMASK(cpu) ((__cpu_mask) 1 << ((cpu) % __NCPUBITS))



typedef struct

{

  __cpu_mask __bits[__CPU_SETSIZE / __NCPUBITS];

} cpu_set_t;



# define __CPU_ZERO(cpusetp) \

  do {                                                                        \

    unsigned int __i;                                                         \

    cpu_set_t *__arr = (cpusetp);                                             \

    for (__i = 0; __i < sizeof (cpu_set_t) / sizeof (__cpu_mask); ++__i)      \

      __arr->__bits[__i] = 0;                                                 \

  } while (0)

# define __CPU_SET(cpu, cpusetp) \

  ((cpusetp)->__bits[__CPUELT (cpu)] |= __CPUMASK (cpu))

# define __CPU_CLR(cpu, cpusetp) \

  ((cpusetp)->__bits[__CPUELT (cpu)] &= ~__CPUMASK (cpu))

# define __CPU_ISSET(cpu, cpusetp) \

  (((cpusetp)->__bits[__CPUELT (cpu)] & __CPUMASK (cpu)) != 0)
  

?


例子:

?

    #include<stdlib.h>

#include<stdio.h>

#include<sys/types.h>

#include<sys/sysinfo.h>

#include<unistd.h>



#define __USE_GNU/*注意寫法*/

#include<sched.h>

#include<ctype.h>

#include<string.h>



int main(int argc, char* argv[])

{

        int num = sysconf(_SC_NPROCESSORS_CONF);

        int created_thread = 0;

        int myid;

        int i;

        int j = 0;



        cpu_set_t mask;

        cpu_set_t get;



        if (argc != 2)

        {

                printf("usage : ./cpu num\n");

                exit(1);

        }



        myid = atoi(argv[1]);



        printf("system has %i processor(s). \n", num);



        CPU_ZERO(&mask);

        CPU_SET(myid, &mask);



        if (sched_setaffinity(0, sizeof(mask), &mask) == -1)

        {

                printf("warning: could not set CPU affinity, continuing...\n");

        }

        while (1)

        {



                CPU_ZERO(&get);

                if (sched_getaffinity(0, sizeof(get), &get) == -1)

                {

                        printf("warning: cound not get cpu affinity, continuing...\n");

                }

                for (i = 0; i < num; i++)

                {

                        if (CPU_ISSET(i, &get))

                        {

                                printf("this process %d is running processor : %d\n",getpid(), i);

                        }

                }

        }

        return 0;

}


  

?




? 與進(jìn)程的情況相似,線程親和性的設(shè)置和獲取主要通過(guò)下面兩個(gè)函數(shù)來(lái)實(shí)現(xiàn):
? ? 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);


      #define _GNU_SOURCE

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <pthread.h>

#include <sched.h>



void *myfun(void *arg)

{

    cpu_set_t mask;

    cpu_set_t get;

    char buf[256];

    int i;

    int j;

    int num = sysconf(_SC_NPROCESSORS_CONF);

    printf("system has %d processor(s)\n", num);



    for (i = 0; i < num; i++) {

        CPU_ZERO(&mask);

        CPU_SET(i, &mask);

        if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {

            fprintf(stderr, "set thread affinity failed\n");

        }

        CPU_ZERO(&get);

        if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {

            fprintf(stderr, "get thread affinity failed\n");

        }

        for (j = 0; j < num; j++) {

            if (CPU_ISSET(j, &get)) {

                printf("thread %d is running in processor %d\n", (int)pthread_self(), j);

            }

        }

        j = 0;

        while (j++ < 100000000) {

            memset(buf, 0, sizeof(buf));

        }

    }

    pthread_exit(NULL);

}



int main(int argc, char *argv[])

{

    pthread_t tid;

    if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0) {

        fprintf(stderr, "thread create failed\n");

        return -1;

    }

    pthread_join(tid, NULL);

    return 0;

}
    


?

進(jìn)程線程與cpu綁定


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論