Dual-core CPU killer makes your Linux process run faster!
When using Linux systems, we often encounter some CPU-intensive tasks, such as data processing, compiling programs, etc., which require a large amount of computing resources to complete. However, if our system hardware configuration is low, it will cause slow task execution and seriously affect our work efficiency. To address this problem, this article will introduce a technology called "CPU binding", which can bind a process to a specific CPU core, thereby improving the execution efficiency of the process.
For ordinary applications, the default scheduling mechanism of the operating system is no problem. However, when a process requires higher operating efficiency, it is necessary to consider binding it to a separate core to reduce the overhead caused by scheduling on different cores.
After binding a process/thread to a specific CPU core, the process will always run on this core and will no longer be scheduled by the operating system to other cores. But the bound core may still be scheduled to run other applications.
Scheduling of multi-core CPUs by the operating system
Currently both windows and linux support scheduling and management of multi-core CPUs.
The core of software development in a multi-core environment is multi-threaded development. This multi-threading not only represents multi-threading in software implementation, but also requires the use of multi-threading technology in hardware.
The focus of multi-core operating systems is on process allocation and scheduling. Process allocation allocates processes to reasonable physical cores, because different cores have different sharing and historical operating conditions. Some physical cores can share the second level cache, while others are independent. If a process with data sharing is allocated to a core with a shared secondary cache, performance will be greatly improved; otherwise, performance may be affected.
Process scheduling will involve real-time, load balancing and other issues. Current research hot issues mainly focus on the following aspects:
- Parallel development and design of programs
- Time correlation of multiple processes
- Task allocation and scheduling
- Cached error sharing
- Consistent access issue
- Inter-process communication
- Competition for resources within multi-processor cores
The situation when multiple processes and multi-threads are running on the CPU core is as follows:
When each CPU core runs a process, since the resources of each process are independent, there is no need to consider the context when switching between CPU cores
When each CPU core runs a thread, sometimes the threads need to share resources, so these resources must be copied from one core of the CPU to another, which will cause additional overhead
Bind the process to run on the cpu core
Check how many cores the cpu has
Use cat /proc/cpuinfo to view cpu information, the following two information:
processor, specify which cpu processor
cpu cores, specify the number of cores per processor
You can also use the system call sysconf to obtain the number of cpu cores:
#include int sysconf(_SC_NPROCESSORS_CONF);/* 返回系统可以使用的核数,但是其值会包括系统中禁用的核的数目,因 此该值并不代表当前系统中可用的核数 */ int sysconf(_SC_NPROCESSORS_ONLN);/* 返回值真正的代表了系统当前可用的核数 */ /* 以下两个函数与上述类似 */ #include int get_nprocs_conf (void);/* 可用核数 */ int get_nprocs (void);/* 真正的反映了当前可用核数 */
I am using a virtual machine with 2 processors. Each processor has only one core, which is equivalent to two cores on one processor.
Use taskset directive
Get process pid
-> % ps PID TTY TIME CMD 2683 pts/1 00:00:00 zsh 2726 pts/1 00:00:00 dgram_servr 2930 pts/1 00:00:00 ps
View which CPU the process is currently running on
-> % taskset -p 2726 pid 2726's current affinity mask: 3
The displayed decimal number 3 is converted to binary and the lowest two are 1. Each 1 corresponds to a CPU, so the process runs on 2 CPUs.
Specify the process to run on cpu1
-> % taskset -pc 1 2726 pid 2726's current affinity list: 0,1 pid 2726's new affinity list: 1
Note that the cpu label starts from 0, so cpu1 represents the second cpu (the first cpu label is 0).
At this point, the application is bound to cpu1 to run. See the following:
-> % taskset -p 2726 pid 2726's current affinity mask: 2
Bind cpu when starting the program
#启动时绑定到第二个cpu -> % taskset -c 1 ./dgram_servr& [1] 3011 #查看确认绑定情况 -> % taskset -p 3011 pid 3011's current affinity mask: 2
Use sched_setaffinity system call
sched_setaffinity can bind a process to a specific CPU.
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include /* 设置进程号为pid的进程运行在mask所设定的CPU上 * 第二个参数cpusetsize是mask所指定的数的长度 * 通常设定为sizeof(cpu_set_t) * 如果pid的值为0,则表示指定的是当前进程 */ 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所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中 */
Example
#include #include #include #include #include #define __USE_GNU #include #include #include #include #define THREAD_MAX_NUM 200 //1个CPU内的最多进程数 int num=0; //cpu中核数 void* threadFun(void* arg) //arg 传递线程标号(自己定义) { cpu_set_t mask; //CPU核的集合 cpu_set_t get; //获取在集合中的CPU int *a = (int *)arg; int i; printf("the thread is:%d\n",*a); //显示是第几个线程 CPU_ZERO(&mask); //置空 CPU_SET(*a,&mask); //设置亲和力值 if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力 { printf("warning: could not set CPU affinity, continuing...\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 if (CPU_ISSET(i, &get))//判断线程与哪个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); //获取核数 if (num > THREAD_MAX_NUM) { printf("num of cores[%d] is bigger than THREAD_MAX_NUM[%d]!\n", num, THREAD_MAX_NUM); return -1; } printf("system has %i processor(s). \n", num); for(i=0;ifor(i=0; ireturn 0; }
operation result
-> % ./a.out system has 2 processor(s). the thread is:0 the thread is:1 this thread 0 is running processor : 0 this thread 1 is running processor : 1
Bind the thread to run on the cpu core
Bind threads to the cpu core using the pthread_setaffinity_np function. Its prototype is defined as follows:
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset); int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset); Compile and link with -pthread.
The meaning of each parameter is similar to sched_setaffinity.
Example
#define _GNU_SOURCE #include #include #include #include #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { int s, j; cpu_set_t cpuset; pthread_t thread; thread = pthread_self(); /* Set affinity mask to include CPUs 0 to 7 */ CPU_ZERO(&cpuset); for (j = 0; j if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np"); printf("Set returned by pthread_getaffinity_np() contained:\n"); for (j = 0; j if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j); exit(EXIT_SUCCESS); }
operation result
-> % ./a.out Set returned by pthread_getaffinity_np() contained: CPU 0 CPU 1
Through the introduction of this article, we have learned how to use CPU binding technology to bind a process to a specific CPU core, thereby significantly improving the execution efficiency of the process. In actual applications, we can choose appropriate CPU binding solutions according to different scenarios and needs to achieve the best performance improvement effect. I hope this article can help readers better understand and apply CPU binding technology, and achieve higher work efficiency in the use of Linux systems.
The above is the detailed content of Dual-core CPU killer makes your Linux process run faster!. For more information, please follow other related articles on the PHP Chinese website!


For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
