top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to set that a particular thread always be executed by a specific core in multi-core Linux environment?

+2 votes
550 views
How to set that a particular thread always be executed by a specific core in multi-core Linux environment?
posted Jun 29, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

See the following snippet to set the particular thread to particular core..

    core = CORE_2;                                                       
    cpu_set_t cpuset;                                                           
    pthread_t thread;                                                           
    thread = pthread_self();                                                    
    CPU_ZERO(&cpuset);                                                          
    CPU_SET(core, &cpuset);                                                     

    ret = pthread_setaffinity_np(thread,                                        
              sizeof(cpu_set_t), &cpuset);                                                   
    ret = pthread_getaffinity_np(thread,                                        
            sizeof(cpu_set_t), &cpuset);                                        

    if (CPU_ISSET(core, &cpuset))                                               
        printf("CPU [%d]\n",core);                              
answer Jun 30, 2016 by Jaganathan
Hi Jaganathan,

Where is this macro 'CORE_2' defined? Also, what data type is variable 'core'?

Regards
Hi Jaganathan,
can you give example clearly, I am a begginer.
Hi upayan

Core is integer variable, CORE_2 is user defined value of particular core number. It start from 0 to N number of core

Hi mohan,

After creating a thread you can set this property any time.
+3 votes

Elaborating Jaganathan's reply:

The header file to include is sched.h, for using CPU_ZERO and CPU_SET macros.
If I am right the #define _GNU_SOURCE macro should also be defined.

cpu_set_t is a bitset where each bit represents a CPU. How the system’s CPUs are mapped to bits in the bitset is system dependent. To manipulate this bitset, to set and reset bits, a number of macros are defined:
CPU_ZERO, CPU_SET, CPU_CLR etc.

The pthread_setaffinity_np, as its already understandable, sets the affinity of thread to the cpuset defined above using CPU_SET macro.

Regards,
Upayan.

answer Jul 2, 2016 by Upayan Dutta
...