<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      線程綁定cpu核心的代碼研究

      1、使用taskset指令
      1)獲取進(jìn)程pid

      [root@CENTOS57 eq]# ps aux | grep led
      root 9240 0.0 0.0 6324 376 pts/0 S 07:40 0:00 ./ledThread
      root 9242 0.0 0.0 112660 968 pts/0 S+ 07:40 0:00 grep --color=auto led
      2)查看進(jìn)程當(dāng)前運(yùn)行在哪個(gè)cpu核上

      p參數(shù)查看進(jìn)程的綁定cpu核。

      [root@CENTOS57 eq]# taskset -p 9240
      pid 9240's current affinity mask: 2
      顯示的十進(jìn)制數(shù)字2轉(zhuǎn)換為2進(jìn)制為10,每個(gè)1對(duì)應(yīng)一個(gè)cpu,所以進(jìn)程運(yùn)行在第2個(gè)cpu核上。

      3)指定進(jìn)程運(yùn)行在cpu3核上

      pc參數(shù)綁定cpu核。

      [root@CENTOS57 eq]# taskset -pc 3 9240
      pid 9240's current affinity list: 2
      pid 9240's new affinity list: 3

      [root@CENTOS57 eq]# taskset -p 9240
      pid 9240's current affinity mask: 8
      cpu的標(biāo)號(hào)是從0開(kāi)始的,所以cpu3表示第4個(gè)cpu(第一個(gè)cpu的標(biāo)號(hào)是0)。

      至此,就把應(yīng)用程序綁定到了cpu3上運(yùn)行

      4)啟動(dòng)程序時(shí)綁定cpu核

      #啟動(dòng)時(shí)綁定到第二個(gè)cpu1
      [root@CENTOS57 eq]# taskset -c 1 ./ledall &
      [1] 3011

      #查看確認(rèn)綁定情況
      [root@CENTOS57 eq]# taskset -p 3011
      pid 3011's current affinity mask: 2
      2、使用sched_setaffinity系統(tǒng)調(diào)用
      sched_setaffinity可以將某個(gè)進(jìn)程綁定到一個(gè)特定的CPU。

      注意:在使用時(shí),需要添加下面宏與頭文件,并且順序不可以顛倒。

      #define _GNU_SOURCE
      #include <sched.h>
      cpu_set_t mask;
      CPU_ZERO(&mask); //置空
      CPU_SET(n,&mask); //設(shè)置親和力值,綁定cpu核到(n)核上
      /* 設(shè)置進(jìn)程號(hào)為pid的進(jìn)程運(yùn)行在mask所設(shè)定的CPU上
      * 第二個(gè)參數(shù)cpusetsize是mask所指定的數(shù)的長(zhǎng)度
      * 通常設(shè)定為sizeof(cpu_set_t)
      * 如果pid的值為0,則表示指定的是當(dāng)前進(jìn)程
      */
      int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
      int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
      /*
      獲得pid所指示的進(jìn)程的CPU位掩碼,并將該掩碼返回到mask所指向的結(jié)構(gòu)中
      */
      例子1:pthread線程內(nèi)部綁核
      #include<stdlib.h>
      #include<stdio.h>
      #include<sys/types.h>
      #include<sys/sysinfo.h>
      #include<unistd.h>

      #define _GNU_SOURCE
      #include<sched.h>
      #include<ctype.h>
      #include<string.h>
      #include<pthread.h>
      #define THREAD_MAX_NUM 200 //1個(gè)CPU內(nèi)的最多進(jìn)程數(shù)

      int num=0; //cpu中核數(shù)
      void* threadFun(void* arg) //arg 傳遞線程標(biāo)號(hào)(自己定義)
      {
      cpu_set_t mask; //CPU核的掩碼mask
      cpu_set_t get; //獲取cpu掩碼
      int *a = (int *)arg;
      int i;

      printf("the thread is:%d\n",*a); //顯示是第幾個(gè)線程
      CPU_ZERO(&mask); //置空
      CPU_SET(*a,&mask); //設(shè)置親和力值,綁定cpu核到(*a)核上
      if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//設(shè)置線程CPU親和力
      {
      printf("warning: could not set CPU affinity \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))//判斷線程與哪個(gè)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); //獲取核數(shù)for(i=0;i<num;i++)
      {
      tid[i] = i; //每個(gè)線程必須有個(gè)tid[i]
      pthread_create(&thread[i],NULL,threadFun,(void*)&tid[i]);
      }
      for(i=0; i< num; i++)
      {
      pthread_join(thread[i],NULL);//等待所有的線程結(jié)束,線程為死循環(huán)所以CTRL+C結(jié)束
      }
      return 0;
      }
      例子2:main主線程綁核
      #define _GNU_SOURCE /* See feature_test_macros(7) */
      #include <stdio.h>
      #include <sys/types.h>
      #include <unistd.h>
      #include <sched.h>
      #include <pthread.h>

      void* testfunc(void* t) {
      while(1);
      return NULL;
      }

      int main()
      {
      cpu_set_t mask;
      printf("pid=%d\n", getpid());
      CPU_ZERO(&mask);
      CPU_SET(0, &mask);//將cpu0綁定
      sched_setaffinity(0, sizeof(cpu_set_t), &mask) ;

      pthread_t tid1;//創(chuàng)建線程1
      if (pthread_create(&tid1, NULL, (void *)testfunc, NULL) != 0)
      {
      fprintf(stderr, "thread create failed\n");
      return -1;
      }
      pthread_join(tid1, NULL);
      return 0;
      }
      例子3:main主函數(shù)內(nèi)部綁線程核
      #define _GNU_SOURCE /* See feature_test_macros(7) */
      #include <stdio.h>
      #include <sys/types.h>
      #include <unistd.h>
      #include <sched.h>
      #include <pthread.h>

      void* testfunc(void* t) {
      int i = 3;
      while(i) {
      sleep(5);
      printf("tid=%d,cpu=%d\n",pthread_self(), sched_getcpu());
      i--;
      }return NULL;
      }

      int main()
      {
      cpu_set_t mask;
      printf("pid=%d\n", getpid());
      CPU_ZERO(&mask);

      pthread_t tid1;
      if (pthread_create(&tid1, NULL, (void *)testfunc, NULL) != 0)
      {
      fprintf(stderr, "thread create failed\n");
      return -1;
      }
      pthread_t tid2;
      if (pthread_create(&tid2, NULL, (void *)testfunc, NULL) != 0)
      {
      fprintf(stderr, "thread create failed\n");
      return -1;
      }
      printf("tid1=%d,tid2=%d\n", tid1,tid2);

      CPU_SET(0, &mask);//綁定cpu0,線程綁定
      pthread_setaffinity_np(tid1, sizeof(cpu_set_t), &mask) ;

      //清除之前設(shè)置,重新設(shè)置綁定cpu3
      CPU_ZERO(&mask);
      CPU_SET(3, &mask);
      pthread_setaffinity_np(tid2, sizeof(cpu_set_t), &mask) ;

      pthread_join(tid1, NULL);
      pthread_join(tid1, NULL);
      return 0;
      }

      posted @ 2024-10-31 18:26  chromeplugin  閱讀(140)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 国产成人综合久久亚洲av| 99国产精品白浆在线观看免费| 91中文字幕一区在线| 天天爽夜夜爱| 九九久久人妻一区精品色| 日韩剧情片电影网站| 亚洲第一精品一二三区| 日韩午夜午码高清福利片| 少妇爽到爆视频网站免费| 福利成人午夜国产一区| 狠狠色狠狠色五月激情| 亚洲天堂在线观看完整版| 欧美成人精品一级在线观看| 久操线在视频在线观看| 国产一区二区三区不卡观| 欧美影院成年免费版| 亚洲一区二区av在线| 久久午夜无码免费| 扒开双腿猛进入喷水高潮叫声| 亚洲深深色噜噜狠狠网站| 人妻少妇中文字幕久久| 国产高清在线精品一本大道| 无码精品人妻一区二区三区湄公河| 成人国产一区二区三区精品| 久草热8精品视频在线观看| 夜鲁鲁鲁夜夜综合视频| 深夜av在线免费观看| 久久无码中文字幕免费影院蜜桃| 884aa四虎影成人精品| 乌兰浩特市| 亚洲人成色99999在线观看| 天天做天天爱夜夜夜爽毛片| 日韩在线视频线观看一区| 四虎在线成人免费观看| 国内女人喷潮完整视频| 亚洲男人AV天堂午夜在| 性视频一区| 久久人人爽人人爽人人av| 精品人妻一区二区三区蜜臀| 免费人成无码大片在线观看| 亚洲真人无码永久在线|