• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            在指定cpu的核心上執行線程

            Linux  (轉自cu)
            原文鏈接: http://linux.chinaunix.net/bbs/viewthread.php?tid=904906
            cpu.c

            [CODE]
            #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;
            }
            [/CODE]

            下面是在兩個終端分別執行了./cpu 0  ./cpu 2 后得到的結果. 效果比較明顯. 


            Cpu0  :  5.3%us,  5.3%sy,  0.0%ni, 87.4%id,  0.0%wa,  0.0%hi,  2.0%si,  0.0%st
            Cpu1  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
            Cpu2  :  5.0%us, 12.2%sy,  0.0%ni, 82.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
            Cpu3  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
            Cpu4  :  0.0%us,  0.0%sy,  0.0%ni, 99.7%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
            Cpu5  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
            Cpu6  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
            Cpu7  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

            #ifdef __USE_GNU
            /* Access macros for `cpu_set'.  */
            #define CPU_SETSIZE __CPU_SETSIZE
            #define CPU_SET(cpu, cpusetp)   __CPU_SET (cpu, cpusetp)
            #define CPU_CLR(cpu, cpusetp)   __CPU_CLR (cpu, cpusetp)
            #define CPU_ISSET(cpu, cpusetp) __CPU_ISSET (cpu, cpusetp)
            #define CPU_ZERO(cpusetp)       __CPU_ZERO (cpusetp)


            /* Set the CPU affinity for a task */
            extern int sched_setaffinity (__pid_t __pid, size_t __cpusetsize,
                                          __const cpu_set_t *__cpuset) __THROW;

            /* Get the CPU affinity for a task */
            extern int sched_getaffinity (__pid_t __pid, size_t __cpusetsize,
                                          cpu_set_t *__cpuset) __THROW;
            #endif
            #define __USE_GNU是為了使用CPU_SET()等宏. 具體在/usr/include/sched.h中有如下的定義. 



            CPU Affinity (CPU親合力)

            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.也表示的是當前進程.

            這幾個宏與函數的具體用法前面已經有講解.

            關于cpu_set_t的定義
            [CODE]
            # 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)

            [/CODE]

            在我的機器上sizeof(cpu_set_t)的大小為128,即一共有1024位.第一位代表一個CPU號.某一位為1則表示某進程可以運行在該位所代表的cpu上.例如
            CPU_SET(1, &mask);
            則mask所對應的第2位被設置為1.
            此時如果printf("%d\n", mask.__bits[0]);就打印出2.表示第2位被置為1了.



            具體我是參考man sched_setaffinity文檔中的函數的. 
            然后再參考了一下IBM的 developerWorks上的一個講解. 
            http://www.ibm.com/developerworks/cn/linux/l-affinity.html

            -----------------------------------------------------------------


            windows

              首先用API函數創建一個線程,
            HANDLE CreateThread(
            LPSECURITY_ATTRIBUTES
            lpThreadAttributes,  // SD
            SIZE_T dwStackSize, // initial stack size
            LPTHREAD_START_ROUTINE lpStartAddress, // thread function
            LPVOID lpParameter, // thread argument
            DWORD dwCreationFlags, // creation option
            LPDWORD lpThreadId     // thread identifier
            );
             
            通過調用SetThreadAffinityMask,就能為各個線程設置親緣性屏蔽:   
            DWORD_PTR SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffinityMask );
            該函數中的hThread參數用于指明要限制哪個線程,
            dwThreadAffinityMask用于指明該線程能夠在哪個CPU上運行。
            dwThreadAffinityMask必須是進程的親緣性屏蔽的相應子集。
            返回值是線程的前一個親緣性屏蔽。因此,若要將3個線程限制到CPU1、2和3上去運行,可以這樣操作:
            //Thread 0 can only run on CPU 0.
            SetThreadAffinityMask(hThread0, 0x00000001);

            //Threads 1, 2, 3 run on CPUs 1.
            SetThreadAffinityMask(hThread1, 0x0000000E);

            posted on 2009-12-29 16:57 Khan 閱讀(5071) 評論(0)  編輯 收藏 引用 所屬分類: GCC/G++跨平臺開發

            大香伊人久久精品一区二区 | 婷婷久久久亚洲欧洲日产国码AV | 精品久久久久久国产牛牛app| 色综合久久久久久久久五月| 久久人人爽人人人人爽AV | 色婷婷狠狠久久综合五月| 精品无码久久久久久久久久| 久久最新精品国产| 精品国产综合区久久久久久 | 久久久久亚洲精品男人的天堂 | 久久国产精品-久久精品| 青青国产成人久久91网| 久久99精品久久久久久噜噜| 久久亚洲天堂| 久久久久久精品成人免费图片| 久久人人青草97香蕉| 亚洲AV无码1区2区久久| 99久久久国产精品免费无卡顿| 久久91精品国产91久久麻豆| 国产精品久久久99| 国产成人精品综合久久久久| 久久综合狠狠综合久久| 久久青草国产精品一区| 偷窥少妇久久久久久久久| 久久综合给合久久狠狠狠97色 | 国产国产成人精品久久| 久久久久无码精品国产app| 国产69精品久久久久APP下载 | 久久久久久久久66精品片| 色妞色综合久久夜夜| 99久久精品国产综合一区| 久久久久久曰本AV免费免费| 久久精品国产亚洲AV麻豆网站 | 久久精品国产精品青草app| 欧美一级久久久久久久大| 热re99久久精品国99热| 久久久久人妻一区精品| 国产韩国精品一区二区三区久久| 亚洲AⅤ优女AV综合久久久| 色综合久久天天综合| 午夜天堂av天堂久久久|