"Process/thread bound CPU" for Linux programming

Source: Internet
Author: User

Intro
-----

Usually when we write server code, we can improve the efficiency of the system scheduler by binding the current process to a fixed CPU core or thread binding to a fixed CPU core, and then paste the complete code below.
```
/************************************************
* This routine explains the process, thread binding to a fixed CPU core running on
* To improve the operational efficiency of the program
************************************************/
#include <unistd.h>
#ifndef __USE_GNU
#define __USE_GNU//In order to use Set_set () and other macro definitions, but unistd.h inside seems to have defined
#endif
#include <sched.h>
#include <pthread.h>
#include <stdio.h>
#include <vector>

unsigned int systemcpunum ()
{
The value of _sc_nprocessors_conf is the number of CPUs, based on the 0 start number
Return sysconf (_sc_nprocessors_conf);
}

BOOL Currentprocessaffinity (std::vector<unsigned int>& runningcpuvector)
{
cpu_set_t Cpuset;

Emptying a collection of CPUs
Cpu_zero (&cpuset);

Gets the specified CPU to which the process ID is bound
int ret = sched_getaffinity (0,//0 represents the current process
sizeof (Cpuset),
&cpuset);
if (Ret < 0)
{
return false;
}

unsigned int cpunum = Systemcpunum ();
Runningcpuvector.clear ();
for (unsigned int i = 0; i < cpunum; ++i)
{
Check if a CPU number is in a collection
if (Cpu_isset (i, &cpuset))
{
Runningcpuvector.push_back (i);
}
}

return true;
}

BOOL Setcurrentprocessaffinity (const std::vector<unsigned int>& needbindcpuvector)
{
cpu_set_t Cpuset;

Emptying a collection of CPUs
Cpu_zero (&cpuset);

for (auto& Iter:needbindcpuvector)
{
Cpu_set (ITER, &cpuset);
}

Binds the specified process ID to the CPU
int ret = sched_setaffinity (0,//0 represents the current process
sizeof (Cpuset),
&cpuset);
if (Ret < 0)
{
return false;
}

return true;
}

BOOL Currentthreadaffinity (std::vector<unsigned int>& runningcpuvector)
{
cpu_set_t Cpuset;

Emptying a collection of CPUs
Cpu_zero (&cpuset);

Gets the CPU to which the specified thread ID is bound
int ret = PTHREAD_GETAFFINITY_NP (Pthread_self (),
sizeof (Cpuset),
&cpuset);
if (Ret < 0)
{
return false;
}

unsigned int cpunum = Systemcpunum ();
Runningcpuvector.clear ();
for (unsigned int i = 0; i < cpunum; ++i)
{
Check if a CPU number is in a collection
if (Cpu_isset (i, &cpuset))
{
Runningcpuvector.push_back (i);
}
}

return true;
}

BOOL Setcurrentthreadaffinity (const std::vector<unsigned int>& needbindcpuvector)
{
cpu_set_t Cpuset;

Emptying a collection of CPUs
Cpu_zero (&cpuset);

for (auto& Iter:needbindcpuvector)
{
Cpu_set (ITER, &cpuset);
}

Binds the specified thread ID to the CPU
int ret = PTHREAD_SETAFFINITY_NP (Pthread_self (),
sizeof (Cpuset),
&cpuset);
if (Ret < 0)
{
return false;
}

return true;
}

int main ()
{
printf ("*****process bind CPU sample*****\n");
unsigned int cpunum = Systemcpunum ();
printf ("Current system has%u CPU (s) \ n", cpunum);

std::vector<unsigned int> Runningcpuvector;
if (!currentprocessaffinity (Runningcpuvector))
{
printf ("Get current process is bound witch CPU failed\n");
return 1;
}

for (auto& Iter:runningcpuvector)
{
printf ("Current process was running at%u cpu\n", ITER);
}

std::vector<unsigned int> needbindcpuvector {0, 2};
if (!setcurrentprocessaffinity (Needbindcpuvector))
{
printf ("Current process bind CPU failed\n");
return 1;
}

printf ("Current process bind CPU success\n");

Runningcpuvector.clear ();
if (!currentprocessaffinity (Runningcpuvector))
{
printf ("Get current process is bound witch CPU failed\n");
return 1;
}

for (auto& Iter:runningcpuvector)
{
printf ("Current process was running at%u cpu\n", ITER);
}

printf ("\n*****thread bind CPU sample*****\n");
Runningcpuvector.clear ();
if (!currentthreadaffinity (Runningcpuvector))
{
printf ("Get current thread is bound witch CPU failed\n");
return 1;
}

for (auto& Iter:runningcpuvector)
{
printf ("Thread%lu is running at%u cpu\n", pthread_self (), ITER);
}

Needbindcpuvector.clear ();
Needbindcpuvector.push_back (1);
if (!setcurrentthreadaffinity (Needbindcpuvector))
{
printf ("Current thread bind CPU failed\n");
return 1;
}

printf ("Thread%lu bind CPU success\n", pthread_self ());

Runningcpuvector.clear ();
if (!currentthreadaffinity (Runningcpuvector))
{
printf ("Get current thread is bound witch CPU failed\n");
return 1;
}

for (auto& Iter:runningcpuvector)
{
printf ("Thread%lu is running at%u cpu\n", pthread_self (), ITER);
}

return 0;
}
```

Output from program execution:
```
Process bind CPU sample*****
Current system have 4 CPU (s)
Current process was running at 0 CPU
Current process was running at 1 CPU
Current process was running at 2 CPU
Current process was running at 3 CPU
Current process bind CPU success
Current process was running at 0 CPU
Current process was running at 2 CPU

Thread bind CPU sample*****
Thread 139871129114432 is running at 0 CPU
Thread 139871129114432 is running at 2 CPU
Thread 139871129114432 bind CPU Success
Thread 139871129114432 is running at 1 CPU
```

The example of GitHub address: Https://github.com/chxuan/samples/blob/master/BindCPU/BindCPU.cpp

"Process/thread bound CPU" for Linux programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.