• <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++跨平臺開發

            久久国产高潮流白浆免费观看| 97精品国产91久久久久久| 久久青草国产精品一区| 亚洲国产成人久久精品影视| www.久久热.com| 青青热久久国产久精品| 久久精品国产欧美日韩99热| 久久99国产综合精品免费| 国产精品欧美亚洲韩国日本久久 | 伊人色综合九久久天天蜜桃| 伊人久久精品无码二区麻豆| 久久久久久夜精品精品免费啦| 国产精品无码久久综合网| 久久精品视频一| 久久久久无码中| 好久久免费视频高清| 久久精品国产欧美日韩99热| 久久国产精品-国产精品| 综合人妻久久一区二区精品| 久久久国产精品| 久久se精品一区二区| 精品久久久久久中文字幕大豆网 | 97精品国产97久久久久久免费| 欧美午夜精品久久久久久浪潮| 国产欧美一区二区久久| 99久久无色码中文字幕人妻| 欧美亚洲另类久久综合婷婷| 国产成人久久精品麻豆一区| 99精品国产在热久久无毒不卡| 久久人人爽人人爽人人片av麻烦| 久久综合给合综合久久| 精品久久人人妻人人做精品| 丁香五月网久久综合| 狠狠色婷婷综合天天久久丁香 | 久久线看观看精品香蕉国产| 久久久噜噜噜久久中文字幕色伊伊 | 久久午夜夜伦鲁鲁片免费无码影视 | 久久久久久精品无码人妻| 人人狠狠综合久久亚洲| 欧美伊人久久大香线蕉综合69| 99久久精品免费观看国产|