欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

進程線程與cpu綁定

系統 1891 0

CPU Affinity


CPU親合力就是指在Linux系統中能夠將一個或多個進程綁定到一個或多個處理器上運行.
一個進程的CPU親合力掩碼決定了該進程將在哪個或哪幾個CPU上運行.在一個多處理器系統中,設置CPU親合力的掩碼可能會獲得更好的性能.
一個CPU的親合力掩碼用一個cpu_set_t結構體來表示一個CPU集合,下面的幾個宏分別對這個掩碼集進行操作:
CPU_ZERO() 清空一個集合
CPU_SET()與CPU_CLR()分別對將一個給定的CPU號加到一個集合或者從一個集合中去掉.
CPU_ISSET()檢查一個CPU號是否在這個集合中.
其實這幾個的用法與select()函數那幾個調用差不多.
下面兩個函數就是最主要的了:
sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)
該函數設置進程為pid的這個進程,讓它運行在mask所設定的CPU上.如果pid的值為0,則表示指定的是當前進程,使當前進程運行在mask所設定的那些CPU上.第二個參數cpusetsize是


mask所指定的數的長度.通常設定為sizeof(cpu_set_t).如果當前pid所指定的CPU此時沒有運行在mask所指定的任意一個CPU上,則該指定的進程會從其它CPU上遷移到mask的指定的


一個CPU上運行.
sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *mask)

該函數獲得pid所指示的進程的CPU位掩碼,并將該掩碼返回到mask所指向的結構中.即獲得指定pid當前可以運行在哪些CPU上.同樣,如果pid的值為0.也表示的是當前進程.


?

    # 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;

}


  

?




? 與進程的情況相似,線程親和性的設置和獲取主要通過下面兩個函數來實現:
? ? 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;

}
    


?

進程線程與cpu綁定


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 丁香久久 | 欧美国产另类 | 成人精品视频 成人影院 | 色人阁在线 | 中国在线播放精品区 | 亚洲免费视 | 一区影院| 亚洲综合色视频在线观看 | 国内成人自拍视频 | 久久综合欧美 | 999久久久免费精品国产 | 免费一级欧美性大片 | 天天拍天天干天天操 | 日韩不卡在线 | 狠狠做深爱婷婷久久一区 | 久久不卡一区二区三区 | 成人国产精品视频 | 日本高清不卡一区久久精品 | 国产亚洲精品久久久久久一区二区 | 久久精品国产免费 | 美女视频黄在线观看 | 欧美成人精品二区三区99精品 | www.99re| 日韩观看 | 成人淫片免费视频95视频 | 第四色婷婷墓地 | 久草视频播放 | 免费网站色 | 超级在线牛碰碰视频 | 成人网在线观看 | 久综合网 | www.成人.com| 草久久久 | 香港午夜三级a三级高清观看 | 草草影院永久地址 | 日韩精品真人荷官无码 | 91传媒蜜桃香蕉在线观看 | 成年人看的视频网站 | 久久久久久亚洲精品 | 国产九色在线观看 | 无码色情影片视频在线看免费 |